python 线性表——双向链表

# 链表的有关操作
# 双向链表


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

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

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

    def length(self):
        # 链表长度
        cur = self.__head
        cnt = 0
        while cur is not None:
            cnt += 1
            cur = cur.next
        return cnt

    def travel(self):
        # 遍历整个列表
        cur = self.__head
        while cur is not None:
            print(cur.elem, end=' ')
            cur = cur.next
        print()

    def add(self, item):
        # 链表头不添加元素
        node = Node(item)
        if self.is_empty():
            self.__head = node
        else:
            node.next = self.__head
            node.next.prev = node  # 原头结点的prev 指向新的结点
            # prev 的指向也可以有 self.__head.prev = node
            self.__head = node

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

    def insert(self, pos, item):
        # 指定位置添加元素
        #  :param pos 从0开始
        if pos <= 0:
            self.add(item)
        elif pos > (self.length() - 1):
            self.append(item)
        else:
            node = Node(item)
            cnt = 1
            cur = self.__head
            while cnt is not pos:
                cnt += 1
                cur = cur.next
                # 当循环退出后 cur 指向pos-1
            node.next = cur.next
            node.prev = cur
            cur.next.prev = node
            cur.next = node

    def remove(self, item):
        # 删除节点
        cur = self.__head
        while cur is not None:
            if cur.elem == item:
                # 先判断此节点是否为头节点
                # 头节点
                if cur == self.__head:
                    self.__head = cur.next
                    if cur.next:
                        # 判断的是该双向链表只有一个结点 时,
                        #  cur.next == None, 不存在 cur.next.prev 的条件,
                        cur.next.prev = None  # 或者 cur.next.prev = cur.prev
                else:
                    cur.prev.next = cur.next
                    if cur.next:
                        # 判断目标结点是否为尾结点
                        cur.next.prev = cur.prev
                return
            else:
                cur = cur.next

    def search(self, item):
        # 查找节点是否存在
        if self.is_empty():
            return False
        cur = self.__head
        res = False
        while cur is not None:
            if cur.elem == item:
                res = True
                break
            else:
                cur = cur.next
        return res



if __name__ == "__main__":
    dll = DoubleLinkList()
    print(dll.is_empty())
    print(dll.length())
    dll.add(1)
    dll.travel()
    dll.append(3)
    dll.travel()
    dll.insert(0, 0)
    dll.travel()
    dll.insert(100, 4)
    dll.travel()
    print(dll.search(4))
    dll.remove(1)
    dll.add(-1)
    dll.travel()
    print(dll.is_empty())
    print(dll.length())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江某指点迷津

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值