Python实现双向链表基本操作

作者:蕾欧娜等等 时间:2022-07-22 10:14:12 

双向链表的基本操作的实现,供大家参考,具体内容如下

在之前的博客中介绍了三种链表,分别是单链表、单向循环链表以及双向链表。本篇博客将用Python来实现双向链表的如下操作。(用到的工具是Python 3)

is_empty() : 判断链表是否为空
length() : 返回链表的长度
travel() : 遍历
add(item) : 在头部添加一个节点
append(item) : 在尾部添加一个节点
insert(pos, item) : 在指定位置 pos 添加一个节点
remove(item) :  删除一个节点
search(item) :  查找节点是否存在

Python实现

class Node(object):
    '''双向链表节点'''
    def __init__(self,item):
        self.item = item
        self.next = None
        self.prev = None
class DoubleLink(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指向None
            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():
            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

指定位置插入节点

在该操作中,要注意链的指向的先后顺序。

Python实现双向链表基本操作

def insert(self, pos, item):
        '''在指定位置添加节点'''
        if pos <= 0:
            self.add(item)
        elif pos > (self.length() - 1):
            self.append(item)
        else:
            node = Node()
            cur = self._head()
            count = 0
            # 移动到指定的前一个位置
            while cur < 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

删除元素

Python实现双向链表基本操作

def remove(self, item):
        '''删除元素'''
        if self.is_empty(): return 
        else:
            cur = self._head
            if cur.item == item:
                # 如果首节点的元素是要删除的元素
                if cur.next == None:
                    # 如果链表中只有一个节点
                    self._head = None
                else:
                    cur.next.prev = None
                    self._head = cur.next
                return
            while cur != None:
                if cur.item == item:
                    cur.prev.next = cur.next
                    cur.next.prev = cur.prev
                    break
                cur = cur.next

来源:https://blog.csdn.net/weixin_38746310/article/details/106598545

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

猜你喜欢

  • MySQL修改默认字符集

    2010-11-02 12:11:00
  • 通过索引优化含ORDER BY的MySQL语句

    2010-03-13 12:20:00
  • php网络安全中命令执行漏洞的产生及本质探究

    2023-05-30 05:34:31
  • python的pip安装以及使用教程

    2022-12-05 11:04:37
  • Python采集图片数据的实现示例

    2023-03-06 05:53:17
  • python PyTorch参数初始化和Finetune

    2023-04-26 08:53:44
  • PyTorch开源图像分类工具箱MMClassification详解

    2023-11-21 02:20:06
  • python实现获取单向链表倒数第k个结点的值示例

    2022-10-12 17:38:10
  • python搭建服务器实现两个Android客户端间收发消息

    2022-05-24 12:21:12
  • python requests.post带head和body的实例

    2022-07-12 01:22:59
  • python anaconda 安装 环境变量 升级 以及特殊库安装的方法

    2022-11-05 01:56:24
  • 在Python中进行自动化单元测试的教程

    2023-07-16 04:12:30
  • 利用Python脚本实现自动刷网课

    2023-04-20 12:39:16
  • Python流程控制 while循环实现解析

    2023-02-07 04:16:33
  • Python实现合并两个有序链表的方法示例

    2023-04-02 22:20:46
  • CentOS 6.6服务器编译安装lnmp(Nginx1.6.2+MySQL5.6.21+PHP5.6.3)

    2023-11-15 06:40:50
  • python傅里叶变换FFT绘制频谱图

    2023-06-30 09:44:07
  • Python 错误和异常代码详解

    2022-02-12 15:14:08
  • Python地理地图可视化folium标记点弹窗设置代码(推荐)

    2022-08-18 17:02:57
  • Python3利用SMTP协议发送E-mail电子邮件的方法

    2023-10-12 17:39:32
  • asp之家 网络编程 m.aspxhome.com