python雙向循環(huán)鏈表實(shí)例詳解
使用python實(shí)現(xiàn)雙向循環(huán)鏈表,供大家參考,具體內(nèi)容如下
雙向循環(huán)鏈表: 將所有的數(shù)據(jù)存放到節(jié)點(diǎn)中,每一個(gè)節(jié)點(diǎn)相連接,首尾鏈接,
每一個(gè)節(jié)點(diǎn)中有一個(gè)數(shù)據(jù)存儲(chǔ)區(qū),和兩個(gè)鏈接區(qū),一個(gè)鏈接前一個(gè)節(jié)點(diǎn),一個(gè)鏈接下一個(gè)節(jié)點(diǎn)
雙向鏈表操作
1、鏈表是否為空
2、鏈表的長度
3、遍歷鏈表
4、鏈表頭部添加元素
5、鏈表尾部添加元素
6、鏈表指定位置添加元素
7、鏈表刪除節(jié)點(diǎn)
8、查找節(jié)點(diǎn)是否存在
代碼實(shí)現(xiàn)
# Functions ?函數(shù)聲明 class Node(): ? ? """實(shí)例化節(jié)點(diǎn)類""" ? ? def __init__(self, item): ? ? ? ? self.item = item ? ? ? ? self.prev = None ? ? ? ? self.next = None class Linklist(): ? ? """ ? ? 存放節(jié)點(diǎn)類 ? ? """ ? ? def __init__(self): ? ? ? ? self.head = None ? ? # 1. 鏈表是否為空 ? ? def is_empty(self): ? ? ? ? return self.head == None ? ? # 2. 鏈表的長度 ? ? def length(self): ? ? ? ? """ ? ? ? ? 返回鏈表中所有數(shù)據(jù)的個(gè)數(shù) ? ? ? ? 實(shí)例化游標(biāo),遍歷鏈表,使用計(jì)數(shù)器自增一 ? ? ? ? 空鏈表 ? ? ? ? """ ? ? ? ? # 實(shí)例化游標(biāo) ? ? ? ? cur = self.head ? ? ? ? # 判斷是否為空 ? ? ? ? if self.is_empty(): ? ? ? ? ? ? return 0 ? ? ? ? else: ? ? ? ? ? ? # 不為空 ? ? ? ? ? ? # 定義計(jì)數(shù) ? ? ? ? ? ? count = 1 ? ? ? ? ? ? while cur.next != self.head: ? ? ? ? ? ? ? ? count+=1 ? ? ? ? ? ? ? ? cur = cur.next ? ? ? ? ? ? return count ? ? ? ? ? ? pass ? ? # 3. 遍歷鏈表 ? ? def travel(self): ? ? ? ? """ ? ? ? ? 遍歷鏈表 ? ? ? ? 實(shí)例化游標(biāo),遍歷鏈表,每次輸出節(jié)點(diǎn)的數(shù)據(jù) ? ? ? ? 空鏈表 ? ? ? ? 只有頭節(jié)點(diǎn) ? ? ? ? """ ? ? ? ? # 實(shí)例化游標(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é)點(diǎn)添加 ? ? ? ? 實(shí)例化節(jié)點(diǎn), ? ? ? ? """ ? ? ? ? # 實(shí)例化節(jié)點(diǎn) ? ? ? ? node = Node(item) ? ? ? ? # 實(shí)例化游標(biāo) ? ? ? ? cur = self.head ? ? ? ? # 判斷是否為空 ? ? ? ? if self.is_empty(): ? ? ? ? ? ? node.next = node ? ? ? ? ? ? node.prev = node ? ? ? ? ? ? self.head = node ? ? ? ? else: ? ? ? ? ? ? # 鏈表不為空的情況 ? ? ? ? ? ? # 只有一個(gè)節(jié)點(diǎn)的情況 ? ? ? ? ? ? # 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ù) ? ? ? ? 實(shí)例化節(jié)點(diǎn),實(shí)例化游標(biāo),指向尾部節(jié)點(diǎn),修改指向 ? ? ? ? 鏈表為空 ? ? ? ? """ ? ? ? ? # 實(shí)例化節(jié)點(diǎn) ? ? ? ? node = Node(item) ? ? ? ? # 實(shí)例化游標(biāo) ? ? ? ? cur = self.head ? ? ? ? if self.is_empty(): ? ? ? ? ? ? self.add(item) ? ? ? ? else: ? ? ? ? ? ? # 不為空的情況 ? ? ? ? ? ? # 指針指向最后一個(gè)節(jié)點(diǎn) ? ? ? ? ? ? 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ù) ? ? ? ? 實(shí)例化節(jié)點(diǎn), 實(shí)例化游標(biāo) ? ? ? ? 移動(dòng)游標(biāo)到索引位置,更改指向 ? ? ? ? 輸入索引大小判斷 ? ? ? ? 鏈表是否為空 ? ? ? ? """ ? ? ? ? # 實(shí)例化節(jié)點(diǎn) ? ? ? ? node = Node(item) ? ? ? ? # 實(shí)例化游標(biāo) ? ? ? ? cur = self.head ? ? ? ? if index <= 0: ? ? ? ? ? ? self.add(item) ? ? ? ? elif index > (self.length()-1): ? ? ? ? ? ? self.append(item) ? ? ? ? else: ? ? ? ? ? ? # 中間添加數(shù)據(jù) ? ? ? ? ? ? # 聲明計(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é)點(diǎn) ? ? def remove(self, item): ? ? ? ? """ ? ? ? ? 刪除數(shù)據(jù) ? ? ? ? 實(shí)例化游標(biāo),遍歷鏈表,查找有沒有改數(shù)據(jù) ? ? ? ? 有,對改數(shù)據(jù)兩側(cè)的節(jié)點(diǎn)進(jìn)行指向修改 ? ? ? ? """ ? ? ? ? # 實(shí)例化游標(biāo) ? ? ? ? cur = self.head ? ? ? ? # 判斷是否為空 ? ? ? ? if self.is_empty(): ? ? ? ? ? ? return None ? ? ? ? else: ? ? ? ? ? ? # 不為空的情況下 ? ? ? ? ? ? # 如果刪除的是頭節(jié)點(diǎn) ? ? ? ? ? ? if cur.item == item: ? ? ? ? ? ? ? ? # 如果只有一個(gè)頭節(jié)點(diǎn) ? ? ? ? ? ? ? ? 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 ? ? ? ? ? ? ? ? # 如果是最后一個(gè)節(jié)點(diǎn) ? ? ? ? ? ? ? ? if cur.item == item: ? ? ? ? ? ? ? ? ? ? cur.prev.next = self.head ? ? ? ? ? ? ? ? ? ? self.head.prev = cur.prev ? ? # 8. 查找節(jié)點(diǎn)是否存在 ? ? def search(self, item): ? ? ? ? """ ? ? ? ? 查詢指定的數(shù)據(jù)是否存在 ? ? ? ? 實(shí)例化游標(biāo) ? ? ? ? 遍歷所有的節(jié)點(diǎn)。每個(gè)節(jié)點(diǎn)中判斷數(shù)據(jù)是否相等,相等,返回True ? ? ? ? """ ? ? ? ? # 實(shí)例化游標(biāo) ? ? ? ? cur = self.head ? ? ? ? # 判斷是否為空 ? ? ? ? if self.is_empty(): ? ? ? ? ? ? return None ? ? ? ? else: ? ? ? ? ? ? # 不為空的情況 ? ? ? ? ? ? # 遍歷所有的節(jié)點(diǎn) ? ? ? ? ? ? while cur.next != self.head: ? ? ? ? ? ? ? ? if cur.item == item: ? ? ? ? ? ? ? ? ? ? return True ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? cur = cur.next ? ? ? ? ? ? if cur.item == item: ? ? ? ? ? ? ? ? return True ? ? ? ? ? ? pass
測試運(yùn)行
# 程序的入口 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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python單向循環(huán)鏈表實(shí)例詳解
- Python數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表詳解
- python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
- python/golang實(shí)現(xiàn)循環(huán)鏈表的示例代碼
- python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法示例
- Python雙向循環(huán)鏈表實(shí)現(xiàn)方法分析
- Python實(shí)現(xiàn)的單向循環(huán)鏈表功能示例
- Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法實(shí)例詳解【單鏈表、循環(huán)鏈表】
- python雙向鏈表實(shí)例詳解
- Python實(shí)現(xiàn)雙向鏈表基本操作
相關(guān)文章
Python實(shí)現(xiàn)獲取域名所用服務(wù)器的真實(shí)IP
本文是給大家分享的使用python獲取到域名所在服務(wù)器的真實(shí)IP,原因是現(xiàn)在很多的網(wǎng)站都使用了CDN,大家很難直接查到域名的服務(wù)器的IP,本文是使用了一個(gè)巧妙的方法,詳情請仔細(xì)看看下文吧2015-10-10Python強(qiáng)化練習(xí)之Tensorflow2 opp算法實(shí)現(xiàn)月球登陸器
在面向?qū)ο蟪霈F(xiàn)之前,我們采用的開發(fā)方法都是面向過程的編程(OPP)。面向過程的編程中最常用的一個(gè)分析方法是“功能分解”。我們會(huì)把用戶需求先分解成模塊,然后把模塊分解成大的功能,再把大的功能分解成小的功能,整個(gè)需求就是按照這樣的方式,最終分解成一個(gè)一個(gè)的函數(shù)2021-10-10Django User 模塊之 AbstractUser 擴(kuò)展詳解
這篇文章主要介紹了Django User 模塊之 AbstractUser 擴(kuò)展詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03VSCode配合pipenv搞定虛擬環(huán)境的實(shí)現(xiàn)方法
這篇文章主要介紹了VSCode配合pipenv搞定虛擬環(huán)境的實(shí)現(xiàn)方法,文中通過圖文教程介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05python開發(fā)利器之ulipad的使用實(shí)踐
Ulipad是一個(gè)國人limodou編寫的專業(yè)Python編輯器,它基于wxpython開發(fā)的GUI(圖形化界面)。下面這篇文章主要介紹了python開發(fā)利器之ulipad的使用實(shí)踐,文中介紹的非常詳細(xì),對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03Python獲取網(wǎng)段內(nèi)ping通IP的方法
今天小編就為大家分享一篇Python獲取網(wǎng)段內(nèi)ping通IP的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01