Python實(shí)現(xiàn)雙向鏈表基本操作
雙向鏈表的基本操作的實(shí)現(xiàn),供大家參考,具體內(nèi)容如下
在之前的博客中介紹了三種鏈表,分別是單鏈表、單向循環(huán)鏈表以及雙向鏈表。本篇博客將用Python來實(shí)現(xiàn)雙向鏈表的如下操作。(用到的工具是Python 3)
is_empty() : 判斷鏈表是否為空
length() : 返回鏈表的長度
travel() : 遍歷
add(item) : 在頭部添加一個(gè)節(jié)點(diǎn)
append(item) : 在尾部添加一個(gè)節(jié)點(diǎn)
insert(pos, item) : 在指定位置 pos 添加一個(gè)節(jié)點(diǎn)
remove(item) : 刪除一個(gè)節(jié)點(diǎn)
search(item) : 查找節(jié)點(diǎn)是否存在
Python實(shí)現(xiàn)
class Node(object): ?? ?'''雙向鏈表節(jié)點(diǎn)''' ?? ?def __init__(self,item): ?? ??? ?self.item = item ?? ??? ?self.next = None ?? ??? ?self.prev = None class DoubleLink(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指向None ?? ??? ??? ?self._head = node ?? ??? ?else: ?? ??? ??? ?# 將node的next指向_head的頭節(jié)點(diǎn) ?? ??? ??? ?node.next = self._head ?? ??? ??? ?# 將_head的頭節(jié)點(diǎn)的prev指向node ?? ??? ??? ?self._head.prev = node ?? ??? ??? ?# 將_head 指向node ?? ??? ??? ?self._head = node ?? ? ?? ?def append(self, item): ?? ??? ?'''尾部插入元素''' ?? ??? ?node = Node(item) ?? ??? ?if self.is_empty(): ?? ??? ??? ?self._head = node ?? ??? ?else: ?? ??? ??? ?# 移動(dòng)到鏈表尾部 ?? ??? ??? ?cur = self._head ?? ??? ??? ?while cur.next != None: ?? ??? ??? ??? ?cur = cur.next ?? ??? ??? ?# 將尾結(jié)點(diǎn)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é)點(diǎn)
在該操作中,要注意鏈的指向的先后順序。
def insert(self, pos, item): ?? ??? ?'''在指定位置添加節(jié)點(diǎn)''' ?? ??? ?if pos <= 0: ?? ??? ??? ?self.add(item) ?? ??? ?elif pos > (self.length() - 1): ?? ??? ??? ?self.append(item) ?? ??? ?else: ?? ??? ??? ?node = Node() ?? ??? ??? ?cur = self._head() ?? ??? ??? ?count = 0 ?? ??? ??? ?# 移動(dòng)到指定的前一個(gè)位置 ?? ??? ??? ?while cur < pos - 1 : ?? ??? ??? ??? ?count += 1 ?? ??? ??? ??? ?cur = cur.next ?? ??? ??? ?# 將node的prev指向cur ?? ??? ??? ?node.prev = cur ?? ??? ??? ?# 將node的next指向cur的下一個(gè)節(jié)點(diǎn) ?? ??? ??? ?node.next = cur.next ?? ??? ??? ?# 將cur的下一個(gè)節(jié)點(diǎn)的prev指向node ?? ??? ??? ?cur.next.prev = node ?? ??? ??? ?# 將cur.next指向node ?? ??? ??? ?cur.next = node
刪除元素
def remove(self, item): ?? ??? ?'''刪除元素''' ?? ??? ?if self.is_empty(): return? ?? ??? ?else: ?? ??? ??? ?cur = self._head ?? ??? ??? ?if cur.item == item: ?? ??? ??? ??? ?# 如果首節(jié)點(diǎn)的元素是要?jiǎng)h除的元素 ?? ??? ??? ??? ?if cur.next == None: ?? ??? ??? ??? ??? ?# 如果鏈表中只有一個(gè)節(jié)點(diǎn) ?? ??? ??? ??? ??? ?self._head = None ?? ??? ??? ??? ?else: ?? ??? ??? ??? ??? ?cur.next.prev = None ?? ??? ??? ??? ??? ?self._head = cur.next ?? ??? ??? ??? ?return ?? ??? ??? ?while cur != None: ?? ??? ??? ??? ?if cur.item == item: ?? ??? ??? ??? ??? ?cur.prev.next = cur.next ?? ??? ??? ??? ??? ?cur.next.prev = cur.prev ?? ??? ??? ??? ??? ?break ?? ??? ??? ??? ?cur = cur.next
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Windows環(huán)境下如何使用Pycharm運(yùn)行sh文件
這篇文章主要介紹了Windows環(huán)境下如何使用Pycharm運(yùn)行sh文件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02對Python中創(chuàng)建進(jìn)程的兩種方式以及進(jìn)程池詳解
今天小編就為大家分享一篇對Python中創(chuàng)建進(jìn)程的兩種方式以及進(jìn)程池詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01python輸出100以內(nèi)的質(zhì)數(shù)與合數(shù)實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了python輸出100以內(nèi)的質(zhì)數(shù)與合數(shù)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07Python range、enumerate和zip函數(shù)用法詳解
這篇文章主要介紹了Python range、enumerate和zip函數(shù)用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09對python3標(biāo)準(zhǔn)庫httpclient的使用詳解
今天小編就為大家分享一篇對python3標(biāo)準(zhǔn)庫httpclient的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12django如何連接已存在數(shù)據(jù)的數(shù)據(jù)庫
這篇文章主要給大家介紹了關(guān)于django如何連接已存在數(shù)據(jù)的數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08Python+Delorean實(shí)現(xiàn)時(shí)間格式智能轉(zhuǎn)換
DeLorean是一個(gè)Python的第三方模塊,基于?pytz?和?dateutil?開發(fā),用于處理Python中日期時(shí)間的格式轉(zhuǎn)換。本文將詳細(xì)講講DeLorean的使用,感興趣的可以了解一下2022-04-04