Python實現(xiàn)雙向鏈表
之前寫的單向鏈表和環(huán)形鏈表都只是單向的,只能單向遍歷,不能根據(jù)后面的節(jié)點獲取前面的節(jié)點,除非進行反轉(zhuǎn)操作。
雙向鏈表每個節(jié)點都有兩個指針,這兩個指針分別指向前后兩個節(jié)點,這樣就可以從任意一個節(jié)點從兩個方向獲取其他的所有節(jié)點,非常方便。但是由于每個節(jié)點有兩個指針,所以雙向鏈表比較消耗空間。
在設計雙向鏈表時,通常會加上一個鏈表頭指針,該鏈表頭指針的數(shù)據(jù)字段不存放任何數(shù)據(jù)。
雙向鏈表的可以是環(huán)形的,也可以不是環(huán)形的,如果是環(huán)形的話,那么最后一個節(jié)點的一個指針將指向鏈表頭,鏈表頭的一個指針將指向最后一個節(jié)點;如果不是環(huán)形的話,那么最后一個節(jié)點的一個指針和鏈表頭的一個指針都將指向None。
我在這里實現(xiàn)的是一個環(huán)形的雙向鏈表,這樣我就可以從鏈表頭開始,從兩個方向中任意選擇一個方向來進行操作。
我在這里主要實現(xiàn)了環(huán)形雙向鏈表的:雙向新增,雙向遍歷,雙向插入,雙向刪除。
如圖為雙向環(huán)形鏈表示意圖,每一個節(jié)點都被兩個指針所指向,同時每個節(jié)點也指向了兩個節(jié)點。
實現(xiàn)代碼如下:
class Player: ? ? """節(jié)點類""" ? ? def __init__(self): ? ? ? ? """初始化姓名,分數(shù),指針""" ? ? ? ? self.name = '' ? ? ? ? self.score = 0 ? ? ? ? self.rlink = None ? ? ? ? self.llink = None ? ? def ergodic(head, num=None, is_print=False, left=False): ? ? """遍歷函數(shù),num是遍歷到哪一個位置序號,is_print是否觸發(fā)打印方法,left表示是否由head開始往左遍歷""" ? ? ptr = head ? ? count = 0 ? ? while True: ? ? ? ? if num == count: ? ? ? ? ? ? break ? ? ? ? ? if not left: ? ? ? ? ? ? if ptr.rlink != head: ? ? ? ? ? ? ? ? ptr = ptr.rlink ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? break ? ? ? ? else: ? ? ? ? ? ? if ptr.llink != head: ? ? ? ? ? ? ? ? ptr = ptr.llink ? ? ? ? ? ? else: ? ? ? ? ? ? ? ? break ? ? ? ? count += 1 ? ? ? ? if is_print: ? ? ? ? ? ? print('No.'+str(count), ptr.llink.name if ptr.llink != head else 'head', '<---', ? ? ? ? ? ? ? ? ? ptr.name, ptr.score, '--->', ptr.rlink.name if ptr.rlink != head else 'head') ? ? return ptr ?# 返回遍歷完成后的最后一個節(jié)點 ? ? head = Player() ?# 初始化一個鏈表頭指針,不用來存放任何數(shù)據(jù) head.rlink = head ?# 初始化右指針 head.llink = head ?# 初始化左指針 ? ? while True: ? ? select = input("(1).新增 ? (2).查看 ? (3).插入 ? (4).刪除 ? (5).離開\n輸入:") ? ? if select == "1": ?# 新增節(jié)點,分為右新增和左新增 ? ? ? ? direction = input("(1).右新增 ? (2).左新增\n輸入:") ? ? ? ? if direction not in ("1", "2"): ? ? ? ? ? ? print("輸入錯誤") ? ? ? ? ? ? continue ? ? ? ? new_data = Player() ? ? ? ? new_data.name = input("姓名:") ? ? ? ? new_data.score = input("分數(shù):") ? ? ? ? if direction == "1": ?# 右新增 ? ? ? ? ? ? ptr = ergodic(head) ?# 從head開始向右遍歷獲取最后一個節(jié)點 ? ? ? ? ? ? ptr.rlink = new_data ? ? ? ? ? ? new_data.llink = ptr ? ? ? ? ? ? new_data.rlink = head ? ? ? ? ? ? head.llink = new_data ? ? ? ? else: ?# 左新增 ? ? ? ? ? ? ptr = ergodic(head, left=True) ?# 從head開始向左遍歷獲取最后一個節(jié)點 ? ? ? ? ? ? ptr.llink = new_data ? ? ? ? ? ? new_data.rlink = ptr ? ? ? ? ? ? new_data.llink = head ? ? ? ? ? ? head.rlink = new_data ? ? ? elif select == "2": ?# 遍歷查看所有節(jié)點,分為右遍歷和左遍歷 ? ? ? ? direction = input("(1).右遍歷 ? (2).左遍歷\n輸入:") ? ? ? ? if direction == "1": ?# 右遍歷 ? ? ? ? ? ? ergodic(head, is_print=True) ? ? ? ? elif direction == "2": ?# 左遍歷 ? ? ? ? ? ? ergodic(head, is_print=True, left=True) ? ? ? ? else: ? ? ? ? ? ? print("輸入錯誤") ? ? ? elif select == '3': ?# 插入節(jié)點,分為右插入和左插入 ? ? ? ? direction = input("(1).右插入 ? (2).左插入\n輸入:") ? ? ? ? if direction not in ("1", "2"): ? ? ? ? ? ? print("輸入錯誤") ? ? ? ? ? ? continue ? ? ? ? try: ? ? ? ? ? ? num = int(input("請輸入需要插入的節(jié)點位置序號:")) ?# 輸入序號必須是大于0的正整數(shù),如果輸入大于最后一個節(jié)點的序號則插入到最后一個節(jié)點之后 ? ? ? ? ? ? if num < 1: ? ? ? ? ? ? ? ? print("輸入必須為大于0的正整數(shù)") ? ? ? ? ? ? ? ? continue ? ? ? ? except ValueError: ? ? ? ? ? ? print("輸入有誤") ? ? ? ? ? ? continue ? ? ? ? insert_data = Player() ? ? ? ? insert_data.name = input("姓名:") ? ? ? ? insert_data.score = input("分數(shù):") ? ? ? ? if direction == "1": ?# 右插入 ? ? ? ? ? ? ptr = ergodic(head, num - 1) ?# 獲取需要插入位置的前一個節(jié)點,新插入的節(jié)點就在這個節(jié)點的后面 ? ? ? ? ? ? insert_data.llink = ptr ? ? ? ? ? ? insert_data.rlink = ptr.rlink ? ? ? ? ? ? ptr.rlink = insert_data ? ? ? ? ? ? insert_data.rlink.llink = insert_data ? ? ? ? else: ?# 左插入 ? ? ? ? ? ? ptr = ergodic(head, num - 1, left=True) ? ? ? ? ? ? insert_data.rlink = ptr ? ? ? ? ? ? insert_data.llink = ptr.llink ? ? ? ? ? ? ptr.llink = insert_data ? ? ? ? ? ? insert_data.llink.rlink = insert_data ? ? ? elif select == '4': ?# 刪除節(jié)點,分為右刪除和左刪除 ? ? ? ? direction = input("(1).右刪除 ? (2).左刪除\n輸入:") ? ? ? ? if direction not in ("1", "2"): ? ? ? ? ? ? print("輸入錯誤") ? ? ? ? ? ? continue ? ? ? ? try: ? ? ? ? ? ? num = int(input("請輸入需要刪除的節(jié)點位置序號:")) ?# 輸入序號必須是大于0的正整數(shù),如果輸入大于最后一個節(jié)點的序號則刪除最后一個節(jié)點 ? ? ? ? ? ? if num < 1: ? ? ? ? ? ? ? ? print("輸入必須為大于0的正整數(shù)") ? ? ? ? ? ? ? ? continue ? ? ? ? except ValueError: ? ? ? ? ? ? print("輸入有誤") ? ? ? ? ? ? continue ? ? ? ? if direction == "1": ?# 右刪除 ? ? ? ? ? ? ptr = ergodic(head, num) ?# 獲取需要刪除的節(jié)點 ? ? ? ? else: ?# 左刪除 ? ? ? ? ? ? ptr = ergodic(head, num, left=True) ? ? ? ? ptr.llink.rlink = ptr.rlink ? ? ? ? ptr.rlink.llink = ptr.llink ? ? ? elif select == '5': ? ? ? ? print("成功離開") ? ? ? ? break ? ? else: ? ? ? ? print("輸入錯誤,請重試")
部分運行效果如下:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python Opencv 通過軌跡(跟蹤)欄實現(xiàn)更改整張圖像的背景顏色
這篇文章主要介紹了Python Opencv 通過軌跡(跟蹤)欄實現(xiàn)更改整張圖像的背景顏色,在文章末尾有一個小訓練——是將所學得的圖像顏色修改應用為畫板一般的刷新,需要的朋友可以參考下2020-03-03python 搭建簡單的http server,可直接post文件的實例
今天小編就為大家分享一篇python 搭建簡單的http server,可直接post文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Pytorch使用DataLoader實現(xiàn)批量加載數(shù)據(jù)
這篇文章主要介紹了Pytorch使用DataLoader實現(xiàn)批量加載數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02對python的bytes類型數(shù)據(jù)split分割切片方法
今天小編就為大家分享一篇對python的bytes類型數(shù)據(jù)split分割切片方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12numpy工程實踐之np.savetxt()存儲數(shù)據(jù)
NumPy提供了多種存取數(shù)組內(nèi)容的文件操作函數(shù),保存數(shù)組數(shù)據(jù)的文件可以是二進制格式或者文本格式,下面這篇文章主要給大家介紹了關(guān)于numpy工程實踐之np.savetxt()存儲數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2023-05-05TensorFlow人工智能學習按索引取數(shù)據(jù)及維度變換詳解
這篇文章主要為大家介紹了TensorFlow人工智能學習按索引取數(shù)據(jù)及維度變換的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11