日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python实现链表(一)

發布時間:2023/12/15 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python实现链表(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

單鏈表,是一種鏈式存儲的數據結構,每個數據中除了有保存的元素本身的數據,還有一個指針,這個指針指向鏈表里的下一個元素。

class nodes:# 初始化鏈表時,傳入參數,這里第一個參數是鏈表的值,也可以說是保存的數據,可以是一個,也可以是多個def __init__(self, node_no: int, node_name):# 下一個節點self.next = Noneself.node_no = node_noself.node_name = node_name

這里每個節點有三個數據,一個是指針,一個是節點編號和節點姓名。

# 單向鏈表的實現,管理鏈表里的元素 class single_linked_list:# 初始化一個頭節點,傳入的索引為空__head = nodes(0, '')

這里的單向鏈表有一個頭結點,這個頭結點是用來指向下一個節點的,本身不保存任何信息。

接下來,添加節點,我這里實現了兩種添加方式,一種是無序添加,一種是按照node_no的大小添加。

無序添加節點

# 首先,找到最后一個節點 # 第二,將最后一個節點的索引指向新的節點 # 無序添加元素 def add(self, node: nodes):head = self.__headwhile True:# 如果鏈表索引為空,則退出循環if head.next == None:breakhead = head.next# 為鏈表尾部的索引賦值head.next = node

有序添加節點

# 根據第一個參數的大小來添加元素 def add_by_order(self, node: nodes):temp = self.__head# 判斷添加的節點是否存在flag = False#考慮如果添加的元素是第一個則直接添加進鏈表中if temp.next==None:temp.next=nodereturnwhile True:# 找到位置,在temp的后端插入if temp.next.node_no > node.node_no:breakelif temp.next.node_no == node.node_no:flag = True # 說明編號存在breakif temp.next == None:breaktemp = temp.nextif flag:print('編號已經存在:' + str(temp.next.node_no))else:# 先將傳入的node里的索引指向temp.next,然后將temp.next里的索引指向nodenode.next = temp.nexttemp.next = node

那么,添加元素就結束了,測試一下,寫一個展示鏈表所有元素的方法

# 顯示鏈表,遍歷一次 def show_list(self):if self.__head.next == None:print('鏈表為空。。。')returntemp = self.__head.nextwhile True:# 判斷鏈表是否到尾部if temp == None:breakprint(temp.node_no, temp.node_name)temp = temp.next node1 = nodes(1, '徐一') node2 = nodes(2, '劉二') node3 = nodes(3, '張三') node4 = nodes(4, '李四')

無序添加元素

#不按順序添加 linklist.add(node1) linklist.add(node4) linklist.add(node3) linklist.add(node2) linklist.show_list()

輸出

1 徐一
4 李四
3 張三
2 劉二

Process finished with exit code 0

再測試一下按no添加

linklist.add_by_order(node1) linklist.add_by_order(node4) linklist.add_by_order(node3) linklist.add_by_order(node2) linklist.add_by_order(node2) linklist.show_list()

編號已經存在:2
1 徐一
2 劉二
3 張三
4 李四

沒問題,接下來繼續,顯示一下頭部元素

# 顯示頭部元素數據 def show_head(self):if self.__head.next == None:print('鏈表為空。。。')# 注意,頭結點是沒有數據的,僅用來指向鏈表里的第一個元素print(self.__head.next.node_no, self.__head.next.node_name)

修改和刪除元素也要加上

# 修改鏈表元素 def update_node(self, node_no: int, node_name):temp = self.__headflag = Falseif temp.next == None:print('鏈表為空')returnwhile True:if temp.node_no == node_no:flag = Truebreakelif temp.next == None: breaktemp = temp.nextif flag:temp.node_name = node_nameelse:print('沒有找到此編號的人物') # 根據編號刪除節點操作 def remove_node(self, node_no: int):temp = self.__headflag = Falseif temp.next == None:print('鏈表為空')returnwhile True:# 查找下一個節點是否和傳入的節點相同if temp.next.node_no == node_no:flag = Truebreakelif temp.next == None: breaktemp = temp.nextif flag:# 將后面的數據刪除temp.next = temp.next.nextelse:print('沒有找到節點')

測試

# 修改編號2姓名為胡歌 linklist.update_node(2, '胡歌') linklist.update_node(5,'一眼丁真') linklist.show_list() # 刪除node2 print('刪除node2') linklist.remove_node(2) linklist.show_list() print(linklist.link_length()) print('再次添加node2') linklist.add_by_order(node2) linklist.show_list() print(linklist.link_length())

輸出

刪除node2
1 徐一
3 張三
4 李四
再次添加node2
1 徐一
2 胡歌
3 張三
4 李四

Process finished with exit code 0
沒有問題,結束

總結

以上是生活随笔為你收集整理的python实现链表(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。