Python數(shù)據(jù)結(jié)構(gòu)之雙向鏈表的定義與使用方法示例
本文實例講述了Python數(shù)據(jù)結(jié)構(gòu)之雙向鏈表的定義與使用方法。分享給大家供大家參考,具體如下:
和單鏈表類似,只不過是增加了一個指向前面一個元素的指針而已。
示意圖:
python 實現(xiàn)代碼:
#!/usr/bin/python # -*- coding: utf-8 -*- class Node(object): def __init__(self,val,p=0): self.data = val self.next = p self.prev = p class LinkList(object): def __init__(self): self.head = 0 def __getitem__(self, key): if self.is_empty(): print 'linklist is empty.' return elif key <0 or key > self.getlength(): print 'the given key is error' return else: return self.getitem(key) def __setitem__(self, key, value): if self.is_empty(): print 'linklist is empty.' return elif key <0 or key > self.getlength(): print 'the given key is error' return else: self.delete(key) return self.insert(key) def initlist(self,data): self.head = Node(data[0]) p = self.head for i in data[1:]: node = Node(i) p.next = node node.prev = p p = p.next def getlength(self): p = self.head length = 0 while p!=0: length+=1 p = p.next return length def is_empty(self): if self.getlength() ==0: return True else: return False def clear(self): self.head = 0 def append(self,item): q = Node(item) if self.head ==0: self.head = q else: p = self.head while p.next!=0: p = p.next p.next = q q.prev = p def getitem(self,index): if self.is_empty(): print 'Linklist is empty.' return j = 0 p = self.head while p.next!=0 and j <index: p = p.next j+=1 if j ==index: return p.data else: print 'target is not exist!' def insert(self,index,item): if self.is_empty() or index<0 or index >self.getlength(): print 'Linklist is empty.' return if index ==0: q = Node(item,self.head) self.head = q p = self.head post = self.head j = 0 while p.next!=0 and j<index: post = p p = p.next j+=1 if index ==j: q = Node(item,p) post.next = q q.prev = post q.next = p p.prev = q def delete(self,index): if self.is_empty() or index<0 or index >self.getlength(): print 'Linklist is empty.' return if index ==0: q = Node(item,self.head) self.head = q p = self.head post = self.head j = 0 while p.next!=0 and j<index: post = p p = p.next j+=1 if index ==j: post.next = p.next p.next.prev = post def index(self,value): if self.is_empty(): print 'Linklist is empty.' return p = self.head i = 0 while p.next!=0 and not p.data ==value: p = p.next i+=1 if p.data == value: return i else: return -1 l = LinkList() l.initlist([1,2,3,4,5]) print "腳本之家測試結(jié)果:" print l.getitem(4) l.append(6) print l.getitem(5) l.insert(4,40) print l.getitem(3) print l.getitem(4) print l.getitem(5) l.delete(5) print l.getitem(5) l.index(5)
結(jié)果為;
和單鏈表結(jié)果一樣。
PS:雙向鏈表就是將鏈表首尾相接。
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Selenium?三種等待方式(強制等待、隱式等待、顯示等待)
這篇文章主要介紹了Selenium?三種等待方式(強制等待、隱式等待、顯示等待),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Python詳解argparse參數(shù)模塊之命令行參數(shù)
這篇文章主要介紹了Python詳解argparse參數(shù)模塊之命令行參數(shù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考下面文章詳解2022-07-07Python 在區(qū)塊鏈智能合約開發(fā)中的應(yīng)用與實踐小結(jié)
Python作為一種廣泛應(yīng)用的編程語言,在區(qū)塊鏈智能合約開發(fā)中扮演著重要角色,通過使用Python框架如Brownie和Web3.py,開發(fā)者可以輕松編寫和部署智能合約,感興趣的朋友一起看看吧2024-09-09Python3的unicode編碼轉(zhuǎn)換成中文的問題及解決方案
這篇文章主要介紹了Python3的unicode編碼轉(zhuǎn)換成中文的問題及解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12