python实现链表(一)
單鏈表,是一種鏈?zhǔn)酱鎯Φ臄?shù)據(jù)結(jié)構(gòu),每個數(shù)據(jù)中除了有保存的元素本身的數(shù)據(jù),還有一個指針,這個指針指向鏈表里的下一個元素。
class nodes:# 初始化鏈表時,傳入?yún)?shù),這里第一個參數(shù)是鏈表的值,也可以說是保存的數(shù)據(jù),可以是一個,也可以是多個def __init__(self, node_no: int, node_name):# 下一個節(jié)點(diǎn)self.next = Noneself.node_no = node_noself.node_name = node_name這里每個節(jié)點(diǎn)有三個數(shù)據(jù),一個是指針,一個是節(jié)點(diǎn)編號和節(jié)點(diǎn)姓名。
# 單向鏈表的實現(xiàn),管理鏈表里的元素 class single_linked_list:# 初始化一個頭節(jié)點(diǎn),傳入的索引為空__head = nodes(0, '')這里的單向鏈表有一個頭結(jié)點(diǎn),這個頭結(jié)點(diǎn)是用來指向下一個節(jié)點(diǎn)的,本身不保存任何信息。
接下來,添加節(jié)點(diǎn),我這里實現(xiàn)了兩種添加方式,一種是無序添加,一種是按照node_no的大小添加。
無序添加節(jié)點(diǎn)
# 首先,找到最后一個節(jié)點(diǎn) # 第二,將最后一個節(jié)點(diǎn)的索引指向新的節(jié)點(diǎn) # 無序添加元素 def add(self, node: nodes):head = self.__headwhile True:# 如果鏈表索引為空,則退出循環(huán)if head.next == None:breakhead = head.next# 為鏈表尾部的索引賦值head.next = node有序添加節(jié)點(diǎn)
# 根據(jù)第一個參數(shù)的大小來添加元素 def add_by_order(self, node: nodes):temp = self.__head# 判斷添加的節(jié)點(diǎn)是否存在flag = False#考慮如果添加的元素是第一個則直接添加進(jìn)鏈表中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('編號已經(jīng)存在:' + str(temp.next.node_no))else:# 先將傳入的node里的索引指向temp.next,然后將temp.next里的索引指向nodenode.next = temp.nexttemp.next = node那么,添加元素就結(jié)束了,測試一下,寫一個展示鏈表所有元素的方法
# 顯示鏈表,遍歷一次 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()編號已經(jīng)存在:2
1 徐一
2 劉二
3 張三
4 李四
沒問題,接下來繼續(xù),顯示一下頭部元素
# 顯示頭部元素數(shù)據(jù) def show_head(self):if self.__head.next == None:print('鏈表為空。。。')# 注意,頭結(jié)點(diǎn)是沒有數(shù)據(jù)的,僅用來指向鏈表里的第一個元素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('沒有找到此編號的人物') # 根據(jù)編號刪除節(jié)點(diǎn)操作 def remove_node(self, node_no: int):temp = self.__headflag = Falseif temp.next == None:print('鏈表為空')returnwhile True:# 查找下一個節(jié)點(diǎn)是否和傳入的節(jié)點(diǎn)相同if temp.next.node_no == node_no:flag = Truebreakelif temp.next == None: breaktemp = temp.nextif flag:# 將后面的數(shù)據(jù)刪除temp.next = temp.next.nextelse:print('沒有找到節(jié)點(diǎn)')測試
# 修改編號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
沒有問題,結(jié)束
總結(jié)
以上是生活随笔為你收集整理的python实现链表(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是EMC?
- 下一篇: 音乐对计算机专业的影响,计算机网络技术对