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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

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

python

Python 实现一个全面的单链表

發(fā)布時(shí)間:2025/7/14 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 实现一个全面的单链表 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

    • 前言
    • 實(shí)現(xiàn)清單
    • 鏈表實(shí)現(xiàn)
    • 總結(jié)

前言

算法和數(shù)據(jù)結(jié)構(gòu)是一個(gè)亙古不變的話(huà)題,作為一個(gè)程序員,掌握常用的數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)是非常非常的有必要的。

實(shí)現(xiàn)清單

實(shí)現(xiàn)鏈表,本質(zhì)上和語(yǔ)言是無(wú)關(guān)的。但是靈活度卻和實(shí)現(xiàn)它的語(yǔ)言密切相關(guān)。今天用Python來(lái)實(shí)現(xiàn)一下,包含如下操作:

['addNode(self, data)'] ['append(self, value)'] ['prepend(self, value)'] ['insert(self, index, value)'] ['delNode(self, index)'] ['delValue(self, value)'] ['isempty(self)'] ['truncate(self)'] ['getvalue(self, index)'] ['peek(self)'] ['pop(self)'] ['reverse(self)'] ['delDuplecate(self)'] ['updateNode(self, index, value)'] ['size(self)'] ['print(self)']

生成這樣的一個(gè)方法清單肯定是不能手動(dòng)寫(xiě)了,要不然得多麻煩啊,于是我寫(xiě)了個(gè)程序,來(lái)匹配這些自己實(shí)現(xiàn)的方法。代碼比較簡(jiǎn)單,核心思路就是匹配源文件的每一行,找到符合匹配規(guī)則的內(nèi)容,并添加到總的結(jié)果集中。

代碼如下:

# coding: utf8# @Author: 郭 璞 # @File: getmethods.py # @Time: 2017/4/5 # @Contact: 1064319632@qq.com # @blog: http://blog.csdn.net/marksinoberg # @Description: 獲取一個(gè)模塊或者類(lèi)中的所有方法及參數(shù)列表import redef parse(filepath, repattern):with open(filepath, 'rb') as f:lines = f.readlines()# 預(yù)解析正則rep = re.compile(repattern)# 創(chuàng)建保存方法和參數(shù)列表的結(jié)果集列表result = []# 開(kāi)始正式的匹配實(shí)現(xiàn)for line in lines:res = re.findall(rep, str(line))print("{}的匹配結(jié)果{}".format(str(line), res))if len(res)!=0 or res is not None:result.append(res)else:continuereturn [item for item in result if item !=[]]if __name__ == '__main__':repattern = "def (.[^_0-9]+\(.*?\)):"filepath = './SingleChain.py'result = parse(filepath, repattern)for item in result:print(str(item))

鏈表實(shí)現(xiàn)

