zoukankan      html  css  js  c++  java
  • Python实现双链表

    双向链表(Double_linked_list)也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。

    这里直接给出参考代码,大家有兴趣请自行探索,这里就不再详细介绍。

    /home/shiyanlou/下新建一个文件double_linked_list.py

    具体实现代码如下:

    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 get_length(self):
            # 返回链表的长度
            cur = self._head
            count = 0
            while cur != None:
                count=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):
            # 删除元素
            if self.is_empty():
                return
            else:
                cur = self._head
                if cur.item == item:
                    # 如果首节点的元素即是要删除的元素
                    if cur.next == None:
                        # 如果链表只有这一个节点
                        self._head = None
                    else:
                        # 将第二个节点的prev设置为None
                        cur.next.prev = None
                        # 将_head指向第二个节点
                        self._head = cur.next
                    return
                while cur != None:
                    if cur.item == item:
                        # 将cur的前一个节点的next指向cur的后一个节点
                        cur.prev.next = cur.next
                        # 将cur的后一个节点的prev指向cur的前一个节点
                        cur.next.prev = cur.prev
                        break
                    cur = cur.next
    

      

  • 相关阅读:
    编译freeglut-3.0.0的Windows版静态库,及其使用示例
    关于网络安全与实名上网的通知
    【机器学习库】mlpack 2.1.1 Ubuntu14.0.4 踩坑记录
    【OpenCV】在Linux下用CMAKE编译安装OpenCV3.2.0
    【Python装饰者】在函数测试的作用
    【校验】TCP和UDP的校验和
    【C++设计模式】单件类与DCLP(Double Check Lock Pattern)的风险
    【爬虫】Python2 爬虫初学笔记
    【统计学习】SVM之超平面方程来源
    【滤波】标量Kalman滤波的过程分析和证明及C实现
  • 原文地址:https://www.cnblogs.com/MasterMonkInTemple/p/11363283.html
Copyright © 2011-2022 走看看