日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

python雙向循環(huán)鏈表實例詳解_python

作者:python-行者 ? 更新時間: 2022-07-24 編程語言

使用python實現(xiàn)雙向循環(huán)鏈表,供大家參考,具體內(nèi)容如下

雙向循環(huán)鏈表: 將所有的數(shù)據(jù)存放到節(jié)點中,每一個節(jié)點相連接,首尾鏈接,
每一個節(jié)點中有一個數(shù)據(jù)存儲區(qū),和兩個鏈接區(qū),一個鏈接前一個節(jié)點,一個鏈接下一個節(jié)點

雙向鏈表操作

1、鏈表是否為空
2、鏈表的長度
3、遍歷鏈表
4、鏈表頭部添加元素
5、鏈表尾部添加元素
6、鏈表指定位置添加元素
7、鏈表刪除節(jié)點
8、查找節(jié)點是否存在

代碼實現(xiàn)

# Functions ?函數(shù)聲明
class Node():
? ? """實例化節(jié)點類"""
? ? def __init__(self, item):
? ? ? ? self.item = item
? ? ? ? self.prev = None
? ? ? ? self.next = None

class Linklist():
? ? """
? ? 存放節(jié)點類
? ? """
? ? def __init__(self):
? ? ? ? self.head = None

? ? # 1. 鏈表是否為空
? ? def is_empty(self):
? ? ? ? return self.head == None

? ? # 2. 鏈表的長度
? ? def length(self):
? ? ? ? """
? ? ? ? 返回鏈表中所有數(shù)據(jù)的個數(shù)
? ? ? ? 實例化游標(biāo),遍歷鏈表,使用計數(shù)器自增一
? ? ? ? 空鏈表

? ? ? ? """
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? # 判斷是否為空
? ? ? ? if self.is_empty():
? ? ? ? ? ? return 0
? ? ? ? else:
? ? ? ? ? ? # 不為空
? ? ? ? ? ? # 定義計數(shù)
? ? ? ? ? ? count = 1
? ? ? ? ? ? while cur.next != self.head:
? ? ? ? ? ? ? ? count+=1
? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? ? ? return count
? ? ? ? ? ? pass

? ? # 3. 遍歷鏈表
? ? def travel(self):
? ? ? ? """
? ? ? ? 遍歷鏈表
? ? ? ? 實例化游標(biāo),遍歷鏈表,每次輸出節(jié)點的數(shù)據(jù)
? ? ? ? 空鏈表
? ? ? ? 只有頭節(jié)點
? ? ? ? """
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? # 判斷是否為空
? ? ? ? if self.is_empty():
? ? ? ? ? ? return None
? ? ? ? else:
? ? ? ? ? ? # 不為空的情況
? ? ? ? ? ? while cur.next != self.head:
? ? ? ? ? ? ? ? print(cur.item, end=' ')
? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? ? ? print(cur.item)
? ? ? ? ? ? pass

? ? # 4. 鏈表頭部添加元素
? ? def add(self, item):
? ? ? ? """
? ? ? ? 頭節(jié)點添加
? ? ? ? 實例化節(jié)點,
? ? ? ? """
? ? ? ? # 實例化節(jié)點
? ? ? ? node = Node(item)
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? # 判斷是否為空
? ? ? ? if self.is_empty():
? ? ? ? ? ? node.next = node
? ? ? ? ? ? node.prev = node
? ? ? ? ? ? self.head = node
? ? ? ? else:
? ? ? ? ? ? # 鏈表不為空的情況
? ? ? ? ? ? # 只有一個節(jié)點的情況
? ? ? ? ? ? # node.next = self.head
? ? ? ? ? ? node.next = cur
? ? ? ? ? ? node.prev = cur
? ? ? ? ? ? if cur.next == self.head:
? ? ? ? ? ? ? ? # print(cur.item)
? ? ? ? ? ? ? ? cur.prev = node
? ? ? ? ? ? ? ? cur.next = node
? ? ? ? ? ? ? ? self.head = node
? ? ? ? ? ? elif cur.next != self.head:
? ? ? ? ? ? ? ? pro = self.head
? ? ? ? ? ? ? ? while cur.next != self.head:
? ? ? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? ? ? ? ? pro.prev = node
? ? ? ? ? ? ? ? cur.next = node
? ? ? ? ? ? ? ? self.head = node
? ? ? ? ? ? ? ? pass

? ? # 5. 鏈表尾部添加元素
? ? def append(self, item):
? ? ? ? """
? ? ? ? 鏈表尾部添加數(shù)據(jù)
? ? ? ? 實例化節(jié)點,實例化游標(biāo),指向尾部節(jié)點,修改指向
? ? ? ? 鏈表為空
? ? ? ? """
? ? ? ? # 實例化節(jié)點
? ? ? ? node = Node(item)
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? if self.is_empty():
? ? ? ? ? ? self.add(item)
? ? ? ? else:
? ? ? ? ? ? # 不為空的情況
? ? ? ? ? ? # 指針指向最后一個節(jié)點
? ? ? ? ? ? self.head.prev = node
? ? ? ? ? ? node.next = self.head
? ? ? ? ? ? while cur.next != self.head:
? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? ? ? node.prev = cur
? ? ? ? ? ? cur.next = node
? ? ? ? ? ? pass