# coding: utf8# @Author: 郭 璞 # @File: SingleChain.py # @Time: 2017/4/5 # @Contact: 1064319632@qq.com # @blog: http://blog.csdn.net/marksinoberg # @Description: 單鏈表實(shí)現(xiàn)class Node(object):def __init__(self, data, next):self.data = dataself.next = nextclass LianBiao(object):def __init__(self):self.root = None# 給單鏈表添加元素節(jié)點(diǎn)def addNode(self, data):if self.root==None:self.root = Node(data=data, next=None)return self.rootelse:# 有頭結(jié)點(diǎn),則需要遍歷到尾部節(jié)點(diǎn),進(jìn)行鏈表增加操作cursor = self.rootwhile cursor.next!= None:cursor = cursor.nextcursor.next = Node(data=data, next=None)return self.root# 在鏈表的尾部添加新節(jié)點(diǎn),底層調(diào)用addNode方法即可def append(self, value):self.addNode(data=value)# 在鏈表首部添加節(jié)點(diǎn)def prepend(self, value):if self.root == None:self.root = Node(value, None)else:newroot = Node(value, None)# 更新root索引newroot.next = self.rootself.root = newroot# 在鏈表的指定位置添加節(jié)點(diǎn)def insert(self, index, value):if self.root == None:returnif index<=0 or index >self.size():print('index %d 非法, 應(yīng)該審視一下您的插入節(jié)點(diǎn)在整個(gè)鏈表的位置!')returnelif index==1:# 如果index==1, 則在鏈表首部添加即可self.prepend(value)elif index == self.size()+1:# 如果index正好比當(dāng)前鏈表長(zhǎng)度大一,則添加在尾部即可self.append(value)else:# 如此,在鏈表中部添加新節(jié)點(diǎn),直接進(jìn)行添加即可。需要使用計(jì)數(shù)器來(lái)維護(hù)插入未知counter = 2pre = self.rootcursor = self.root.nextwhile cursor!=None:if counter == index:temp = Node(value, None)pre.next = temptemp.next = cursorbreakelse:counter += 1pre = cursorcursor = cursor.next# 刪除指定位置上的節(jié)點(diǎn)def delNode(self, index):if self.root == None:returnif index<=0 or index > self.size():return# 對(duì)第一個(gè)位置需要小心處理if index == 1:self.root = self.root.nextelse:pre = self.rootcursor = pre.nextcounter = 2while cursor!= None:if index == counter:print('can be here!')pre.next = cursor.nextbreakelse:pre = cursorcursor = cursor.nextcounter += 1# 刪除值為value的鏈表節(jié)點(diǎn)元素def delValue(self, value):if self.root == None:return# 對(duì)第一個(gè)位置需要小心處理if self.root.data == value:self.root = self.root.nextelse:pre = self.rootcursor = pre.nextwhile cursor!=None:if cursor.data == value:pre.next = cursor.next# 千萬(wàn)記得更新這個(gè)節(jié)點(diǎn),否則會(huì)出現(xiàn)死循環(huán)。。。cursor = cursor.nextcontinueelse:pre = cursorcursor = cursor.next# 判斷鏈表是否為空def isempty(self):if self.root == None or self.size()==0:return Trueelse:return False# 刪除鏈表及其內(nèi)部所有元素def truncate(self):if self.root == None or self.size()==0:returnelse:cursor = self.rootwhile cursor!= None:cursor.data = Nonecursor = cursor.nextself.root = Nonecursor = None# 獲取指定位置的節(jié)點(diǎn)的值def getvalue(self, index):if self.root is None or self.size()==0:print('當(dāng)前鏈表為空!')return Noneif index<=0 or index>self.size():print("index %d不合法!"%index)return Noneelse:counter = 1cursor = self.rootwhile cursor is not None:if index == counter:return cursor.dataelse:counter += 1cursor = cursor.next# 獲取鏈表尾部的值,且不刪除該尾部節(jié)點(diǎn)def peek(self):return self.getvalue(self.size())# 獲取鏈表尾部節(jié)點(diǎn)的值,并刪除該尾部節(jié)點(diǎn)def pop(self):if self.root is None or self.size()==0:print('當(dāng)前鏈表已經(jīng)為空!')return Noneelif self.size()==1:top = self.root.dataself.root = Nonereturn topelse:pre = self.rootcursor = pre.nextwhile cursor.next is not None:pre = cursorcursor = cursor.nexttop = cursor.datacursor = Nonepre.next = Nonereturn top# 單鏈表逆序?qū)崿F(xiàn)def reverse(self):if self.root is None:returnif self.size()==1:returnelse:# post = Nonepre = Nonecursor = self.rootwhile cursor is not None:# print('逆序操作逆序操作')post = cursor.nextcursor.next = prepre = cursorcursor = post# 千萬(wàn)不要忘記了把逆序后的頭結(jié)點(diǎn)賦值給root,否則無(wú)法正確顯示self.root = pre# 刪除鏈表中的重復(fù)元素def delDuplecate(self):# 使用一個(gè)map來(lái)存放即可,類(lèi)似于變形的“桶排序”dic = {}if self.root == None:returnif self.size() == 1:returnpre = self.rootcursor = pre.nextdic = {}# 為字典賦值temp = self.rootwhile temp!=None:dic[str(temp.data)] = 0temp = temp.nexttemp = None# 開(kāi)始實(shí)施刪除重復(fù)元素的操作while cursor!=None:if dic[str(cursor.data)] == 1:pre.next = cursor.nextcursor = cursor.nextelse:dic[str(cursor.data)] += 1pre = cursorcursor = cursor.next# 修改指定位置節(jié)點(diǎn)的值def updateNode(self, index, value):if self.root == None:returnif index<0 or index>self.size():returnif index == 1:self.root.data = valuereturnelse:cursor = self.root.nextcounter = 2while cursor!=None:if counter == index:cursor.data = valuebreakcursor = cursor.nextcounter += 1# 獲取單鏈表的大小def size(self):counter = 0if self.root == None:return counterelse:cursor = self.rootwhile cursor!=None:counter +=1cursor = cursor.nextreturn counter# 打印鏈表自身元素def print(self):if(self.root==None):returnelse:cursor = self.rootwhile cursor!=None:print(cursor.data, end='\t')cursor = cursor.nextprint()if __name__ == '__main__':# 創(chuàng)建一個(gè)鏈表對(duì)象lianbiao = LianBiao()# 判斷當(dāng)前鏈表是否為空print("鏈表為空%d"%lianbiao.isempty())# 判斷當(dāng)前鏈表是否為空lianbiao.addNode(1)print("鏈表為空%d"%lianbiao.isempty())# 添加一些節(jié)點(diǎn),方便操作lianbiao.addNode(2)lianbiao.addNode(3)lianbiao.addNode(4)lianbiao.addNode(6)lianbiao.addNode(5)lianbiao.addNode(6)lianbiao.addNode(7)lianbiao.addNode(3)# 打印當(dāng)前鏈表所有值print('打印當(dāng)前鏈表所有值')lianbiao.print()# 測(cè)試對(duì)鏈表求size的操作print("鏈表的size: "+str(lianbiao.size()))# 測(cè)試指定位置節(jié)點(diǎn)值的獲取print('測(cè)試指定位置節(jié)點(diǎn)值的獲取')print(lianbiao.getvalue(1))print(lianbiao.getvalue(lianbiao.size()))print(lianbiao.getvalue(7))# 測(cè)試刪除鏈表中指定值, 可重復(fù)性刪除print('測(cè)試刪除鏈表中指定值, 可重復(fù)性刪除')lianbiao.delNode(4)lianbiao.print()lianbiao.delValue(3)lianbiao.print()# 去除鏈表中的重復(fù)元素print('去除鏈表中的重復(fù)元素')lianbiao.delDuplecate()lianbiao.print()# 指定位置的鏈表元素的更新測(cè)試print('指定位置的鏈表元素的更新測(cè)試')lianbiao.updateNode(6, 99)lianbiao.print()# 測(cè)試在鏈表首部添加節(jié)點(diǎn)print('測(cè)試在鏈表首部添加節(jié)點(diǎn)')lianbiao.prepend(77)lianbiao.prepend(108)lianbiao.print()# 測(cè)試在鏈表尾部添加節(jié)點(diǎn)print('測(cè)試在鏈表尾部添加節(jié)點(diǎn)')lianbiao.append(99)lianbiao.append(100)lianbiao.print()# 測(cè)試指定下標(biāo)的插入操作print('測(cè)試指定下標(biāo)的插入操作')lianbiao.insert(1, 10010)lianbiao.insert(3, 333)lianbiao.insert(lianbiao.size(), 99999)lianbiao.print()# 測(cè)試peek 操作print('測(cè)試peek 操作')print(lianbiao.peek())lianbiao.print()# 測(cè)試pop 操作print('測(cè)試pop 操作')print(lianbiao.pop())lianbiao.print()# 測(cè)試單鏈表的逆序輸出print('測(cè)試單鏈表的逆序輸出')lianbiao.reverse()lianbiao.print()# 測(cè)試鏈表的truncate操作print('測(cè)試鏈表的truncate操作')lianbiao.truncate()lianbiao.print()

