Python實現(xiàn)針對給定單鏈表刪除指定節(jié)點(diǎn)的方法
本文實例講述了Python實現(xiàn)針對給定單鏈表刪除指定節(jié)點(diǎn)的方法。分享給大家供大家參考,具體如下:
題目:
初始化定義一個單鏈表,刪除指定節(jié)點(diǎn),輸出鏈表
下面是具體的實現(xiàn):
#!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:給定一個單鏈表刪除指定節(jié)點(diǎn) ''' class Node(object): ''''' 節(jié)點(diǎn)類 ''' def __init__(self,data): self.num=data self.next=None class DeleteNode(): ''''' 實現(xiàn)刪除指定節(jié)點(diǎn)功能 ''' def delete_node(self,node): node.num=node.next.num node.next=node.next.next class PrintNode(): ''''' 輸出指定節(jié)點(diǎn)為起始節(jié)點(diǎn)的鏈表 ''' def print_node(self,node): res_list=[] while node: res_list.append(str(node.num)) node=node.next print '->'.join(res_list) if __name__ == '__main__': node1=Node(90) node2=Node(34) node3=Node(89) node4=Node(77) node5=Node(23) node1.next=node2 node2.next=node3 node3.next=node4 node4.next=node5 print 'init single linknode is:' printnode=PrintNode() printnode.print_node(node1) delete=DeleteNode() delete.delete_node(node4) print 'after delete node,the single linknode is:' printnode.print_node(node1)
結(jié)果如下:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Pytorch模型遷移和遷移學(xué)習(xí),導(dǎo)入部分模型參數(shù)的操作
這篇文章主要介紹了Pytorch模型遷移和遷移學(xué)習(xí),導(dǎo)入部分模型參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03python client使用http post 到server端的代碼
python client使用 http post 到server端的代碼,供大家學(xué)習(xí)參考2013-02-02Python爬取數(shù)據(jù)保存為Json格式的代碼示例
今天小編就為大家分享一篇關(guān)于Python爬取數(shù)據(jù)保存為Json格式的代碼示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04django 按時間范圍查詢數(shù)據(jù)庫實例代碼
這篇文章主要介紹了django 按時間范圍查詢數(shù)據(jù)庫實例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02Python socket套接字實現(xiàn)C/S模式遠(yuǎn)程命令執(zhí)行功能案例
這篇文章主要介紹了Python socket套接字實現(xiàn)C/S模式遠(yuǎn)程命令執(zhí)行功能,涉及Python socket套接字編寫服務(wù)器/客戶機(jī)模式數(shù)據(jù)傳輸相關(guān)操作技巧,需要的朋友可以參考下2018-07-07