python操作鏈表的示例代碼
class Node: def __init__(self,dataval=None): self.dataval=dataval self.nextval=Noneclass 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# 在兩個數(shù)據(jù)結點之間插入 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=eelis.head_insert(’cc’)lis.tail_insert(’dd’)lis.middle_insert(ee,'Fri')lis.remove_node(’bb’)lis.traversal_slist()
以上就是python操作鏈表的示例代碼的詳細內(nèi)容,更多關于Python鏈表的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. PHP驗證碼工具-Securimage2. Python使用Excel將數(shù)據(jù)寫入多個sheet3. 使用本機IIS?Express開發(fā)Asp.Net?Core應用圖文教程4. 如何用JS實現(xiàn)簡單的數(shù)據(jù)監(jiān)聽5. 解決Docker network Create加--subnet后遇到問題6. JSP動態(tài)網(wǎng)頁開發(fā)原理詳解7. 利用CSS制作3D動畫8. HTML-Canvas的優(yōu)越性能以及實際應用9. HTML iframe標簽用法案例詳解10. 基于Python實現(xiàn)全自動下載抖音視頻

網(wǎng)公網(wǎng)安備