python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法示例
本文實(shí)例講述了python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
單向循環(huán)鏈表
單鏈表的一個(gè)變形是單向循環(huán)鏈表,鏈表中最后一個(gè)節(jié)點(diǎn)的next域不再為None,而是指向鏈表的頭節(jié)點(diǎn)。

操作
- is_empty() 判斷鏈表是否為空
- length() 返回鏈表的長(zhǎng)度
- travel() 遍歷
- add(item) 在頭部添加一個(gè)節(jié)點(diǎn)
- append(item) 在尾部添加一個(gè)節(jié)點(diǎn)
- insert(pos, item) 在指定位置pos添加節(jié)點(diǎn)
- remove(item) 刪除一個(gè)節(jié)點(diǎn)
- search(item) 查找節(jié)點(diǎn)是否存在
實(shí)現(xiàn)
# -*- coding:utf-8 -*-
#! python3
class Node(object):
"""節(jié)點(diǎn)"""
def __init__(self, item):
self.item = item
self.next = None
class SinCycLinkedlist(object):
"""單向循環(huán)鏈表"""
def __init__(self):
self.__head = None
def is_empty(self):
"""判斷鏈表是否為空"""
return self.__head == None
def length(self):
"""返回鏈表的長(zhǎng)度"""
# 如果鏈表為空,返回長(zhǎng)度0
if self.is_empty():
return 0
count = 1
cur = self.__head
while cur.next != self.__head:
count += 1
cur = cur.next
return count
def travel(self):
"""遍歷鏈表"""
if self.is_empty():
return
cur = self.__head
print(cur.item,)
while cur.next != self.__head:
cur = cur.next
print(cur.item,)
print("")
def add(self, item):
"""頭部添加節(jié)點(diǎn)"""
node = Node(item)
if self.is_empty():
self.__head = node
node.next = self.__head
else:
# 添加的節(jié)點(diǎn)指向_head
node.next = self.__head
# 移到鏈表尾部,將尾部節(jié)點(diǎn)的next指向node
cur = self.__head
while cur.next != self.__head:
cur = cur.next
cur.next = node
# _head指向添加node的
self.__head = node
def append(self, item):
"""尾部添加節(jié)點(diǎn)"""
node = Node(item)
if self.is_empty():
self.__head = node
node.next = self.__head
else:
# 移到鏈表尾部
cur = self.__head
while cur.next != self.__head:
cur = cur.next
# 將尾節(jié)點(diǎn)指向node
cur.next = node
# 將node指向頭節(jié)點(diǎn)_head
node.next = self.__head
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(item)
cur = self.__head
count = 0
# 移動(dòng)到指定位置的前一個(gè)位置
while count < (pos - 1):
count += 1
cur = cur.next
node.next = cur.next
cur.next = node
def remove(self, item):
"""刪除一個(gè)節(jié)點(diǎn)"""
# 若鏈表為空,則直接返回
if self.is_empty():
return
# 將cur指向頭節(jié)點(diǎn)
cur = self.__head
pre = None
while cur.next != self.__head:
if cur.item == item:
# 先判斷此結(jié)點(diǎn)是否是頭節(jié)點(diǎn)
if cur == self.__head:
# 頭節(jié)點(diǎn)的情況
# 找尾節(jié)點(diǎn)
rear = self.__head
while rear.next != self.__head:
rear = rear.next
self.__head = cur.next
rear.next = self.__head
else:
# 中間節(jié)點(diǎn)
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
# 退出循環(huán),cur指向尾節(jié)點(diǎn)
if cur.item == item:
if cur == self.__head:
# 鏈表只有一個(gè)節(jié)點(diǎn)
self.__head = None
else:
# pre.next = cur.next
pre.next = self.__head
def search(self, item):
"""查找節(jié)點(diǎn)是否存在"""
if self.is_empty():
return False
cur = self.__head
if cur.item == item:
return True
while cur.next != self.__head:
cur = cur.next
if cur.item == item:
return True
return False
if __name__ == "__main__":
ll = SinCycLinkedlist()
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(7))
ll.remove(1)
print("length:", ll.length())
ll.travel()
運(yùn)行結(jié)果:
length: 6
6
2
1
4
3
5True
False
length: 5
6
2
4
3
5
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python單向循環(huán)鏈表實(shí)例詳解
- Python數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表詳解
- python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
- python/golang實(shí)現(xiàn)循環(huá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數(shù)據(jù)結(jié)構(gòu)之棧詳解
棧和隊(duì)列是在程序設(shè)計(jì)中常見的數(shù)據(jù)類型,從數(shù)據(jù)結(jié)構(gòu)的角度來(lái)講,棧和隊(duì)列也是線性表,是操作受限的線性表。本文將詳細(xì)介紹一下Python中的棧,感興趣的可以了解一下2022-03-03
Matlab如何實(shí)現(xiàn)矩陣復(fù)制擴(kuò)充
這篇文章主要介紹了使用Matlab實(shí)現(xiàn)矩陣復(fù)制擴(kuò)充的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Python基于pyopencv人臉識(shí)別并繪制GUI界面
本文詳細(xì)講解了Python基于pyopencv人臉識(shí)別并繪制GUI界面,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
基于Python+tkinter實(shí)現(xiàn)簡(jiǎn)易計(jì)算器桌面軟件
tkinter是Python的標(biāo)準(zhǔn)GUI庫(kù),對(duì)于初學(xué)者來(lái)說,它非常友好,因?yàn)樗峁┝舜罅康念A(yù)制部件,本文小編就來(lái)帶大家詳細(xì)一下如何利用tkinter制作一個(gè)簡(jiǎn)易計(jì)算器吧2023-09-09

