最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python雙向鏈表實例詳解

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

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

雙向鏈表: 指的是講數(shù)據(jù)鏈接在一起,每個數(shù)據(jù)是一個節(jié)點,每一個節(jié)點都有一個數(shù)據(jù)區(qū),兩個鏈接區(qū),分別鏈接上一個節(jié)點和下一個節(jié)點
數(shù)據(jù)區(qū): 存放數(shù)據(jù)的地方

prev: 鏈接上一個節(jié)點
next: 鏈接下一個節(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):
? ? ? ? """
? ? ? ? 初始化一個頭結(jié)點
? ? ? ? 默認(rèn)為空
? ? ? ? """
? ? ? ? self.head = None

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

? ? # 2. 鏈表的長度
? ? def length(self):
? ? ? ? """
? ? ? ? 返回節(jié)點的長度
? ? ? ? 實例化一個游標(biāo)
? ? ? ? 使用這個游標(biāo)遍歷所有的數(shù)據(jù)
? ? ? ? 使用一個計數(shù)器,遍歷一個數(shù)據(jù),自增一,最后輸出計數(shù)器
? ? ? ? """
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? # 技術(shù)器
? ? ? ? # 如果鏈表為空,不會進(jìn)入循環(huán),直接輸出 0
? ? ? ? count = 0
? ? ? ? # 遍歷所有的數(shù)據(jù)
? ? ? ? while cur != None:
? ? ? ? ? ? count +=1
? ? ? ? ? ? cur = cur.next
? ? ? ? return count

? ? # 3. 遍歷鏈表
? ? def treval(self):
? ? ? ? """
? ? ? ? 遍歷鏈表,獲取所有的數(shù)據(jù)
? ? ? ? 使用游標(biāo),遍歷整個鏈表,每次輸出cur.item 的值
? ? ? ? 注意鏈表為空的情況,
? ? ? ? """
? ? ? ? # 實例化一個游標(biāo)
? ? ? ? cur = self.head
? ? ? ? # 遍歷鏈表
? ? ? ? while cur != None:
? ? ? ? ? ? print(cur.item, end=' ')
? ? ? ? ? ? cur = cur.next
? ? ? ? print()
? ? # 4. 鏈表頭部添加元素
? ? def add(self, item):
? ? ? ? """
? ? ? ? 頭部添加數(shù)據(jù)
? ? ? ? 分析:
? ? ? ? 1、頭部添加數(shù)據(jù),鏈表為空時: self.head 指向node
? ? ? ? 2、鏈表不為空時: 先將node.next = self.head.next, 再講 self.head = node
? ? ? ? """
? ? ? ? # 實例化節(jié)點
? ? ? ? node = Node(item)
? ? ? ? # 添加數(shù)據(jù)
? ? ? ? # 判斷鏈表是否為空
? ? ? ? if self.is_empty():
? ? ? ? ? ? # 為空,直接將self.head 指向node
? ? ? ? ? ? self.head=node
? ? ? ? else:
? ? ? ? ? ? # 不為空,先將noede
? ? ? ? ? ? node.next = self.head
? ? ? ? ? ? self.head.prev=node
? ? ? ? ? ? self.head=node

? ? # 5. 鏈表尾部添加元素
? ? def append(self,item):
? ? ? ? """
? ? ? ? 尾部添加數(shù)據(jù)
? ? ? ? 分析:
? ? ? ? 要先將指針指向尾部的節(jié)點
? ? ? ? 最后的節(jié)點的 cur.next = node, node.prev = cur
? ? ? ? """
? ? ? ? # 實例化節(jié)點
? ? ? ? node = Node(item)
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? # 移動游標(biāo)到最后一個節(jié)點
? ? ? ? # 如果鏈表為空
? ? ? ? # 直接使用頭插法
? ? ? ? if self.is_empty():
? ? ? ? ? ? self.add(item)
? ? ? ? else:
? ? ? ? ? ? while cur.next != None:
? ? ? ? ? ? ? ? # cur.next 不為空,則進(jìn)入循環(huán), 上次循環(huán),是進(jìn)入上上個節(jié)點,但 將cur ?= cur.next,就指向了最后一個節(jié)點
? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? ? ? node.prev = cur
? ? ? ? ? ? cur.next = node

