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# 表頭插入結(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=eelis.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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼3. android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解4. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法5. 淺談python出錯(cuò)時(shí)traceback的解讀6. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解7. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法8. Nginx+php配置文件及原理解析9. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析
