Python實現(xiàn)的單向循環(huán)鏈表功能示例
本文實例講述了Python實現(xiàn)的單向循環(huán)鏈表功能。分享給大家供大家參考,具體如下:
概述:
單向循環(huán)鏈表是指在單鏈表的基礎(chǔ)上,表的最后一個元素指向鏈表頭結(jié)點,不再是為空。

由圖可知,單向循環(huán)鏈表的判斷條件不再是表為空了,而變成了是否到表頭。
操作
is_empty() 判斷鏈表是否為空
length() 返回鏈表的長度
travel() 遍歷
add(item) 在頭部添加一個節(jié)點
append(item) 在尾部添加一個節(jié)點
insert(pos, item) 在指定位置pos添加節(jié)點
remove(item) 刪除一個節(jié)點
search(item) 查找節(jié)點是否存在
具體代碼:
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
# 若頭節(jié)點的元素就是要查找的元素item
if cur.item == item:
# 如果鏈表不止一個節(jié)點
if cur.next != self._head:
# 先找到尾節(jié)點,將尾節(jié)點的next指向第二個節(jié)點
while cur.next != self._head:
cur = cur.next
# cur指向了尾節(jié)點
cur.next = self._head.next
self._head = self._head.next
else:
# 鏈表只有一個節(jié)點
self._head = None
else:
pre = self._head
# 第一個節(jié)點不是要刪除的
while cur.next != self._head:
# 找到了要刪除的元素
if cur.item == item:
# 刪除
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
# cur 指向尾節(jié)點
if cur.item == item:
# 尾部刪除
pre.next = cur.next
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é)果:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(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雙向循環(huán)鏈表實現(xiàn)方法分析
- Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法實例詳解【單鏈表、循環(huán)鏈表】
- python雙向鏈表實例詳解
- Python實現(xiàn)雙向鏈表基本操作
- python雙向循環(huán)鏈表實例詳解
相關(guān)文章
解讀FastAPI異步化為transformers模型打造高性能接口
這篇文章主要介紹了解讀FastAPI異步化為transformers模型打造高性能接口問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
簡單了解Django ORM常用字段類型及參數(shù)配置
這篇文章主要介紹了簡單了解Django ORM常用字段類型及參數(shù)配置,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01

