python双向链表原理与实现方法详解

作者:xlengji 时间:2021-03-06 05:29:39 

本文实例讲述了python双向链表原理与实现方法。分享给大家供大家参考,具体如下:

双向链表

一种更复杂的链表是“双向链表”或“双面链表”。每个节点有两个链接:一个指向前一个节点,当此节点为第一个节点时,指向空值;而另一个指向下一个节点,当此节点为最后一个节点时,指向空值。

操作

  • is_empty() 链表是否为空

  • length() 链表长度

  • travel() 遍历链表

  • add(item) 链表头部添加

  • append(item) 链表尾部添加

  • insert(pos, item) 指定位置添加

  • remove(item) 删除节点

  • search(item) 查找节点是否存在

实现


class Node(object):
 """双向链表节点"""
 def __init__(self, item):
   self.item = item
   self.next = None
   self.prev = None
class DLinkList(object):
 """双向链表"""
 def __init__(self):
   self.__head = None
 def is_empty(self):
   """判断链表是否为空"""
   return self.__head == None
 def length(self):
   """返回链表的长度"""
   cur = self.__head
   count = 0
   while cur != None:
     count += 1
     cur = cur.next
   return count
 def travel(self):
   """遍历链表"""
   cur = self.__head
   while cur != None:
     print cur.item,
     cur = cur.next
   print ""
 def add(self, item):
   """头部插入元素"""
   node = Node(item)
   if self.is_empty():
     # 如果是空链表,将_head指向node
     self.__head = node
   else:
     # 将node的next指向_head的头节点
     node.next = self.__head
     # 将_head的头节点的prev指向node
     self.__head.prev = node
     # 将_head 指向node
     self.__head = node
 def append(self, item):
   """尾部插入元素"""
   node = Node(item)
   if self.is_empty():
     # 如果是空链表,将_head指向node
     self.__head = node
   else:
     # 移动到链表尾部
     cur = self.__head
     while cur.next != None:
       cur = cur.next
     # 将尾节点cur的next指向node
     cur.next = node
     # 将node的prev指向cur
     node.prev = cur
 def search(self, item):
   """查找元素是否存在"""
   cur = self.__head
   while cur != None:
     if cur.item == item:
       return True
     cur = cur.next
   return False

指定位置插入节点


 def insert(self, pos, item):
   """在指定位置添加节点"""
   if pos <= 0:
     self.add(item)
   elif pos > (self.length()-1):
     self.append(item)
   else:
     node = Node(item)
     cur = self.__head
     count = 0
     # 移动到指定位置的前一个位置
     while count < (pos-1):
       count += 1
       cur = cur.next
     # 将node的prev指向cur
     node.prev = cur
     # 将node的next指向cur的下一个节点
     node.next = cur.next
     # 将cur的下一个节点的prev指向node
     cur.next.prev = node
     # 将cur的next指向node
     cur.next = node

删除元素


 def remove(self, item):
   """删除元素"""
   cur = self.__head
   while cur != None:
     # 找到了要删除的元素
     if cur.item == item:
       # 先判断此结点是否是头节点
       # 头节点
       if cur == self.__head:
         self.__head = cur.next
         # 如果存在下一个结点,则设置下一个结点
         if cur.next:
           # 判断链表是否只有一个结点
           cur.next.prev = None
       else:
         cur.prev.next = cur.next
         # 如果存在下一个结点,则设置下一个结点
         if cur.next:
           cur.next.prev = cur.prev
       break
     else:
       cur = cur.next

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

来源:https://blog.csdn.net/xlengji/article/details/82143101

标签:python,双向链表
0
投稿

猜你喜欢

  • jQuery中文入门教程

    2007-12-09 19:20:00
  • pytorch 获取层权重,对特定层注入hook, 提取中间层输出的方法

    2021-05-20 07:01:01
  • Python3中小括号()、中括号[]、花括号{}的区别详解

    2022-05-31 09:20:36
  • jupyter实现重新加载模块

    2023-12-16 20:46:45
  • 相同记录行如何取最大值

    2008-07-26 12:32:00
  • Python中字符串List按照长度排序

    2023-11-28 21:43:02
  • PHP global全局变量经典应用与注意事项分析【附$GLOBALS用法对比】 <font color=red>原创</font>

    2023-11-16 07:37:55
  • python爬虫爬取股票的北上资金持仓数据

    2023-11-18 20:44:56
  • Jquery实现div模拟Select控件

    2008-12-01 15:47:00
  • 整理Python最基本的操作字典的方法

    2022-03-01 07:04:38
  • display:inline-block的深入理解

    2007-05-11 17:03:00
  • 怎样修改 MySQL数据库中的密码

    2008-11-27 15:35:00
  • Jquery中Ajax 缓存带来的影响的解决方法

    2011-05-21 16:14:00
  • Python中删除文件的几种方法实例

    2021-02-02 05:57:13
  • 如何使用SQL Server中的客户端配置工具

    2009-01-13 14:05:00
  • Python爬取豆瓣数据实现过程解析

    2022-01-27 09:12:20
  • OpenCV 图像梯度的实现方法

    2023-07-14 08:25:43
  • 编写python程序的90条建议

    2022-04-30 00:36:22
  • 有用的SQL语句(删除重复记录,收缩日志)

    2008-03-04 16:59:00
  • 深入浅析Python代码规范性检测

    2023-08-23 21:16:55
  • asp之家 网络编程 m.aspxhome.com