Python數(shù)據(jù)結(jié)構(gòu)之單鏈表詳解
本文實(shí)例為大家分享了Python數(shù)據(jù)結(jié)構(gòu)之單鏈表的具體代碼,供大家參考,具體內(nèi)容如下
# 節(jié)點(diǎn)類(lèi)
class Node():
__slots__=['_item','_next'] # 限定Node實(shí)例的屬性
def __init__(self,item):
self._item = item
self._next = None # Node的指針部分默認(rèn)指向None
def getItem(self):
return self._item
def getNext(self):
return self._next
def setItem(self,newitem):
self._item = newitem
def setNext(self,newnext):
self._next=newnext
# 單鏈表
class SingleLinkedList():
def __init__(self):
self._head = None #初始化鏈表為空 始終指向鏈表的頭部
self._size = 0 # 鏈表大小
# 返回鏈表的大小
def size(self):
current = self._head
count = 0
while current != None:
count += 1
current = current.getNext()
return count
# 遍歷鏈表
def travel(self):
current = self._head
while current != None:
print(current.getItem())
current = current.getNext()
# 檢查鏈表是否為空
def isEmpty(self):
return self._head == None
# 在鏈表前端添加元素
def add(self,item):
temp = Node(item) # 創(chuàng)建新的節(jié)點(diǎn)
temp.setNext(self._head) # 新創(chuàng)建的next指針指向_head
self._head = temp # _head指向新創(chuàng)建的指針
# 在鏈表尾部添加元素
def append(self,item):
temp = Node(item)
if self.isEmpty():
self._head = temp # 若為空表就直接插入
else:
current = self._head
while current.getNext() != None:
current = current.getNext() # 遍歷列表
current.setNext(temp) # 此時(shí)current為鏈表最后的元素,在末尾插入
# 檢索元素是否在鏈表中
def search(self,item):
current = self._head
founditem = False
while current != None and not founditem:
if current.getItem() == item:
founditem = True
else:
current = current.getNext()
return founditem
# 索引元素在表中的位置
def index(self,item):
current = self._head
count = 0
found = None
while current != None and not found:
count += 1
if current.getItem() == item:
found = True
else:
current = current.getNext()
if found:
return count
else:
return -1 # 返回-1表示不存在
# 刪除表中的某項(xiàng)元素
def remove(self,item):
current = self._head
pre = None
while current!=None:
if current.getItem() == item:
if not pre:
self._head = current.getNext()
else:
pre.setNext(current.getNext())
break
else:
pre = current
current = current.getNext()
# 在鏈表任意位置插入元素
def insert(self,pos,item):
if pos <= 1:
self.add(item)
elif pos > self.size():
self.append(item)
else:
temp = Node(item)
count = 1
pre = None
current = self._head
while count < pos:
count += 1
pre = current
current = current.getNext()
pre.setNext(temp)
temp.setNext(current)
if __name__=='__main__':
a=SingleLinkedList()
for i in range(1,10):
a.append(i)
print('鏈表的大小',a.size())
a.travel()
print(a.search(6))
print(a.index(5))
a.remove(4)
a.travel()
a.insert(4,100)
a.travel()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)順序表的簡(jiǎn)單代碼
- Python中順序表的實(shí)現(xiàn)簡(jiǎn)單代碼分享
- Python數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)代碼示例
- python數(shù)據(jù)結(jié)構(gòu)之線性表的順序存儲(chǔ)結(jié)構(gòu)
- python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實(shí)現(xiàn)線性表的順序
- Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之鏈表詳解
- python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實(shí)例講解)
- Python數(shù)據(jù)結(jié)構(gòu)之翻轉(zhuǎn)鏈表
- python數(shù)據(jù)結(jié)構(gòu)之鏈表詳解
- Python 數(shù)據(jù)結(jié)構(gòu)之旋轉(zhuǎn)鏈表
- Python中順序表原理與實(shí)現(xiàn)方法詳解
相關(guān)文章
Python實(shí)現(xiàn)在線暴力破解郵箱賬號(hào)密碼功能示例【測(cè)試可用】
這篇文章主要介紹了Python實(shí)現(xiàn)在線暴力破解郵箱賬號(hào)密碼功能,結(jié)合完整實(shí)例形式分析了Python讀取txt字典文件針對(duì)郵箱的相關(guān)驗(yàn)證破解操作技巧,需要的朋友可以參考下2017-09-09
python中csv文件數(shù)據(jù)顏色設(shè)置方式
這篇文章主要介紹了python中csv文件數(shù)據(jù)顏色設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
如何用Python?實(shí)現(xiàn)景區(qū)安防系統(tǒng)
本設(shè)計(jì)中,利用YOLO目標(biāo)檢測(cè)算法、Openpose姿態(tài)識(shí)別算法、deepsort跟蹤算法、MSCNN人群密度估計(jì)算法實(shí)現(xiàn)了火災(zāi)監(jiān)測(cè)、吸煙監(jiān)測(cè)、行為安全監(jiān)測(cè)、人群密度監(jiān)測(cè)、口罩率監(jiān)測(cè)、人員定位監(jiān)測(cè)六大功能,對(duì)Python?實(shí)現(xiàn)景區(qū)安防系統(tǒng)感興趣的朋友一起看看吧2022-07-07
Python決策樹(shù)分類(lèi)算法學(xué)習(xí)
這篇文章主要為大家詳細(xì)介紹了Python決策樹(shù)分類(lèi)算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Pyspark 線性回歸梯度下降交叉驗(yàn)證知識(shí)點(diǎn)詳解
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于Pyspark 線性回歸梯度下降交叉驗(yàn)證的相關(guān)知識(shí)點(diǎn)及實(shí)例,需要的朋友們可以參考下。2021-12-12
Django的用戶模塊與權(quán)限系統(tǒng)的示例代碼
這篇文章主要介紹了Django的用戶模塊與權(quán)限系統(tǒng)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python利用yarl實(shí)現(xiàn)輕松操作url
在諸如網(wǎng)絡(luò)爬蟲(chóng)、web應(yīng)用開(kāi)發(fā)等場(chǎng)景中,我們需要利用Python完成大量的url解析、生成等操作。本文為大家介紹了Pythonyarl操作url的方法,需要的可以了解一下2022-10-10
pandas中iloc函數(shù)的具體實(shí)現(xiàn)
iloc是Pandas中用于基于整數(shù)位置進(jìn)行索引和切片的方法,本文主要介紹了pandas中iloc函數(shù)的具體實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
基于Python實(shí)現(xiàn)主機(jī)遠(yuǎn)程控制
這篇文章主要介紹了基于Python實(shí)現(xiàn)主機(jī)遠(yuǎn)程控制,本文為?HITwh?網(wǎng)絡(luò)空間安全專業(yè)網(wǎng)絡(luò)空間安全設(shè)計(jì)與實(shí)踐選題,主要實(shí)現(xiàn)了遠(yuǎn)程監(jiān)控局域網(wǎng)內(nèi)的主機(jī)桌面與網(wǎng)絡(luò)情況、簡(jiǎn)單鍵鼠控制、遠(yuǎn)程斷網(wǎng)(ARP?攻擊)、數(shù)據(jù)加密傳輸?shù)裙δ?,下面?lái)看看具體實(shí)現(xiàn)過(guò)程吧2022-01-01

