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

歡迎訪問 生活随笔!

生活随笔

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

python

算法—详细讲解单向循环链表的实现(python)

發布時間:2025/3/21 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法—详细讲解单向循环链表的实现(python) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

單向循環列表如圖所示

單向循環鏈表的實現

一、往鏈表頭部添加一個節點值為4
1、新增一個節點node=Node(4)

新建一個節點

node=Node(data)if self.is_empty():#即是頭節點也是尾節點self.head=node#node.next=self.headelse:# 先讓node指向當前鏈表中的頭節點node.next=self.head# 再讓鏈表的尾節點的next指向nodecur=self.headwhile cur.next!=self.head:cur=cur.nextcur.next=node#再讓鏈表的head指向當前node節點self.head=node#添加節點之后,鏈表的長度加1self._length+=1


二、往鏈表尾部添加一個節點

新建一個節點node,值為data

node=Node(data)# 找到鏈表的尾節點# 思路:從頭節點開始遍歷鏈表中的所有節點]# 每次判斷當前節點的next是否為空# 為空這說明當前節點就是尾節點# 不為空,通過當前節點的next方法去訪問下一個節點,if self.head:cur = self.headwhile cur.next != self.head:cur = cur.nextcur.next = node #原本的尾節點指向新建的節點else:self.head=nodenode.next=self.head #新的尾節點指向當前的頭節點# 讓當前的尾節點的指針指向新建的節點node# 鏈表的長度加1self._length+=1


三、往鏈表中插入一個節點

if pos<=0:self.add(data)elif pos>self._length:self.append(data)else:# 1新建一個節點node = Node(data)# 2找到鏈表中索引為pos-1的節點cur=self.headwhile pos-1:cur=cur.nextpos-=1#到這里之后,cur指向的是索引為pos-1的節點# node.next=prev_node.next# 3讓node的next指向索引為pos的節點node.next=cur.next# 4讓索引為pos-1的節點的next指向nodecur.next=node# 5鏈表的長度加1self._length+=1


四、刪除鏈表中第一個值為data的節點
1、刪除的值為頭部節點

2、刪除的值為非頭節點

a、先要判斷鏈表是否為空,為空那必然沒有值為data的節點

#判斷鏈表是否為空,為空那必然沒有值為data的節點cur=self.headflag=True # 標志位的作用:讓第一次循環能進入prev=Nonewhile cur!=self.head or flag:if cur.data==data:flag=False #讓循環繼續的條件就是cur!=self.head#如果前驅節點為空,說明我們要刪除的節點是第一個節點if not prev:#找到尾節點last_node=self.headwhile last_node.next!=self.head:last_node=last_node.next#讓尾節點的next指向頭節點last_node.next=self.head.nextself.head=cur.next #self.head.nextelse:prev.next = cur.nextself._length-=1return 0prev=curcur = cur.nextreturn -1

五、修改鏈表中指定位置節點的值

# 修改鏈表中指定位置節點的值if 0<pos<self._length:cur = self.headwhile pos:cur = cur.nextpos -= 1cur.data=dataelse:print('輸入的范圍不符合要求')

六、查找鏈表中是否有節點的值為data

if self.is_empty():return Falsecur = self.headflag=Truewhile cur!=self.head or flag:flag=Falseif cur.data == data:return Truecur = cur.nextreturn False

代碼塊:

class Node:def __init__(self, data, _next=None):self.data = data # 數據域self.next = _next # 指針域class SingleCycleLinkList:def __init__(self):self.head = None # 鏈表的的頭節點self._length = 0 # 鏈表的長度,鏈表的元素個數#self.tail=None # 鏈表的尾節點def is_empty(self):# 判斷鏈表的是否為空return self._length==0def length(self):# 返回鏈表的長度return self._lengthdef nodes_list(self):# 返回鏈表中的所有節點的值組成的列表res=[]#判斷鏈表是否為空if self.is_empty():return reselse:res.append(self.head.data)cur=self.head.nextwhile cur!=self.head:res.append(cur.data)cur=cur.nextreturn resdef add(self, data):# 往鏈表的頭部添加一個節點,值為data# 新建一個節點node=Node(data)if self.is_empty():self.head=nodenode.next=self.headelse:# 先讓node指向當前鏈表中的頭節點node.next=self.head# 再讓鏈表的尾節點的next指向nodecur=self.headwhile cur.next!=self.head:cur=cur.nextcur.next=node#再讓鏈表的head指向當前node節點self.head=node#添加節點之后,鏈表的長度加1self._length+=1def append(self, data):# 往鏈表的尾部添加一個節點,值為data# 新建一個節點node,值為datanode=Node(data)# 找到鏈表的尾節點# 思路:從頭節點開始遍歷鏈表中的所有節點# 每次判斷當前節點的next是否為空# 為空這說明當前節點就是尾節點# 不為空,通過當前節點的next方法去訪問下一個節點,if self.head:cur = self.headwhile cur.next != self.head:cur = cur.nextcur.next = node #原本的尾節點指向新建的節點else:self.head=nodenode.next=self.head #新的尾節點指向當前的頭節點# 讓當前的尾節點的指針指向新建的節點node# 鏈表的長度加1self._length+=1def insert(self, pos, data):# 往鏈表的指定位置插入一個節點,值為dataif pos<=0:self.add(data)elif pos>self._length:self.append(data)else:# 1新建一個節點node = Node(data)# 2找到鏈表中索引為pos-1的節點cur=self.headwhile pos-1:cur=cur.nextpos-=1#到這里之后,cur指向的是索引為pos-1的節點# node.next=prev_node.next# 3讓node的next指向索引為pos的節點node.next=cur.next# 4讓索引為pos-1的節點的next指向nodecur.next=node# 5鏈表的長度加1self._length+=1def remove(self, data):# 刪除鏈表中第一個值為data的節點# 判斷鏈表是否為空,為空那必然沒有值為data的節點cur=self.headflag=True # 標志位的作用:讓第一次循環能進入prev=Nonewhile cur!=self.head or flag:if cur.data==data:flag=False #讓循環繼續的條件就是cur!=self.head#如果前驅節點為空,說明我們要刪除的節點是第一個節點if not prev:#找到尾節點last_node=self.headwhile last_node.next!=self.head:last_node=last_node.next#讓尾節點的next指向頭節點last_node.next=self.head.nextself.head=cur.next #self.head.nextelse:prev.next = cur.nextself._length-=1return 0prev=curcur = cur.nextreturn -1def modify(self, pos,data):# 修改鏈表中指定位置節點的值if 0<pos<self._length:cur = self.headwhile pos:cur = cur.nextpos -= 1cur.data=dataelse:print('輸入的范圍不符合要求')def search(self, data):# 查找鏈表中是否有節點的值為dataif self.is_empty():return Falsecur = self.headflag=Truewhile cur!=self.head or flag:flag=Falseif cur.data == data:return Truecur = cur.nextreturn Falseif __name__ == '__main__':l1=SingleCycleLinkList() #新建一個鏈表類# print(l1.head,l1.length())# l1.add(1)# print(l1.head.data, l1.length())# print(l1.nodes_list())# l1.add(5)# print(l1.head.data, l1.length())# print(l1.nodes_list())## print(l1.head.data,l1.head.next.data,l1.head.next.next.data) #驗證是循環的# l1.append(2)# print(l1.head.data, l1.length())# print(l1.nodes_list())# print(l1.head.data, l1.head.next.data, l1.head.next.next.data,l1.head.next.next.next.data) # 驗證是循環的# l1.insert(1,7)# print(l1.nodes_list())# l1.insert(-1, 6)# print(l1.nodes_list())# print(l1.is_empty())# l1.remove(7)# print(l1.nodes_list())# l1.modify(2,8)# print(l1.nodes_list())# print(l1.search(8))l1.add(1)l1.add(2)l1.add(3)l1.add(4)l1.add(5)print(l1.nodes_list())l1.remove(5)print(l1.nodes_list())l1.modify(1,7)print(l1.nodes_list())print(l1.search(8)) 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的算法—详细讲解单向循环链表的实现(python)的全部內容,希望文章能夠幫你解決所遇到的問題。

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