這篇文章將為大家詳細講解有關怎么在python中實現(xiàn)一個單向循環(huán)鏈表,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設,東寧企業(yè)網(wǎng)站建設,東寧品牌網(wǎng)站建設,網(wǎng)站定制,東寧網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,東寧網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。單向循環(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é)點
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()運行結果:
length: 6
6
2
1
4
3
5True
False
length: 5
6
2
4
3
5
關于怎么在python中實現(xiàn)一個單向循環(huán)鏈表就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
當前文章:怎么在python中實現(xiàn)一個單向循環(huán)鏈表-創(chuàng)新互聯(lián)
分享路徑:http://chinadenli.net/article28/cepccp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供用戶體驗、虛擬主機、網(wǎng)站建設、網(wǎng)站內鏈、企業(yè)建站、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)