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

操作
- is_empty() 判斷鏈表是否為空
- length() 返回鏈表的長度
- travel() 遍歷
- add(item) 在頭部添加一個節(jié)點
- append(item) 在尾部添加一個節(jié)點
- insert(pos, item) 在指定位置pos添加節(jié)點
- remove(item) 刪除一個節(jié)點
- search(item) 查找節(jié)點是否存在
實現(xiàn)
# -*- coding:utf-8 -*-
#! python3
class Node(object):
"""節(jié)點"""
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):
"""返回鏈表的長度"""
# 如果鏈表為空,返回長度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é)點"""
node = Node(item)
if self.is_empty():
self.__head = node
node.next = self.__head
else:
# 添加的節(jié)點指向_head
node.next = self.__head
# 移到鏈表尾部,將尾部節(jié)點的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é)點"""
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é)點指向node
cur.next = node
# 將node指向頭節(jié)點_head
node.next = self.__head
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.next = cur.next
cur.next = node
def remove(self, item):
"""刪除一個節(jié)點"""
# 若鏈表為空,則直接返回
if self.is_empty():
return
# 將cur指向頭節(jié)點
cur = self.__head
pre = None
while cur.next != self.__head:
if cur.item == item:
# 先判斷此結(jié)點是否是頭節(jié)點
if cur == self.__head:
# 頭節(jié)點的情況
# 找尾節(jié)點
rear = self.__head
while rear.next != self.__head:
rear = rear.next
self.__head = cur.next
rear.next = self.__head
else:
# 中間節(jié)點
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
# 退出循環(huán),cur指向尾節(jié)點
if cur.item == item:
if cur == self.__head:
# 鏈表只有一個節(jié)點
self.__head = None
else:
# pre.next = cur.next
pre.next = self.__head
def search(self, item):
"""查找節(jié)點是否存在"""
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()
運行結(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)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- python單向循環(huán)鏈表實例詳解
- Python數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表詳解
- python實現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
- python/golang實現(xiàn)循環(huán)鏈表的示例代碼
- Python雙向循環(huán)鏈表實現(xiàn)方法分析
- Python實現(xiàn)的單向循環(huán)鏈表功能示例
- Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法實例詳解【單鏈表、循環(huán)鏈表】
- python雙向鏈表實例詳解
- Python實現(xiàn)雙向鏈表基本操作
- python雙向循環(huán)鏈表實例詳解
相關(guān)文章
Python數(shù)據(jù)結(jié)構(gòu)之棧詳解
棧和隊列是在程序設(shè)計中常見的數(shù)據(jù)類型,從數(shù)據(jù)結(jié)構(gòu)的角度來講,棧和隊列也是線性表,是操作受限的線性表。本文將詳細(xì)介紹一下Python中的棧,感興趣的可以了解一下2022-03-03
Matlab如何實現(xiàn)矩陣復(fù)制擴(kuò)充
這篇文章主要介紹了使用Matlab實現(xiàn)矩陣復(fù)制擴(kuò)充的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
基于Python+tkinter實現(xiàn)簡易計算器桌面軟件
tkinter是Python的標(biāo)準(zhǔn)GUI庫,對于初學(xué)者來說,它非常友好,因為它提供了大量的預(yù)制部件,本文小編就來帶大家詳細(xì)一下如何利用tkinter制作一個簡易計算器吧2023-09-09

