日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。