Python實現(xiàn)環(huán)形鏈表
本文實例為大家分享了Python實現(xiàn)環(huán)形鏈表的具體代碼,供大家參考,具體內(nèi)容如下
我們將單向鏈表的最后一個節(jié)點的指針指向鏈表的頭部(第一個節(jié)點),那么就形成了一個環(huán)形鏈表。環(huán)形節(jié)點可以從任意節(jié)點開始遍歷其他的節(jié)點。
這里主要實現(xiàn)了環(huán)形鏈表節(jié)點的遍歷、添加、插入、刪除,反轉(zhuǎn)。
代碼如下:
class Player:
? ? """節(jié)點類"""
? ? def __init__(self):
? ? ? ? """初始化姓名,分數(shù),指針"""
? ? ? ? self.name = ''
? ? ? ? self.score = 0
? ? ? ? self.next = None
?
?
def ergodic(head, num=None, is_print=False):
? ? """遍歷函數(shù),num是遍歷到哪一個位置序號,is_print是否觸發(fā)打印方法"""
? ? if head.next is None:
? ? ? ? return None
? ? ptr = head
? ? count = 0
? ? while True:
? ? ? ? count += 1
? ? ? ? if is_print:
? ? ? ? ? ? print('No.'+str(count), ptr.name, ptr.score, '--->', ptr.next.name)
? ? ? ? if count == num:
? ? ? ? ? ? break
? ? ? ? if ptr.next == head:
? ? ? ? ? ? break
? ? ? ? ptr = ptr.next
? ? return ptr ?# 返回遍歷完成后的最后一個節(jié)點
?
?
def invert(x): ?# x是鏈表的第一個節(jié)點
? ? """反轉(zhuǎn)環(huán)形鏈表"""
? ? y = x.next ?# y是x原來的next
? ? x.next = ergodic(x) ?# 將第一個節(jié)點的next指向最后一個節(jié)點(因為反轉(zhuǎn)了)
? ? while True: ?# 循環(huán)反轉(zhuǎn)后面的所有節(jié)點
? ? ? ? r = y.next
? ? ? ? y.next = x
? ? ? ? if r == head: ?# r是head說明y已經(jīng)是原本鏈表的最后一個節(jié)點了
? ? ? ? ? ? return y ?# 返回y,這個y是反轉(zhuǎn)后的鏈表的第一個節(jié)點
? ? ? ? x = y
? ? ? ? y = r
?
?
head = Player()
ptr = head
?
?
while True:
? ? select = input("(1).新增 ? (2).查看 ? (3).插入 ? (4).刪除 ? (5).反轉(zhuǎn) ? (6).離開\n輸入:")
? ? if select == "1": ?# 新增節(jié)點
? ? ? ? ptr = ergodic(head) ?# 獲取當前鏈表最后一個節(jié)點
? ? ? ? if ptr is None: ?# ptr為None說明當前在添加第一個節(jié)點head
? ? ? ? ? ? head.name = input("姓名:")
? ? ? ? ? ? head.score = input("分數(shù):")
? ? ? ? ? ? head.next = head
? ? ? ? else: ?# 添加第一個節(jié)點之后的節(jié)點
? ? ? ? ? ? next_data = Player()
? ? ? ? ? ? next_data.name = input("姓名:")
? ? ? ? ? ? next_data.score = input("分數(shù):")
? ? ? ? ? ? next_data.next = head
? ? ? ? ? ? ptr.next = next_data
?
? ? elif select == "2": ?# 遍歷查看鏈表所有節(jié)點
? ? ? ? ergodic(head, is_print=True) ?# 遍歷鏈表,將打印參數(shù)設(shè)為True
?
? ? elif select == '3': ?# 向鏈表中任意位置插入節(jié)點,位置以序號表示,即第一個節(jié)點序號為1,第二個節(jié)點序號為2,以此類推
? ? ? ? try:
? ? ? ? ? ? num = int(input("請輸入需要插入的節(jié)點位置序號:")) ?# 輸入序號必須是大于0的正整數(shù),如果輸入大于最后一個節(jié)點的序號則插入到最后一個節(jié)點之后
? ? ? ? ? ? if num < 1:
? ? ? ? ? ? ? ? print("輸入必須為大于0的正整數(shù)")
? ? ? ? ? ? ? ? continue
? ? ? ? except ValueError:
? ? ? ? ? ? print("輸入有誤")
? ? ? ? ? ? continue
? ? ? ? ptr = ergodic(head, num-1) ?# 獲取需要插入位置的前一個節(jié)點
? ? ? ? insert_data = Player()
? ? ? ? insert_data.name = input("姓名:")
? ? ? ? insert_data.score = input("分數(shù):")
? ? ? ? insert_data.next = ptr.next
? ? ? ? ptr.next = insert_data
? ? ? ? if num == 1: ?# 如果插入位置是1的話,那么head將發(fā)生變化
? ? ? ? ? ? head = insert_data
?
? ? elif select == '4': ?# 刪除鏈表中任意位置的節(jié)點
? ? ? ? try:
? ? ? ? ? ? num = int(input("請輸入需要刪除的節(jié)點位置序號:")) ?# 輸入序號必須是大于0的正整數(shù),如果輸入大于最后一個節(jié)點的序號則刪除最后一個節(jié)點
? ? ? ? ? ? if num < 1:
? ? ? ? ? ? ? ? print("輸入必須為大于0的正整數(shù)")
? ? ? ? ? ? ? ? continue
? ? ? ? except ValueError:
? ? ? ? ? ? print("輸入有誤")
? ? ? ? ? ? continue
? ? ? ? ptr = ergodic(head, num - 1) ?# 獲取需要刪除位置的前一個節(jié)點
? ? ? ? if ptr == ergodic(head, num): ?# 輸入序號過大時需要做特殊處理,因為輸入序號過大也代表刪除最后一個節(jié)點,那么這時我需要獲取這最后一個節(jié)點的前一個節(jié)點
? ? ? ? ? ? ptr = ergodic(ptr)
? ? ? ? ptr.next = ptr.next.next
? ? ? ? if num == 1: ?# 如果刪除位置是1的話,那么head將發(fā)生變化
? ? ? ? ? ? head = ptr.next
?
? ? elif select == '5': ?# 反轉(zhuǎn)鏈表
? ? ? ? new_first = invert(head) ?# 獲取新的第一個節(jié)點
? ? ? ? head = new_first ?# head指向新的第一個節(jié)點
? ? ? ? print('成功反轉(zhuǎn)')
?
? ? elif select == '6':
? ? ? ? print("成功離開")
? ? ? ? break
? ? else:
? ? ? ? print("輸入錯誤,請重試")部分運行結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
keras.layers.Conv2D()函數(shù)參數(shù)用法及說明
這篇文章主要介紹了keras.layers.Conv2D()函數(shù)參數(shù)用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
CentOS 7 安裝python3.7.1的方法及注意事項
這篇文章主要介紹了CentOS 7 安裝python3.7.1的方法,文中給大家提到了注意事項,需要的朋友可以參考下2018-11-11
Python多線程與多進程相關(guān)知識總結(jié)
進程(process)和線程(thread)是操作系統(tǒng)的基本概念,是操作系統(tǒng)程序運行的基本單元,本文簡要介紹進程和線程的概念以及Python中的多進程和多線程.需要的朋友可以參考下2021-05-05
python中的Numpy二維數(shù)組遍歷與二維數(shù)組切片后遍歷效率比較
這篇文章主要介紹了python中的Numpy二維數(shù)組遍歷與二維數(shù)組切片后遍歷效率比較,在python-numpy使用中,可以用雙層?for循環(huán)對數(shù)組元素進行訪問,也可以切片成每一行后進行一維數(shù)組的遍歷,下面小編擊來舉例介紹吧,需要的朋友可以參考一下2022-03-03

