欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)針對(duì)給定單鏈表刪除指定節(jié)點(diǎn)的方法

 更新時(shí)間:2018年04月12日 10:10:51   作者:Together_CZ  
這篇文章主要介紹了Python實(shí)現(xiàn)針對(duì)給定單鏈表刪除指定節(jié)點(diǎn)的方法,結(jié)合實(shí)例形式分析了Python單鏈表的定義、節(jié)點(diǎn)添加、刪除、打印等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)針對(duì)給定單鏈表刪除指定節(jié)點(diǎn)的方法。分享給大家供大家參考,具體如下:

題目:

初始化定義一個(gè)單鏈表,刪除指定節(jié)點(diǎn),輸出鏈表

下面是具體的實(shí)現(xiàn):

#!usr/bin/env python
#encoding:utf-8
'''''
__Author__:沂水寒城
功能:給定一個(gè)單鏈表刪除指定節(jié)點(diǎn)
'''
class Node(object):
  '''''
  節(jié)點(diǎn)類
  '''
  def __init__(self,data):
    self.num=data
    self.next=None
class DeleteNode():
  '''''
  實(shí)現(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)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論