Python实现针对给定单链表删除指定节点的方法

yipeiwu_com5年前Python基础

本文实例讲述了Python实现针对给定单链表删除指定节点的方法。分享给大家供大家参考,具体如下:

题目:

初始化定义一个单链表,删除指定节点,输出链表

下面是具体的实现:

#!usr/bin/env python
#encoding:utf-8
'''''
__Author__:沂水寒城
功能:给定一个单链表删除指定节点
'''
class Node(object):
  '''''
  节点类
  '''
  def __init__(self,data):
    self.num=data
    self.next=None
class DeleteNode():
  '''''
  实现删除指定节点功能
  '''
  def delete_node(self,node):
    node.num=node.next.num
    node.next=node.next.next
class PrintNode():
  '''''
  输出指定节点为起始节点的链表
  '''
  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)

结果如下:

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

常见的python正则用法实例讲解

下面列出Python正则表达式的几种匹配用法: 此外,关于正则的一切http://deerchao.net/tutorials/regex/regex.htm  1.测试正则表...

SublimeText 2编译python出错的解决方法(The system cannot find the file specified)

[Error 2] The system cannot find the file specified 解决方法:1.环境变量path添加:C:\Python32\Tools\Scrip...

Python中logging模块的用法实例

本文实例讲述了logging模块的用法实例,分享给大家供大家参考。具体方法如下: import logging import os log = logging.getLogg...

python中range()与xrange()用法分析

python中range()与xrange()用法分析

本文实例讲述了python中range()与xrange()用法。分享给大家供大家参考,具体如下: 据说range比xrange开销要大,原因是range会直接生成一个list对象,而x...

Python流程控制 while循环实现解析

Python流程控制 while循环实现解析

一、语法 while 条件: 执行代码 while就是当的意思,它指当其后面的条件成立,就执行while下面的代码。 例:写一个从0打印到10的程序 count = 0 whi...