日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python单向链表和双向链表的图示代码说明

發(fā)布時間:2023/12/20 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python单向链表和双向链表的图示代码说明 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

圖示說明:

單向鏈表:

insert、 remove、 update、pop方法

class Node:def __init__(self, data):self.data = dataself.next = Nonedef __str__(self):return str(self.data)# 通過單鏈表構(gòu)建一個list的結(jié)構(gòu): 添加 刪除 插入 查找 獲取長度 判斷是否為空... # list1 = [] list1.append(5) [5,] slist = SingleList() slist.append(5) class SingleList:def __init__(self, node=None):self._head = nodedef isEmpty(self):return self._head == Nonedef append(self, item):# 尾部添加node = Node(item)if self.isEmpty():self._head = nodeelse:cur = self._headwhile cur.next != None:cur = cur.nextcur.next = node# 求長度def len(self):cur = self._headcount = 0while cur != None:count += 1cur = cur.nextreturn count# 遍歷def print_all(self):cur = self._headwhile cur != None:print(cur)cur = cur.nextdef pop(self, index):if index < 0 or index >= self.len():raise IndexError('index Error')if index == 0:self._head = self._head.nextelse:cur = self._head# 找到當(dāng)前下標(biāo)的前一個元素while index - 1:cur = cur.nextindex -= 1# 修改的next的指向位置cur.next = cur.next.nextdef insert(self, index, item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(item, Node):raise TypeError('不能是Node類型')else:node = Node(item)if index == 0:node.next = self._headself._head = nodeelse:cur = self._headwhile index - 1:cur = cur.nextindex -= 1node.next = cur.nextcur.next = nodedef update(self, index, new_item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(new_item, Node):raise TypeError('不能是Node類型')else:node = Node(new_item)if index == 0:node.next = self._head.nextself._head = nodeelse:cur = self._headnode.next = cur.next.nextcur.next = nodedef remove(self, item):if isinstance(item, Node):raise TypeError('不能是Node類型')else:node = Node(item)cur = self._headwhile cur == node:cur = cur.nextcur.next = cur.next.nextif __name__ == '__main__':slist = SingleList()print(slist.isEmpty()) # Trueprint(slist.len())slist.append(5)print(slist.isEmpty()) # Falseprint(slist.len()) # 1slist.append(8)slist.append(6)slist.append(3)slist.append(1)print(slist.isEmpty()) # Trueprint(slist.len())print('---------------------')slist.print_all()print('----------pop-------------')slist.pop(2)slist.print_all()print('--------insert-------')slist.insert(1, 19)slist.print_all()print('--------update-------')slist.update(1, 18)slist.print_all()print('--------remove-------')slist.remove(18)slist.print_all()

雙向鏈表:

insert、 remove、 update方法

''' 雙向鏈表 '''class Node:def __init__(self, data):self.data = dataself.next = Noneself.prev = Nonedef __str__(self):return str(self.data)class DoubleList:def __init__(self):self._head = Nonedef isEmpty(self):return self._head == Nonedef append(self, item):# 尾部添加node = Node(item)if self.isEmpty():self._head = nodeelse:cur = self._headwhile cur.next != None:cur = cur.nextcur.next = node# 求長度def add(self, item):node = Node(item)if self.isEmpty():self._head = nodeelse:node.next = self._headself._head.prev = nodeself._head = nodedef len(self):cur = self._headcount = 0while cur != None:count += 1cur = cur.nextreturn countdef print_all(self):cur = self._headwhile cur != None:print(cur)cur = cur.nextdef insert(self, index, item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(item, Node):raise TypeError('不能是Node類型')if index == 0:node = Node(item)node.next = self._headself._head.prev= nodeself._head = nodeelse:cur = self._headnode = Node(item)while index - 1:cur = cur.nextindex -= 1#cur 是xindex的前一個節(jié)點# 設(shè)置node節(jié)點的前一個是cur節(jié)點node.prev = cur#設(shè)置node的后一個節(jié)點node.next = cur.next#設(shè)置cur下一個節(jié)點的prev指向nodecur.next.prev = node# 設(shè)置cur的下一個節(jié)點cur.next = nodedef remove(self, item):if self.isEmpty():raise ValueError('double link list is empty')else:cur = self._headif cur.data == item:#刪除的是頭節(jié)點if cur.next ==None:#只有頭節(jié)點self._head = Noneelse:# 除了頭部節(jié)點,還有其他節(jié)點cur.next.prve = Noneself._head = cur.nextelse:while cur != None:if cur.data == item:cur.prev.next = cur.nextcur.next.prve = cur.prev # 雙向的breakcur = cur.nextdef update(self, index, new_item):if index < 0 or index >= self.len():raise IndexError('index Error')if isinstance(new_item, Node):raise TypeError('不能是Node類型')else:node = Node(new_item)cur = self._head#獲取curwhile index :cur = cur.nextindex -= 1node.prev = cur.prevcur.prev.next = nodenode.next =cur.nextcur.next.prev = node# if index == 0:# node.next = self._head.next# node.prev = self._head.prev# self._head = node# else:# cur = self._head# node.next = cur.next.next# node.prev = cur.prev# cur.next = node# cur.prev = nodeif __name__ == '__main__':dlist = DoubleList()print(dlist.len())print(dlist.isEmpty())# dlist.append(6)# dlist.append(9)# dlist.append(5)# print(dlist.len())# print(dlist.isEmpty())# dlist.print_all()dlist.add(6)dlist.add(9)dlist.add(5)dlist.print_all()print('--------insert-------')dlist.insert(1, 19)dlist.print_all()print('--------update-------')dlist.update(1, 1)dlist.print_all()print('--------remove-------')dlist.remove(9)dlist.print_all()

