python操作鏈表的示例代碼
更新時間:2020年09月27日 14:35:28 作者:HHMLXL
這篇文章主要介紹了python操作鏈表的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
class Node: def __init__(self,dataval=None): self.dataval=dataval self.nextval=None class SLinkList: def __init__(self): self.headval=None # 遍歷列表 def traversal_slist(self): head_node=self.headval while head_node is not None: print(head_node.dataval) head_node=head_node.nextval # 表頭插入結(jié)點 def head_insert(self,newdata): Newdata=Node(newdata) Newdata.nextval=self.headval self.headval=Newdata # 表尾插入結(jié)點 def tail_insert(self,newdata): Newdata=Node(newdata) if self.headval is None: self.headval=Newdata return head_node = self.headval while head_node.nextval : head_node=head_node.nextval head_node.nextval=Newdata # 在兩個數(shù)據(jù)結(jié)點之間插入 def middle_insert(self,middle_node,newdata): Newdata=Node(newdata) if middle_node is None: return Newdata.nextval=middle_node.nextval middle_node.nextval=Newdata # 刪除結(jié)點 def remove_node(self,newdata): head_node=self.headval if head_node==None: return if head_node.dataval == newdata: self.headval = head_node.nextval head_node = None return while head_node is not None: prev=head_node head_node=head_node.nextval if head_node: if head_node.dataval==newdata: prev.nextval=head_node.nextval lis=SLinkList() lis.headval=Node('aa') ee=Node('bb') lis.headval.nextval=ee lis.head_insert('cc') lis.tail_insert('dd') lis.middle_insert(ee,"Fri") lis.remove_node('bb') lis.traversal_slist()
以上就是python操作鏈表的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python鏈表的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python數(shù)據(jù)結(jié)構(gòu)之遞歸可視化詳解
遞歸函數(shù)是直接調(diào)用自己或通過一系列語句間接調(diào)用自己的函數(shù)。遞歸在程序設(shè)計有著舉足輕重的作用,在很多情況下,借助遞歸可以優(yōu)雅的解決問題。本文主要介紹了如何利用可視化方式來了解遞歸函數(shù)的執(zhí)行步驟,需要的可以參考一下2022-04-04OpenCV圖像識別之姿態(tài)估計Pose?Estimation學(xué)習(xí)
這篇文章主要為大家介紹了OpenCV圖像識別之姿態(tài)估計Pose?Estimation學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Python數(shù)據(jù)分析之獲取雙色球歷史信息的方法示例
這篇文章主要介紹了Python數(shù)據(jù)分析之獲取雙色球歷史信息的方法,涉及Python網(wǎng)頁抓取、正則匹配、文件讀寫及數(shù)值運算等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02python實現(xiàn)簡單socket程序在兩臺電腦之間傳輸消息的方法
這篇文章主要介紹了python實現(xiàn)簡單socket程序在兩臺電腦之間傳輸消息的方法,涉及Python操作socket的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03使用python對文件中的數(shù)值進(jìn)行累加的實例
今天小編就為大家分享一篇使用python對文件中的數(shù)值進(jìn)行累加的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11