網(wǎng)站首頁 編程語言 正文
使用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
相關(guān)推薦
- 2022-04-26 python?moviepy?的用法入門篇_python
- 2022-05-31 Python中的列表及其操作方法_python
- 2022-08-02 Android中TextView自動適配文本大小的幾種解決方案_Android
- 2022-06-16 golang?gorm錯誤處理事務(wù)以及日志用法示例_Golang
- 2022-09-06 關(guān)于react+antd樣式不生效問題的解決方式_React
- 2023-04-24 numpy?產(chǎn)生隨機(jī)數(shù)的幾種方法_python
- 2022-11-20 Python必知必會之os模塊實例詳解_python
- 2022-07-21 配置nacos持久化
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支