代碼運(yùn)行的結(jié)果如何呢?是否能滿(mǎn)足我們的需求,且看打印的結(jié)果:

D:\Software\Python3\python.exe E:/Code/Python/Python3/CommonTest/datastructor/SingleChain.py 鏈表為空1 鏈表為空0 打印當(dāng)前鏈表所有值 1 2 3 4 6 5 6 7 3 鏈表的size: 9 測(cè)試指定位置節(jié)點(diǎn)值的獲取 1 3 6 測(cè)試刪除鏈表中指定值, 可重復(fù)性刪除 can be here! 1 2 3 6 5 6 7 3 1 2 6 5 6 7 去除鏈表中的重復(fù)元素 1 2 6 5 7 指定位置的鏈表元素的更新測(cè)試 1 2 6 5 7 測(cè)試在鏈表首部添加節(jié)點(diǎn) 108 77 1 2 6 5 7 測(cè)試在鏈表尾部添加節(jié)點(diǎn) 108 77 1 2 6 5 7 99 100 測(cè)試指定下標(biāo)的插入操作 10010 108 333 77 1 2 6 5 7 99 99999 100 測(cè)試peek 操作 100 10010 108 333 77 1 2 6 5 7 99 99999 100 測(cè)試pop 操作 100 10010 108 333 77 1 2 6 5 7 99 99999 測(cè)試單鏈表的逆序輸出 99999 99 7 5 6 2 1 77 333 108 10010 測(cè)試鏈表的truncate操作Process finished with exit code 0

