欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年05月25日 11:35:24   作者:python-行者  
這篇文章主要為大家詳細介紹了python雙向循環(huán)鏈表實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

使用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ù)
? ? ? ? 實例化游標,遍歷鏈表,使用計數(shù)器自增一
? ? ? ? 空鏈表

? ? ? ? """
? ? ? ? # 實例化游標
? ? ? ? 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):
? ? ? ? """
? ? ? ? 遍歷鏈表
? ? ? ? 實例化游標,遍歷鏈表,每次輸出節(jié)點的數(shù)據(jù)
? ? ? ? 空鏈表
? ? ? ? 只有頭節(jié)點
? ? ? ? """
? ? ? ? # 實例化游標
? ? ? ? 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)
? ? ? ? # 實例化游標
? ? ? ? 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é)點,實例化游標,指向尾部節(jié)點,修改指向
? ? ? ? 鏈表為空
? ? ? ? """
? ? ? ? # 實例化節(jié)點
? ? ? ? node = Node(item)
? ? ? ? # 實例化游標
? ? ? ? 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é)點, 實例化游標
? ? ? ? 移動游標到索引位置,更改指向
? ? ? ? 輸入索引大小判斷
? ? ? ? 鏈表是否為空
? ? ? ? """
? ? ? ? # 實例化節(jié)點
? ? ? ? node = Node(item)
? ? ? ? # 實例化游標
? ? ? ? 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ù)
? ? ? ? 實例化游標,遍歷鏈表,查找有沒有改數(shù)據(jù)
? ? ? ? 有,對改數(shù)據(jù)兩側(cè)的節(jié)點進行指向修改
? ? ? ? """
? ? ? ? # 實例化游標
? ? ? ? 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ù)是否存在
? ? ? ? 實例化游標
? ? ? ? 遍歷所有的節(jié)點。每個節(jié)點中判斷數(shù)據(jù)是否相等,相等,返回True
? ? ? ? """
? ? ? ? # 實例化游標
? ? ? ? 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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python時間序列的實現(xiàn)

    Python時間序列的實現(xiàn)

    本文主要介紹了Python時間序列的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python實現(xiàn)獲取域名所用服務(wù)器的真實IP

    Python實現(xiàn)獲取域名所用服務(wù)器的真實IP

    本文是給大家分享的使用python獲取到域名所在服務(wù)器的真實IP,原因是現(xiàn)在很多的網(wǎng)站都使用了CDN,大家很難直接查到域名的服務(wù)器的IP,本文是使用了一個巧妙的方法,詳情請仔細看看下文吧
    2015-10-10
  • python中使用matplotlib繪制熱力圖

    python中使用matplotlib繪制熱力圖

    熱力圖,是一種通過對色塊著色來顯示數(shù)據(jù)的統(tǒng)計圖表,它通過使用顏色編碼來表示數(shù)據(jù)的值,并在二維平面上呈現(xiàn)出來,本文就給大家介紹一下python使用matplotlib繪制熱力圖的方法,需要的朋友可以參考下
    2023-08-08
  • Python中深淺拷貝的區(qū)別詳細分析

    Python中深淺拷貝的區(qū)別詳細分析

    深拷貝和淺拷貝都是對原對象的拷貝,都會生成一個看起來相同的對象,下面這篇文章主要給大家介紹了關(guān)于Python中深淺拷貝的區(qū)別的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-06-06
  • Python強化練習(xí)之Tensorflow2 opp算法實現(xiàn)月球登陸器

    Python強化練習(xí)之Tensorflow2 opp算法實現(xiàn)月球登陸器

    在面向?qū)ο蟪霈F(xiàn)之前,我們采用的開發(fā)方法都是面向過程的編程(OPP)。面向過程的編程中最常用的一個分析方法是“功能分解”。我們會把用戶需求先分解成模塊,然后把模塊分解成大的功能,再把大的功能分解成小的功能,整個需求就是按照這樣的方式,最終分解成一個一個的函數(shù)
    2021-10-10
  • kaggle數(shù)據(jù)分析家庭電力消耗過程詳解

    kaggle數(shù)據(jù)分析家庭電力消耗過程詳解

    這篇文章主要為大家介紹了kaggle數(shù)據(jù)分析家庭電力消耗示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Django User 模塊之 AbstractUser 擴展詳解

    Django User 模塊之 AbstractUser 擴展詳解

    這篇文章主要介紹了Django User 模塊之 AbstractUser 擴展詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • VSCode配合pipenv搞定虛擬環(huán)境的實現(xiàn)方法

    VSCode配合pipenv搞定虛擬環(huán)境的實現(xiàn)方法

    這篇文章主要介紹了VSCode配合pipenv搞定虛擬環(huán)境的實現(xiàn)方法,文中通過圖文教程介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • python開發(fā)利器之ulipad的使用實踐

    python開發(fā)利器之ulipad的使用實踐

    Ulipad是一個國人limodou編寫的專業(yè)Python編輯器,它基于wxpython開發(fā)的GUI(圖形化界面)。下面這篇文章主要介紹了python開發(fā)利器之ulipad的使用實踐,文中介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-03-03
  • Python獲取網(wǎng)段內(nèi)ping通IP的方法

    Python獲取網(wǎng)段內(nèi)ping通IP的方法

    今天小編就為大家分享一篇Python獲取網(wǎng)段內(nèi)ping通IP的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01

最新評論