Python實現(xiàn)針對給定單鏈表刪除指定節(jié)點的方法
本文實例講述了Python實現(xiàn)針對給定單鏈表刪除指定節(jié)點的方法。分享給大家供大家參考,具體如下:
題目:
初始化定義一個單鏈表,刪除指定節(jié)點,輸出鏈表
下面是具體的實現(xiàn):
#!usr/bin/env python
#encoding:utf-8
'''''
__Author__:沂水寒城
功能:給定一個單鏈表刪除指定節(jié)點
'''
class Node(object):
'''''
節(jié)點類
'''
def __init__(self,data):
self.num=data
self.next=None
class DeleteNode():
'''''
實現(xiàn)刪除指定節(jié)點功能
'''
def delete_node(self,node):
node.num=node.next.num
node.next=node.next.next
class PrintNode():
'''''
輸出指定節(jié)點為起始節(jié)點的鏈表
'''
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īng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
使用pandas模塊讀取csv文件和excel表格,并用matplotlib畫圖的方法
今天小編就為大家分享一篇使用pandas模塊讀取csv文件和excel表格,并用matplotlib畫圖的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
用 Django 開發(fā)一個 Python Web API的方法步驟
這篇文章主要介紹了用 Django 開發(fā)一個 Python Web API的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
python 實現(xiàn)創(chuàng)建文件夾和創(chuàng)建日志文件的方法
這篇文章主要介紹了python 實現(xiàn)創(chuàng)建文件夾和創(chuàng)建日志文件的方法,文中給大家介紹了python 讀寫創(chuàng)建文件文件夾的方法 ,需要的朋友可以參考下2019-07-07
python如何使用socketserver模塊實現(xiàn)并發(fā)聊天
這篇文章主要介紹了python如何使用socketserver模塊實現(xiàn)并發(fā)聊天,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12

