python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
看此博客之前建議先看看B站的視頻python數(shù)據(jù)結(jié)構(gòu)與算法系列課程,該課程中未實(shí)現(xiàn)雙向循環(huán)鏈表的操作,所以我按照該視頻的鏈表思路實(shí)現(xiàn)了雙向循環(huán)鏈表的操作,歡迎大家閱讀與交流,如有侵權(quán),請(qǐng)聯(lián)系博主!
下面附上代碼:
class Node:
def __init__(self, elem):
self.elem = elem
self.prev = None
self.next = None
class DoubleCycleLinkList:
def __init__(self, node=None):
self.__head = node
def is_empty(self):
"""判空"""
if self.__head is None:
return True
return False
def length(self):
"""鏈表長(zhǎng)度"""
if self.is_empty():
return 0
cur = self.__head
count = 1
while cur.next is not self.__head:
count += 1
cur = cur.next
return count
def travel(self):
"""遍歷鏈表"""
if self.is_empty():
return
cur = self.__head
while cur.next is not self.__head:
print(cur.elem, end=" ")
cur = cur.next
print(cur.elem, end=" ")
print("")
def add(self, elem):
"""頭插法"""
node = Node(elem)
if self.is_empty():
self.__head = node
node.prev = node
node.next = node
else:
self.__head.prev.next = node
node.prev = self.__head.prev
node.next = self.__head
self.__head.prev = node
self.__head = node
def append(self, elem):
"""尾插法"""
node = Node(elem)
if self.is_empty():
self.__head = node
node.prev = node
node.next = node
else:
node.next = self.__head
node.prev = self.__head.prev
self.__head.prev.next = node
self.__head.prev = node
def insert(self, pos, elem):
"""任一位置(pos)插入, 下標(biāo)從0數(shù)起"""
if pos <= 0:
self.add(elem)
elif pos > (self.length() - 1):
self.append(elem)
else:
count = 0
cur = self.__head
node = Node(elem)
while count < (pos - 1):
count += 1
cur = cur.next
node.next = cur.next
node.prev = cur
node.next.prev = node
cur.next = node
def remove(self, elem):
"""刪除某一節(jié)點(diǎn),若有多個(gè)符合條件的節(jié)點(diǎn),刪除第一個(gè)即可"""
if self.is_empty():
return
cur = self.__head
while cur.next is not self.__head:
if cur.elem == elem:
if cur is self.__head:
self.__head = cur.next
cur.prev.next = cur.next
cur.next.prev = cur.prev
else:
cur.prev.next = cur.next
cur.next.prev = cur.prev
break
cur = cur.next
if cur.elem == elem:
cur.prev.next = self.__head
self.head = cur.prev
def search(self, elem):
"""查找某一個(gè)節(jié)點(diǎn)"""
if self.is_empty():
return False
cur = self.__head
while cur.next is not self.__head:
if cur.elem == elem:
return True
cur = cur.next
# while中處理不到尾節(jié)點(diǎn),所以進(jìn)行最后尾節(jié)點(diǎn)的判斷
if cur.elem == elem:
return True
return False
到此這篇關(guān)于python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例的文章就介紹到這了,更多相關(guān)python 雙向循環(huán)鏈表操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python單向循環(huán)鏈表實(shí)例詳解
- Python數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表詳解
- python/golang實(shí)現(xiàn)循環(huán)鏈表的示例代碼
- python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法示例
- Python雙向循環(huán)鏈表實(shí)現(xiàn)方法分析
- Python實(shí)現(xiàn)的單向循環(huán)鏈表功能示例
- Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法實(shí)例詳解【單鏈表、循環(huán)鏈表】
- python雙向鏈表實(shí)例詳解
- Python實(shí)現(xiàn)雙向鏈表基本操作
- python雙向循環(huán)鏈表實(shí)例詳解
相關(guān)文章
使用Python集合顯著優(yōu)化算法性能的實(shí)戰(zhàn)案例
掌握?Python?中的?set?數(shù)據(jù)結(jié)構(gòu),是算法和數(shù)據(jù)結(jié)構(gòu)的基本功,今天我們從一個(gè)實(shí)戰(zhàn)案例出發(fā),探討如何利用Python集合顯著優(yōu)化算法性能,感興趣的同學(xué)跟著小編一起來探討吧2023-06-06
Python打包文件夾的方法小結(jié)(zip,tar,tar.gz等)
這篇文章主要介紹了Python打包文件夾的方法,結(jié)合實(shí)例形式總結(jié)分析了Python打包成zip,tar,tar.gz等格式文件的操作技巧,需要的朋友可以參考下2016-09-09
Python實(shí)現(xiàn)從URL地址提取文件名的方法
這篇文章主要介紹了Python實(shí)現(xiàn)從URL地址提取文件名的方法,涉及OS模塊中basename方法的使用技巧,需要的朋友可以參考下2015-05-05
Python中collections.Counter()的具體使用
本文主要介紹了Python中collections.Counter()的具體使用,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07

