Python教程

python双向链表

本文主要是介绍python双向链表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一种更复杂的链表是“双向链表”或“双面链表”。每个节点有两个链接:一个指向前一个节点,当此节点为第一个节点时,指向空值;而另一个指向下一个节点,当此节点为最后一个节点时,指向空值。
在这里插入图片描述
操作

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

实现:

class Node(object):
    """结点"""

    def __init__(self, elem):
        self.elem = elem
        self.next = None
        self.prev = None


class DoubleLinkList(object):
    """双向链表"""

    def __init__(self, node=None):
        self.__head = node

    def is_empty(self):
        """判断链表是否为空"""
        return self.__head is None

    def length(self):
        """链表长度"""
        cursor = self.__head
        count = 0
        while cursor is not None:
            count += 1
            cursor = cursor.next
        return count

    def travel(self):
        """遍历链表"""
        cursor = self.__head
        while cursor is not None:
            print(cursor.elem, end=' ')
            cursor = cursor.next

    def add(self, elem):
        """链表头部添加元素"""
        node = Node(elem)
        node.next = self.__head
        self.__head.prev = node
        self.__head = node

    def append(self, elem):
        """链表尾部添加元素"""
        node = Node(elem)
        if self.is_empty():
            self.__head = node
        else:
            cursor = self.__head
            while cursor.next is not None:
                cursor = cursor.next
            cursor.next = node
            node.prev = cursor

    def insert(self, pos, elem):
        """在指定位置插入元素"""
        node = Node(elem)
        if pos <= 0:
            self.add(elem)
        elif pos > (self.length() - 1):
            self.append(elem)
        else:
            cursor = self.__head
            count = 0
            while count < pos:
                count += 1
                cursor = cursor.next
            node.next = cursor
            node.prev = cursor.prev
            cursor.prev.next = node
            cursor.prev = node

    def remove(self, elem):
        """删除元素"""
        cursor = self.__head
        while cursor is not None:
            if cursor.elem == elem:
                if cursor == self.__head:
                    self.__head = cursor.next
                    if cursor.next is not None:
                        cursor.next.prev = None
                    break
                else:
                    cursor.prev.next = cursor.next
                    if cursor.next is not None:
                        cursor.next.prev = cursor.prev
                    break
            else:
                cursor = cursor.next

    def search(self, elem):
        """查找元素"""
        cursor = self.__head
        while cursor is not None:
            if cursor.elem == elem:
                return True
            else:
                cursor = cursor.next
        else:
            return False


if __name__ == "__main__":
    link1 = DoubleLinkList()
    print(link1.is_empty())
    print(link1.length())
    link1.append(1)
    print(link1.is_empty())
    print(link1.length())
    link1.append(2)
    link1.append(3)
    link1.add(6)
    link1.append(4)
    link1.insert(1, 7)
    link1.append(5)
    link1.insert(-1, 8)
    link1.insert(10, 9)
    link1.travel()
    print()
    link1.remove(9)
    link1.travel()
    print()
    link1.remove(8)
    link1.travel()

这篇关于python双向链表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!