python中的單向鏈表實現
更新時間:2022年01月29日 08:58:31 作者:tt丫
大家好,本篇文章主要講的是python中的單向鏈表實現,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
一、單向鏈表概念
單向鏈表的鏈接方向是單向的,由結點構成,head指針指向第一個成為head結點,而終止于最后一個指向None的指針,對鏈表的訪問要通過順序讀取從頭部開始。
二、建立節(jié)點對象
class Node: def __init__(self,data): self.data = data #節(jié)點的值域 self.next = None #連接下一個節(jié)點,暫時指向空
三、鏈表對象的初始定義
class linkList: def __init__(self): self.head = None #首先建立鏈表頭,暫時指向空
四、判斷鏈表是否為空
#判斷鏈表是否為空 def isEmpty(self): if self.head: return False else: return True
五、獲取鏈表長度
def length(self): if self.isEmpty(): return 0 else: t = self.head n = 1 while t.next: t = t.next n = n + 1 return n
六、向頭部添加節(jié)點
def addhead(self,data): node = Node(data) #新建一個節(jié)點 node.next = self.head #新建的節(jié)點接上原來的鏈表 self.head = node #重置鏈表的頭
七、向尾部添加節(jié)點
def addtail(self,data): node = Node(data) #新建一個節(jié)點 #先判斷鏈表是否為空 if self.isEmpty(): self.addhead(data) else: t = self.head while t.next: #通過循環(huán)找到尾部 t = t.next t.next = node #尾部接上
八、指定位置插入節(jié)點
def insert(self,data,index): if index == 0 or self.isEmpty(): self.addhead(data) elif index >= self.length(): self.addtail(data) else: node = Node(data) t = self.head n = 1 while n < index - 1: t = t.next n = n + 1 a = t.next.next t.next = node node.next = a
九、刪除指定位置的節(jié)點
def delete(self,index): if self.isEmpty(): print("The linked list is empty") else: t = self.head if index == 0: self.head = t.next elif index == self.length() - 1: n = 1 while n < self.length() - 1: t = t.next n = n + 1 t.next = None elif index > self.length() - 1: print("Out of range") elif index < 0: print("Wrong operation") else: n = 1 while n < index - 1: t = t.next n = n + 1 a = t.next.next t.next = a
十、查找是否有該數據的節(jié)點
def search(self,data): t = self.head n = 1 while t.next: if t.data == data: print(str(n) + " ") t = t.next n = n + 1 if (t.data == data): print(str(n) + " ")
十一、遍歷輸出整個鏈表
def form(self,datalist): self.addhead(datalist[0]) for i in range(1,len(datalist)): self.addtail(datalist[i]) t = self.head while t.next: print(t.data) t = t.next print(t.data)
十二、輸入數據創(chuàng)建鏈表
def form(self,datalist): self.addhead(datalist[0]) for i in range(1,len(datalist)): self.addtail(datalist[i]) t = self.head while t.next: print(t.data) t = t.next print(t.data)
十三、具體實現
data = input("input(以空格為界):") data = data.split(" ") linkList = linkList() linkList.form(data) #創(chuàng)建鏈表 addlist = linkList.addhead(5) #在頭節(jié)點加入 linkList.ergodic() #遍歷輸出 addlist = linkList.addtail(5) #在尾節(jié)點加入 linkList.ergodic() #遍歷輸出 linkList.search(5) #查找是否有"5"的節(jié)點 linkList.delete(4) #刪除第4個數據 linkList.ergodic() #遍歷輸出 print(linkList.length()) #輸出鏈表長度 linkList.insert(89,2) #指定位置插入數據 linkList.ergodic() #遍歷輸出
到此這篇關于python中的單向鏈表實現的文章就介紹到這了,更多相關python單向鏈表內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Pandas時間序列重采樣(resample)方法中closed、label的作用詳解
這篇文章主要介紹了Pandas時間序列重采樣(resample)方法中closed、label的作用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12Python numpy.array()生成相同元素數組的示例
今天小編就為大家分享一篇Python numpy.array()生成相同元素數組的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11Pygame游戲開發(fā)之太空射擊實戰(zhàn)子彈與碰撞處理篇
相信大多數8090后都玩過太空射擊游戲,在過去游戲不多的年代太空射擊自然屬于經典好玩的一款了,今天我們來自己動手實現它,在編寫學習中回顧過往展望未來,下面開始講解子彈與碰撞處理,在本課中,我們將添加玩家與敵人之間的碰撞,以及添加供玩家射擊的子彈2022-08-08