? ? # 6. 鏈表指定位置添加元素
? ? def insert(self, index, item):
? ? ? ? """
? ? ? ? 指定位置添加數(shù)據(jù)
? ? ? ? 實例化節(jié)點, 實例化游標(biāo)
? ? ? ? 移動游標(biāo)到索引位置,更改指向
? ? ? ? 輸入索引大小判斷
? ? ? ? 鏈表是否為空
? ? ? ? """
? ? ? ? # 實例化節(jié)點
? ? ? ? node = Node(item)
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? if index <= 0:
? ? ? ? ? ? self.add(item)
? ? ? ? elif index > (self.length()-1):
? ? ? ? ? ? self.append(item)
? ? ? ? else:
? ? ? ? ? ? # 中間添加數(shù)據(jù)
? ? ? ? ? ? # 聲明計數(shù)
? ? ? ? ? ? count = 0
? ? ? ? ? ? print(index)
? ? ? ? ? ? while count < index-1:
? ? ? ? ? ? ? ? count+=1
? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? ? ? # print(cur.item)
? ? ? ? ? ? node.next = cur.next
? ? ? ? ? ? node.prev = cur
? ? ? ? ? ? cur.next.prev = node
? ? ? ? ? ? cur.next = node
? ? ? ? ? ? pass

? ? # 7. 鏈表刪除節(jié)點
? ? def remove(self, item):
? ? ? ? """
? ? ? ? 刪除數(shù)據(jù)
? ? ? ? 實例化游標(biāo),遍歷鏈表,查找有沒有改數(shù)據(jù)
? ? ? ? 有,對改數(shù)據(jù)兩側(cè)的節(jié)點進(jìn)行指向修改
? ? ? ? """
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? # 判斷是否為空
? ? ? ? if self.is_empty():
? ? ? ? ? ? return None
? ? ? ? else:
? ? ? ? ? ? # 不為空的情況下
? ? ? ? ? ? # 如果刪除的是頭節(jié)點
? ? ? ? ? ? if cur.item == item:
? ? ? ? ? ? ? ? # 如果只有一個頭節(jié)點
? ? ? ? ? ? ? ? if cur.next == self.head:
? ? ? ? ? ? ? ? ? ?self.head = None
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? # self.head = cur.next
? ? ? ? ? ? ? ? ? ? pro = cur.next
? ? ? ? ? ? ? ? ? ? while cur.next != self.head:
? ? ? ? ? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? ? ? ? ? ? ? cur.next = pro
? ? ? ? ? ? ? ? ? ? pro.prev = cur
? ? ? ? ? ? ? ? ? ? self.head = pro
? ? ? ? ? ? ? ? ? ? pass
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? while cur.next != self.head:
? ? ? ? ? ? ? ? ? ? if cur.item == item:
? ? ? ? ? ? ? ? ? ? ? ? # print(cur.item)
? ? ? ? ? ? ? ? ? ? ? ? pro = cur.prev
? ? ? ? ? ? ? ? ? ? ? ? nex = cur.next
? ? ? ? ? ? ? ? ? ? ? ? pro.next = cur.next
? ? ? ? ? ? ? ? ? ? ? ? nex.prev = pro
? ? ? ? ? ? ? ? ? ? ? ? return True
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? ? ? ? ? # 如果是最后一個節(jié)點
? ? ? ? ? ? ? ? if cur.item == item:
? ? ? ? ? ? ? ? ? ? cur.prev.next = self.head
? ? ? ? ? ? ? ? ? ? self.head.prev = cur.prev

? ? # 8. 查找節(jié)點是否存在
? ? def search(self, item):
? ? ? ? """
? ? ? ? 查詢指定的數(shù)據(jù)是否存在
? ? ? ? 實例化游標(biāo)
? ? ? ? 遍歷所有的節(jié)點。每個節(jié)點中判斷數(shù)據(jù)是否相等,相等,返回True
? ? ? ? """
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? # 判斷是否為空
? ? ? ? if self.is_empty():
? ? ? ? ? ? return None
? ? ? ? else:
? ? ? ? ? ? # 不為空的情況
? ? ? ? ? ? # 遍歷所有的節(jié)點
? ? ? ? ? ? while cur.next != self.head:
? ? ? ? ? ? ? ? if cur.item == item:
? ? ? ? ? ? ? ? ? ? return True
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? ? ? if cur.item == item:
? ? ? ? ? ? ? ? return True
? ? ? ? ? ? pass

測試運行

# 程序的入口
if __name__ == "__main__":
? ? a = Linklist()
? ? a .add(400)
? ? a .add(300)
? ? a .add(200)
? ? a .add(100)
? ? a.append(10)
? ? a.append(11)
? ? a.add(1)
? ? a.insert(30, 12) # 1 100 200 300 400 10 11 12
? ? a.remove(1) ? ?# 100 200 300 400 10 11 12
? ? a.remove(12) ? # 100 200 300 400 10 11
? ? a.remove(400) ?# # 100 200 300 ?10 11
? ? a.remove(4000)
? ? print(a.search(100)) ?# True
? ? print(a.search(11)) ? # True
? ? print(a.search(111)) ?# None
? ? print(a.is_empty()) ? # False
? ? a.travel() ? ? ? ? ? ?# 100 200 300 10 11
? ? print(a.length()) ? ? # 5
? ? pass

原文鏈接:https://blog.csdn.net/Dhaihaihai/article/details/111304544

欄目分類
最近更新