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
# 表頭插入結點
def head_insert(self,newdata):
Newdata=Node(newdata)
Newdata.nextval=self.headval
self.headval=Newdata
# 表尾插入結點
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
# 在兩個數據結點之間插入
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
# 刪除結點
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操作鏈表的示例代碼的詳細內容,更多關于Python鏈表的資料請關注腳本之家其它相關文章!
相關文章
如何用Python中Tushare包輕松完成股票篩選(詳細流程操作)
這篇文章主要介紹了如何用Python中Tushare包輕松完成股票篩選(詳細流程操作),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
python numpy中mat和matrix的區(qū)別
這篇文章主要介紹了python numpy中mat和matrix的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python中Windows和macOS文件路徑格式不一致的解決方法
在 Python 中,Windows 和 macOS 的文件路徑字符串格式不一致主要體現在路徑分隔符上,這種差異可能導致跨平臺代碼在處理文件路徑時出錯,下面我們看看如何解決吧2025-03-03
淺談Keras參數 input_shape、input_dim和input_length用法
這篇文章主要介紹了淺談Keras參數 input_shape、input_dim和input_length用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06

