日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python實現單向鏈表_python

作者:HBLQ_GK ? 更新時間: 2022-07-23 編程語言

單向鏈表每個節點都是由兩部分組成:數據字段和指針,指針指向下一個元素在內存中的位置。

單向鏈表的第一個節點節點是鏈表頭指針,而最后一個節點的指針設為None,不指向任何元素。(鏈表頭指針相當重要?。。。?/p>

使用Python實現單向鏈表,我們可以使用一個節點類來封裝節點,每創建一個節點就生成一個節點類的實例。

這里主要實現了單向鏈表節點的遍歷、添加、插入、刪除,反轉。

代碼如下:

class Player:
? ? """節點類"""
? ? def __init__(self):
? ? ? ? """初始化姓名,分數,指針"""
? ? ? ? self.name = ''
? ? ? ? self.score = 0
? ? ? ? self.next = None
?
?
def ergodic(head, num=None, is_print=False):
? ? """遍歷函數,num是遍歷到哪一個位置序號,is_print是否觸發打印方法"""
? ? ptr = head
? ? count = 0
? ? while True:
? ? ? ? if num == count:
? ? ? ? ? ? break
? ? ? ? if ptr.next:
? ? ? ? ? ? count += 1
? ? ? ? ? ? ptr = ptr.next
? ? ? ? ? ? if is_print:
? ? ? ? ? ? ? ? print('No.'+str(count), ptr.name, ptr.score, '--->', ptr.next.name if ptr.next else None)
? ? ? ? else:
? ? ? ? ? ? break
? ? return ptr ?# 返回遍歷完成后的最后一個節點
?
?
def invert(x): ?# x是鏈表的第一個節點(即head的指針的指向)
? ? """反轉鏈表"""
? ? y = x.next ?# y是x原來的next
? ? if y is None: ?# y等于None表示當前鏈表只有一個節點
? ? ? ? return x
? ? x.next = None ?# 將第一個節點的next指向None(因為反轉了)
? ? while True: ?# 循環反轉后面的所有節點
? ? ? ? r = y.next
? ? ? ? y.next = x
? ? ? ? if r is None: ?# r是None說明y已經是原本鏈表的最后一個節點了
? ? ? ? ? ? return y ?# 返回y,這個y是反轉后的鏈表的第一個節點
? ? ? ? x = y
? ? ? ? y = r
?
?
head = Player()
?
?
while True:
? ? select = input("(1).新增 ? (2).查看 ? (3).插入 ? (4).刪除 ? (5).反轉 ? (6).離開\n輸入:")
? ? if select == "1": ?# 新增節點
? ? ? ? ptr = ergodic(head) ?# 獲取當前鏈表最后一個節點
? ? ? ? next_data = Player() ?# 創建一個新節點
? ? ? ? next_data.name = input("姓名:")
? ? ? ? next_data.score = input("分數:")
? ? ? ? next_data.next = None
? ? ? ? ptr.next = next_data ?# 連接節點
? ? ? ??
? ? elif select == "2": ?# 遍歷查看鏈表所有節點
? ? ? ? print("所有數據顯示如下:")
? ? ? ? ergodic(head, is_print=True) ?# 遍歷鏈表,將打印參數設為True
? ? ? ??
? ? elif select == '3': ?# 向鏈表中任意位置插入節點,位置以序號表示,即第一個節點序號為1,第二個節點序號為2,以此類推
? ? ? ? try:
? ? ? ? ? ? num = int(input("請輸入需要插入的節點位置序號:")) ?# 輸入序號必須是大于0的正整數,如果輸入大于最后一個節點的序號則插入到最后一個節點之后
? ? ? ? ? ? if num < 1:
? ? ? ? ? ? ? ? print("輸入必須為大于0的正整數")
? ? ? ? ? ? ? ? continue
? ? ? ? except ValueError:
? ? ? ? ? ? print("輸入有誤")
? ? ? ? ? ? continue
? ? ? ? ptr = ergodic(head, num-1) ?# 獲取需要插入位置的前一個節點
? ? ? ? insert_data = Player()
? ? ? ? insert_data.name = input("姓名:")
? ? ? ? insert_data.score = input("分數:")
? ? ? ? insert_data.next = ptr.next
? ? ? ? ptr.next = insert_data
? ? ? ??
? ? elif select == '4': ?# 刪除鏈表中任意位置的節點
? ? ? ? try:
? ? ? ? ? ? num = int(input("請輸入需要刪除的節點位置序號:")) ?# 輸入序號必須是大于0的正整數,如果輸入大于最后一個節點的序號則刪除最后一個節點
? ? ? ? ? ? if num < 1:
? ? ? ? ? ? ? ? print("輸入必須為大于0的正整數")
? ? ? ? ? ? ? ? continue
? ? ? ? except ValueError:
? ? ? ? ? ? print("輸入有誤")
? ? ? ? ? ? continue
? ? ? ? ptr = ergodic(head, num - 1) ?# 獲取需要刪除位置的前一個節點
? ? ? ? ptr.next = ptr.next.next
? ? ? ??
? ? elif select == '5': ?# 反轉鏈表
? ? ? ? new_first = invert(head.next) ?# 獲取新的第一個節點
? ? ? ? head.next = new_first ?# head指向新的第一個節點
? ? ? ? print('成功反轉')
? ? ? ??
? ? elif select == '6':
? ? ? ? print("成功離開")
? ? ? ? break
? ? else:
? ? ? ? print("輸入錯誤,請重試")

部分運行結果:

原文鏈接:https://blog.csdn.net/heibuliuqiu_gk/article/details/102641541

欄目分類
最近更新