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

主站蜘蛛池模板: 永久免费的网站入口 | 伊人22综合 | 在线看一区二区 | 秋霞成人av | 波多野结衣喷潮 | 天天草天天爽 | 四虎网址大全 | 亚洲精品aa | 亚洲人午夜精品 | 久久久久二区 | 国产亚洲欧美日韩高清 | 在线观看中文字幕第一页 | 天天干天天爽天天射 | 台湾少妇xxxx做受 | 少妇又紧又色又爽又刺激视频 | 国产资源在线播放 | 香蕉成人在线视频 | 欧美无砖区 | 午夜精品在线 | 日韩小视频网站 | www.波多野结衣.com | 性xxx欧美 | 久久精品网址 | 一级片一区二区三区 | 四级黄色片 | 精品一区二区不卡 | 亚洲精品日韩欧美 | 深夜精品| 日韩一区二区精品 | 91免费看国产 | 国外亚洲成av人片在线观看 | 国产精品伦一区二区三区免费看 | 手机看片日本 | 性高跟鞋xxxxhd国产电影 | 国产三级一区二区三区 | 九九黄色片 | 无码人妻丰满熟妇区毛片18 | 自拍偷拍日韩精品 | 精品人妻久久久久久888不卡 | 久久99精品国产麻豆婷婷洗澡 | 99自拍视频在线观看 | 天堂va蜜桃一区二区三区 | 狠狠干2024 | 久久久资源| 黄色三级视频在线观看 | 8x8x国产精品一区二区 | 日韩精品一区在线 | 日韩一区二区三区电影 | 一本到视频 | 欲色av| 成人在线播放av | 午夜激情导航 | 一区二区不卡在线 | 亚洲最大网 | www.国产视频.com | 中文字幕在线乱 | 91视频日本 | 女人又爽又黄免费女仆 | 久久综合色视频 | 日韩国产成人无码av毛片 | 久久久18| 女同性恋毛片 | 日韩激情网 | 美女被c出水 | 无码人妻aⅴ一区二区三区日本 | www.日本免费| 怡红院成永久免费人全部视频 | 男人天堂五月天 | 亚洲老老头同性老头交j | 国产精品自产拍在线观看 | 亚洲三级网站 | av手机在线免费观看 | 丝袜老师让我了一夜网站 | 在线观看日韩国产 | 依人成人 | 狠狠躁狠狠躁视频专区 | 伊人一道本 | 成人作爱视频 | 日韩三级中文 | 中文字幕观看在线 | 欧美成人一级片 | 国产激情91 | 日本一区二区三区四区视频 | 国产这里只有精品 | wwwav在线播放 | 国产干b | 三级在线观看 | 亚洲欧美日韩图片 | 亚洲特级黄色片 | 日韩亚洲欧美综合 | 亚洲色图欧洲色图 | 另类小说亚洲色图 | 久久亚洲精品无码va白人极品 | 凹凸日日摸日日碰夜夜 | 中文字幕免费中文 | 精品一区二区三区蜜桃 | 免费看裸体网站视频 | 国产日韩一级 | 丝袜综合网 |