首页 > 编程 > Python > 正文

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

2019-11-25 14:56:52
字体:
来源:转载
供稿:网友

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

题目:

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

下面是具体的实现:

#!usr/bin/env python#encoding:utf-8'''''__Author__:沂水寒城功能:给定一个单链表删除指定节点'''class Node(object):  '''''  节点类  '''  def __init__(self,data):    self.num=data    self.next=Noneclass DeleteNode():  '''''  实现删除指定节点功能  '''  def delete_node(self,node):    node.num=node.next.num    node.next=node.next.nextclass 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程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表