? ? # 6. 鏈表指定位置添加元素
? ? def insert(self, index, item):
? ? ? ? """
? ? ? ? 指定位置添加數(shù)據(jù)
? ? ? ? 分析
? ? ? ? 單鏈表中需要實例化兩個有游標(biāo)
? ? ? ? 雙向鏈表,直接向指針指向索引的位置
? ? ? ? 將這個位置節(jié)點的 cur.
? ? ? ? """
? ? ? ? # 實例化節(jié)點
? ? ? ? node = Node(item)
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? # 起始位置
? ? ? ? count = 0
? ? ? ? if index<=0:
? ? ? ? ? ? # 使用頭插法
? ? ? ? ? ? self.add(item)
? ? ? ? elif index > (self.length()-1):
? ? ? ? ? ? self.append(item)
? ? ? ? else:
? ? ? ? ? ? # 移動游標(biāo)
? ? ? ? ? ? while count < index:
? ? ? ? ? ? ? ? # 增加一
? ? ? ? ? ? ? ? count += 1
? ? ? ? ? ? ? ? # 移動游標(biāo)到索引位置
? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? ? ? node.next = cur
? ? ? ? ? ? node.prev = cur.prev
? ? ? ? ? ? cur.prev.next = node
? ? ? ? ? ? cur.prev = node


? ? # 7. 鏈表刪除節(jié)點
? ? def remove(self,item):
? ? ? ? """
? ? ? ? 刪除指定的節(jié)點
? ? ? ? 首先實例化節(jié)點,遍歷鏈表,找到該節(jié)點,刪除該節(jié)點
? ? ? ? """
? ? ? ? # 實例化游標(biāo)
? ? ? ? cur = self.head
? ? ? ? # 遍歷鏈表,找到該節(jié)點
? ? ? ? while cur != None:
? ? ? ? ? ? if cur.item == item:
? ? ? ? ? ? ? ? if self.head == cur:
? ? ? ? ? ? ? ? ? ? self.head = cur.next
? ? ? ? ? ? ? ? ? ? if cur.next:
? ? ? ? ? ? ? ? ? ? ? ? cur.next.prev = None
? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? cur.prev.next = cur.next
? ? ? ? ? ? ? ? ? ? # 如果有下個節(jié)點,防止最后一個節(jié)點
? ? ? ? ? ? ? ? ? ? if cur.next:
? ? ? ? ? ? ? ? ? ? ? ? cur.next.prev = cur.prev
? ? ? ? ? ? ? ? ? ? pass
? ? ? ? ? ? ? ? break
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? cur = cur.next

? ? # # 8. 查找節(jié)點是否存在
? ? def search(self, item):
? ? ? ? """
? ? ? ? 查看節(jié)點是否存在
? ? ? ? 分析
? ? ? ? 遍歷鏈表,查看每一個節(jié)點的數(shù)據(jù)區(qū)
? ? ? ? 空鏈表
? ? ? ? 只有頭節(jié)點
? ? ? ? 只有尾節(jié)點
? ? ? ? """
? ? ? ? # 實例化一個游標(biāo)
? ? ? ? cur = self.head

? ? ? ? # 遍歷鏈表
? ? ? ? while cur != None:
? ? ? ? ? ? if cur.item == item:
? ? ? ? ? ? ? ? return True
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? cur = cur.next
? ? ? ? return False

測試運行

# 程序的入口
if __name__ == "__main__":
? ? s = Linklist()
? ? print(s.is_empty()) ?# ?True
? ? s.append(100)?
? ? s.append(200)
? ? s.append(300)
? ? s.add(6)
? ? s.insert(1, 10)
? ? s.search(6)
? ? s.treval() ? # 6 10 100 200 300?
? ? s.remove(100)
? ? s.treval() ? # 6 10 200 300?
? ? pass

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

相關(guān)文章

最新評論

葵青区| 龙川县| 平乡县| 称多县| 肇庆市| 德清县| 称多县| 富源县| 东乌珠穆沁旗| 庆安县| 清远市| 廉江市| 衡水市| 沁水县| 南开区| 钟山县| 贵德县| 桦甸市| 美姑县| 蒙山县| 庆安县| 肇庆市| 孝义市| 日喀则市| 阿巴嘎旗| 收藏| 南平市| 宜兰市| 洪洞县| 库尔勒市| 阿荣旗| 民权县| 新化县| 泸溪县| 福鼎市| 灌南县| 绍兴市| 布尔津县| 舒兰市| 南靖县| 贡觉县|