python中的單向鏈表實現(xiàn)
更新時間:2022年01月29日 08:58:31 作者:tt丫
大家好,本篇文章主要講的是python中的單向鏈表實現(xiàn),感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
一、單向鏈表概念
單向鏈表的鏈接方向是單向的,由結點構成,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十、查找是否有該數(shù)據(jù)的節(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)十二、輸入數(shù)據(jù)創(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)十三、具體實現(xiàn)
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個數(shù)據(jù)
linkList.ergodic() #遍歷輸出
print(linkList.length()) #輸出鏈表長度
linkList.insert(89,2) #指定位置插入數(shù)據(jù)
linkList.ergodic() #遍歷輸出 到此這篇關于python中的單向鏈表實現(xiàn)的文章就介紹到這了,更多相關python單向鏈表內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python使用正則表達式獲取網(wǎng)頁中所需要的信息
這篇文章主要介紹了Python使用正則獲取網(wǎng)頁中所需要的信息的相關資料,需要的朋友可以參考下2018-01-01
python實現(xiàn)一個函數(shù)版的名片管理系統(tǒng)過程解析
這篇文章主要介紹了python實現(xiàn)一個函數(shù)版的名片管理系統(tǒng)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
一文搞懂Python中Pandas數(shù)據(jù)合并
pandas是基于NumPy的一種工具,該工具是為了解決數(shù)據(jù)分析任務而創(chuàng)建的。Pandas納入了大量庫和一些標準的數(shù)據(jù)模型,提供了高效操作大型數(shù)據(jù)集的工具。pandas提供大量快速便捷地處理數(shù)據(jù)的函數(shù)和方法。你很快就會發(fā)現(xiàn),它是使Python強大而高效的數(shù)據(jù)分析環(huán)境的重要因素之一2021-11-11
Python使用sqlalchemy實現(xiàn)連接數(shù)據(jù)庫的幫助類
這篇文章主要為大家詳細介紹了Python如何使用sqlalchemy實現(xiàn)連接數(shù)據(jù)庫的幫助類,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考下2024-02-02
Python使用pyinstaller打包spec文件的方法詳解
PyInstaller是一個用于將Python腳本打包成獨立的可執(zhí)行文件的工具,使用PyInstaller您可以將Python應用程序轉換為可執(zhí)行文件,而無需用戶安裝Python解釋器或任何額外的庫,這篇文章主要給大家介紹了關于Python使用pyinstaller打包spec文件的相關資料,需要的朋友可以參考下2024-08-08

