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é)點(diǎn)
def head_insert(self,newdata):
Newdata=Node(newdata)
Newdata.nextval=self.headval
self.headval=Newdata
# 表尾插入結(jié)點(diǎn)
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
# 在兩個(gè)數(shù)據(jù)結(jié)點(diǎn)之間插入
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é)點(diǎn)
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鏈表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python數(shù)據(jù)結(jié)構(gòu)之遞歸可視化詳解
遞歸函數(shù)是直接調(diào)用自己或通過一系列語句間接調(diào)用自己的函數(shù)。遞歸在程序設(shè)計(jì)有著舉足輕重的作用,在很多情況下,借助遞歸可以優(yōu)雅的解決問題。本文主要介紹了如何利用可視化方式來了解遞歸函數(shù)的執(zhí)行步驟,需要的可以參考一下2022-04-04
Python中Array特性與應(yīng)用實(shí)例深入探究
這篇文章主要為大家介紹了Python中Array特性與應(yīng)用實(shí)例深入探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
OpenCV圖像識(shí)別之姿態(tài)估計(jì)Pose?Estimation學(xué)習(xí)
這篇文章主要為大家介紹了OpenCV圖像識(shí)別之姿態(tài)估計(jì)Pose?Estimation學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Python數(shù)據(jù)分析之獲取雙色球歷史信息的方法示例
這篇文章主要介紹了Python數(shù)據(jù)分析之獲取雙色球歷史信息的方法,涉及Python網(wǎng)頁抓取、正則匹配、文件讀寫及數(shù)值運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
python實(shí)現(xiàn)簡單socket程序在兩臺(tái)電腦之間傳輸消息的方法
這篇文章主要介紹了python實(shí)現(xiàn)簡單socket程序在兩臺(tái)電腦之間傳輸消息的方法,涉及Python操作socket的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
使用python對(duì)文件中的數(shù)值進(jìn)行累加的實(shí)例
今天小編就為大家分享一篇使用python對(duì)文件中的數(shù)值進(jìn)行累加的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
原理解析為什么pydantic可變對(duì)象沒有隨著修改而變化
這篇文章主要介紹了為什么pydantic可變對(duì)象沒有隨著修改而變化的原因解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