剛好實(shí)現(xiàn)了目標(biāo)需求。

總結(jié)

今天的內(nèi)容還是比較基礎(chǔ),也沒(méi)什么難點(diǎn)。但是看懂和會(huì)寫(xiě)還是兩碼事,沒(méi)事的時(shí)候?qū)憣?xiě)這樣的代碼還是很有收獲的。

總結(jié)

以上是生活随笔為你收集整理的Python 实现一个全面的单链表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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

主站蜘蛛池模板: 男女av在线 | 99在线国产| 一区二区三区在线观看视频 | 偷拍女澡堂一区二区三区 | 国产精品无码自拍 | 欧产日产国产精品98 | 亚洲网在线观看 | 成年网站免费观看 | aa免费视频 | 成人精品免费视频 | 国产精品自产拍在线观看 | 久草视频免费看 | 美女网站一区 | 成人国产毛片 | 黄色视屏在线免费观看 | 亚洲av无码国产精品麻豆天美 | 不卡一区二区在线观看 | www.中文字幕av| 久久91精品国产91久久小草 | 日日夜夜人人 | 亚洲伊人久久综合 | 国产探花一区 | 国内精品久久久久久久久久久 | 污漫网站 | 国产成人一区二区三区影院在线 | 仙踪林久久久久久久999 | 国产香蕉视频在线播放 | 91av视频网站 | 伊人网在线免费观看 | 91一区在线 | 日本老年老熟无码 | 国产学生美女无遮拦高潮视频 | 热99视频| 91狠狠爱| 人人看人人模 | 国产不卡在线 | 伊人99在线 | 欧美无吗 | 中文字幕亚洲专区 | 色屁屁影院www国产高清麻豆 | 黑人精品xxx一区一二区 | 欧美精品1区 | 精品熟妇无码av免费久久 | 欧美视频一区二区三区在线观看 | 色综合五月 | 亚洲另类欧美日韩 | 日日操天天操 | 伊人成年综合网 | 精品在线观看视频 | 伊人久久久久久久久久 | 无码人妻aⅴ一区二区三区有奶水 | 欧美性极品少妇xxxx | 欧美不卡在线观看 | 中文字幕 国产精品 | 永久视频| 黑巨茎大战欧美白妞 | 免费人成视频在线播放 | 亚洲欧美日韩色 | 风韵丰满熟妇啪啪区老熟熟女 | 黄色三级网站在线观看 | www.成人免费视频 | 福利姬在线观看 | 国产无码精品在线观看 | 茄子av在线 | 99精品在线视频观看 | 国产一级啪啪 | 亚洲第三区 | 成人免费网站 | 久艹在线观看视频 | 黄色在线网站 | 国产亚洲第一页 | 最近中文字幕在线mv视频在线 | 91免费版视频 | 日韩男人的天堂 | 美人被强行糟蹋np各种play | 1024亚洲天堂 | 狠狠五月| 95看片淫黄大片一级 | 精品国产亚洲av麻豆 | 五月天伊人网 | 日韩成人精品一区二区三区 | 成人国产免费视频 | 亚洲天堂三区 | 亚洲毛片儿 | 成人学院中文字幕 | 韩国一区在线 | 黄色xxxx| 亚洲视频免费看 | 91日日| 色呦呦免费视频 | 亚洲伦乱 | 北岛玲一区二区 | 国产高清99| 国产深喉视频一区二区 | 男女日批在线观看 | 日批视频免费播放 | 国产九九九 | 日本做爰全过程免费看 | 亚洲影院中文字幕 |