?

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的python单向链表和双向链表的图示代码说明的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 另类激情综合 | 国产猛男猛女超爽免费视频 | 亚洲 欧美 日韩系列 | 久久精品视频一区 | 在线观看超碰 | 日本黄色片免费看 | 欧美性猛交久久久乱大交小说 | 午夜日韩精品 | 亚洲乱码国产乱码精品精98午夜 | 蜜乳av中文字幕 | 亚洲免费色视频 | 鲁啊鲁在线视频 | 精品人妻人伦一区二区有限公司 | 午夜性影院 | 天堂网在线观看 | 色av免费| 成人18在线 | 超碰97自拍 | 动漫精品一区 | 操操操综合网 | 69人人| 已满18岁免费观看电视连续剧 | 日韩精品人妻中文字幕有码 | 日本女人毛茸茸 | 国产成人在线观看免费 | 欧美系列一区二区 | 亚洲色图偷拍视频 | 日本美女性高潮 | 婷婷国产在线 | 精品久久久久久久 | 国产精品传媒视频 | 自拍偷拍激情 | 插插宗合网 | 操大爷影院 | 99re6在线精品视频免费播放 | 欧美大片免费观看网址 | 久久久免费av | 激情宗合 | 日本精品视频一区 | 仙踪林av | 国产亚洲精品久久久久丝瓜 | 91精品一区二区三区在线观看 | 国产日韩欧美电影 | 69福利视频 | 国产成人无码精品久久 | 欧美特级黄色录像 | 亚洲午夜激情视频 | 久久五| 亚洲精品一级 | 国产浮力第一页 | 日韩爱爱爱 | 蜜桃久久久久久 | 喷水少妇| 午夜亚洲aⅴ无码高潮片苍井空 | 亚洲在线观看一区二区 | 成人免费视频网站在线观看 | 国产日韩不卡 | 超碰在线影院 | 激情六月 | 亚洲精品区 | 蜜桃视频一区二区三区 | 一区二区三区四区五区六区 | 午夜久久久久 | 亚洲a中文字幕 | 久久精品国产亚洲av蜜臀色欲 | 91在线视频网址 | 中文字幕第315页 | 欧美在线视频免费观看 | 一级色网站 | 欧美天天搞 | 在线免费福利视频 | 视频网站在线观看18 | 中文国产在线观看 | 国产一区二区三区高清 | 让男按摩师摸好爽 | 国产高清在线观看视频 | 伊人成人在线视频 | av免费入口 | 91偷拍网| 欧美久久99 | 伊人精品国产 | 欧美20p| 四虎4hu永久免费网站影院 | 午夜天堂av| 三男一女吃奶添下面 | 欧美午夜精品久久久久久蜜 | 成年人免费在线观看 | 一级做a视频 | aa一级片 | 天天人人综合 | wwwav在线播放 | 国产乱子伦精品视频 | 人妻无码一区二区三区免费 | 美女隐私免费网站 | 亚洲男人的天堂网站 | 天堂久久久久 | 极品美女扒开粉嫩小泬 | 人人草网 | 亚洲精品国产精品国自 |