python實現(xiàn)雙向鏈表原理
雙向鏈表
一種更復雜的鏈表是“雙向鏈表”或“雙面鏈表”。每個節(jié)點有兩個鏈接:一個指向前一個節(jié)點,當此節(jié)點為第一個節(jié)點時,指向空值;而另一個指向下一個節(jié)點,當此節(jié)點為最后一個節(jié)點時,指向空值。
操作
is_empty() 鏈表是否為空
length() 鏈表長度
travel() 遍歷鏈表
add(item) 鏈表頭部添加
append(item) 鏈表尾部添加
insert(pos, item) 指定位置添加
remove(item) 刪除節(jié)點
search(item) 查找節(jié)點是否存在
實現(xiàn)
class Node(object): ? ? """雙向鏈表節(jié)點""" ? ? def __init__(self, item): ? ? ? ? self.item = item ? ? ? ? self.next = None ? ? ? ? self.prev = None class DLinkList(object): ? ? """雙向鏈表""" ? ? def __init__(self): ? ? ? ? self.__head = None ? ? def is_empty(self): ? ? ? ? """判斷鏈表是否為空""" ? ? ? ? return self.__head == None ? ? def length(self): ? ? ? ? """返回鏈表的長度""" ? ? ? ? cur = self.__head ? ? ? ? count = 0 ? ? ? ? while cur != None: ? ? ? ? ? ? count += 1 ? ? ? ? ? ? cur = cur.next ? ? ? ? return count ? ? def travel(self): ? ? ? ? """遍歷鏈表""" ? ? ? ? cur = self.__head ? ? ? ? while cur != None: ? ? ? ? ? ? print cur.item, ? ? ? ? ? ? cur = cur.next ? ? ? ? print "" ? ? def add(self, item): ? ? ? ? """頭部插入元素""" ? ? ? ? node = Node(item) ? ? ? ? if self.is_empty(): ? ? ? ? ? ? # 如果是空鏈表,將_head指向node ? ? ? ? ? ? self.__head = node ? ? ? ? else: ? ? ? ? ? ? # 將node的next指向_head的頭節(jié)點 ? ? ? ? ? ? node.next = self.__head ? ? ? ? ? ? # 將_head的頭節(jié)點的prev指向node ? ? ? ? ? ? self.__head.prev = node ? ? ? ? ? ? # 將_head 指向node ? ? ? ? ? ? self.__head = node ? ? def append(self, item): ? ? ? ? """尾部插入元素""" ? ? ? ? node = Node(item) ? ? ? ? if self.is_empty(): ? ? ? ? ? ? # 如果是空鏈表,將_head指向node ? ? ? ? ? ? self.__head = node ? ? ? ? else: ? ? ? ? ? ? # 移動到鏈表尾部 ? ? ? ? ? ? cur = self.__head ? ? ? ? ? ? while cur.next != None: ? ? ? ? ? ? ? ? cur = cur.next ? ? ? ? ? ? # 將尾節(jié)點cur的next指向node ? ? ? ? ? ? cur.next = node ? ? ? ? ? ? # 將node的prev指向cur ? ? ? ? ? ? node.prev = cur ? ? def search(self, item): ? ? ? ? """查找元素是否存在""" ? ? ? ? cur = self.__head ? ? ? ? while cur != None: ? ? ? ? ? ? if cur.item == item: ? ? ? ? ? ? ? ? return True ? ? ? ? ? ? cur = cur.next ? ? ? ? return False
指定位置插入節(jié)點
def insert(self, pos, item): ? ? ? ? """在指定位置添加節(jié)點""" ? ? ? ? if pos <= 0: ? ? ? ? ? ? self.add(item) ? ? ? ? elif pos > (self.length()-1): ? ? ? ? ? ? self.append(item) ? ? ? ? else: ? ? ? ? ? ? node = Node(item) ? ? ? ? ? ? cur = self.__head ? ? ? ? ? ? count = 0 ? ? ? ? ? ? # 移動到指定位置的前一個位置 ? ? ? ? ? ? while count < (pos-1): ? ? ? ? ? ? ? ? count += 1 ? ? ? ? ? ? ? ? cur = cur.next ? ? ? ? ? ? # 將node的prev指向cur ? ? ? ? ? ? node.prev = cur ? ? ? ? ? ? # 將node的next指向cur的下一個節(jié)點 ? ? ? ? ? ? node.next = cur.next ? ? ? ? ? ? # 將cur的下一個節(jié)點的prev指向node ? ? ? ? ? ? cur.next.prev = node ? ? ? ? ? ? # 將cur的next指向node ? ? ? ? ? ? cur.next = node
刪除元素
def remove(self, item): ? ? ? ? """刪除元素""" ? ? ? ? cur = self.__head ? ? ? ? while cur != None: ? ? ? ? ? ? # 找到了要刪除的元素 ? ? ? ? ? ? if cur.item == item: ? ? ? ? ? ? ? ? # 先判斷此結(jié)點是否是頭節(jié)點 ? ? ? ? ? ? ? ? # 頭節(jié)點 ? ? ? ? ? ? ? ? if cur == self.__head: ? ? ? ? ? ? ? ? ? ? self.__head = cur.next ? ? ? ? ? ? ? ? ? ? # 如果存在下一個結(jié)點,則設置下一個結(jié)點 ? ? ? ? ? ? ? ? ? ? if cur.next: ? ? ? ? ? ? ? ? ? ? ? ? # 判斷鏈表是否只有一個結(jié)點 ? ? ? ? ? ? ? ? ? ? ? ? cur.next.prev = None ? ? ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? ? ? cur.prev.next = cur.next ? ? ? ? ? ? ? ? ? ? # 如果存在下一個結(jié)點,則設置下一個結(jié)點 ? ? ? ? ? ? ? ? ? ? if cur.next: ? ? ? ? ? ? ? ? ? ? ? ? cur.next.prev = cur.prev ? ? ? ? ? ? ? ? break ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? cur = cur.next
測試
if __name__ == "__main__": ? ? ll = DLinkList() ? ? ll.add(1) ? ? ll.add(2) ? ? ll.append(3) ? ? ll.insert(2, 4) ? ? ll.insert(4, 5) ? ? ll.insert(0, 6) ? ? print "length:",ll.length() ? ? ll.travel() ? ? print ll.search(3) ? ? print ll.search(4) ? ? ll.remove(1) ? ? print "length:",ll.length() ? ? ll.travel()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python3.7安裝matplotlib失敗問題的完美解決方法
由于學習需要安裝matplotlib庫,閱讀網(wǎng)上教程后一直出現(xiàn)各種各樣的錯誤,下面這篇文章主要給大家介紹了關(guān)于python3.7安裝matplotlib失敗問題的完美解決方法,需要的朋友可以參考下2022-07-07Python udp網(wǎng)絡程序?qū)崿F(xiàn)發(fā)送、接收數(shù)據(jù)功能示例
這篇文章主要介紹了Python udp網(wǎng)絡程序?qū)崿F(xiàn)發(fā)送、接收數(shù)據(jù)功能,結(jié)合實例形式分析了Python使用socket模塊進行udp套接字創(chuàng)建、數(shù)據(jù)收發(fā)等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12女友半夜加班發(fā)自拍 python男友用30行代碼發(fā)現(xiàn)驚天秘密
大家好,我是Lex 喜歡欺負超人那個Lex 女友說今晚加班,還給我發(fā)了一張照片? 我心生懷疑,就用python分析了一下照片,結(jié)果發(fā)現(xiàn)。。。 劃重點:利用Python讀取照片的GPS信息信息2021-08-08python讀取raw binary圖片并提取統(tǒng)計信息的實例
今天小編就為大家分享一篇python讀取raw binary圖片并提取統(tǒng)計信息的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01