日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3 链表_python3实现链表

發布時間:2025/3/15 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 链表_python3实现链表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#鏈表節點結構實現 私有屬性_pro_item是指向下個節點的指針,_item為此節點的值

classNode():def __init__(self,item = None,pos_item=None):

self._item=item

self._next=pos_itemdef __repr__(self):'''用來定義Node的字符輸出,

print為輸出item'''

returnstr(self._item)#單鏈表實現

classChain():def __init__(self):

self._head=None

self.length=0#判空

defisEmpty(self):return self.length ==0#鏈表結尾插入

defappend(self,item):ifisinstance(item,Node):

node=itemelse:

node=Node(item)if self._head ==None:

self._head=nodeelse:

be_node=self._headwhilebe_node._next:

be_node=be_node._next

be_node._next=node

self.length+= 1

#插入數據

definsert(self,index,item):ifself.isEmpty():print('this chain table is empty')return

if index<0 or index >=self.length:print("error: out of index")returnin_node=Node(item)

node=self._head

count= 1

whileTrue:

node=node._next

count+= 1

if count ==index:

next_node=node._next

node._next=in_node

in_node._next=next_node

self.length+= 1

return

#node = s

#刪除數據

defdelete(self,index):ifself.isEmpty():print('this chain table is empty')return

if index<0 or index >=self.length:print("error: out of index")return

#if index == 0

#self._head = None

else:

node=self._head

count=0whileTrue:

count+= 1

if index ==count:

node._next=node._next._nextbreaknode=node._next

self.length-= 1

def __repr__(self):ifself.isEmpty():print("the chain table is empty")returnnlist= ""node=self._headwhilenode:

nlist+= node._item +''node=node._nextreturnnlistif __name__ == '__main__':

chain=Chain()

chain.append('A')

chain.append('B')

chain.append('C')

chain.append('D')

chain.append('E')

chain.append('F')

chain.append('G')

chain.insert(4,'p')

chain.delete(3)print(chain,chain._head._item,chain.length)

總結

以上是生活随笔為你收集整理的python3 链表_python3实现链表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。