双向循环链表(python代码)

双向循环链表(python代码)

# coding:utf-8
class Node():
    def __init__(self,elem):
        self.elem = elem
        self.pre = None
        self.next = None
class DCLL():
    def __init__(self):
        self.__head = None
    def add(self,item):
        node = Node(item)
        if self.__head == None: #空链表,加入第一个结点情况
            self.__head = node
            node.next = self.__head
            node.pre = self.__head
            return True
        elif self.__head.next == self.__head:   #链表只有一个结点,加入第二的结点情况
            node.next = self.__head
            self.__head.pre = node
            node.pre = self.__head
            self.__head.next = node
            self.__head = node
            return True
        else:   #链表有两个及以上结点,加入新的的结点情况
            node.next = self.__head
            node.pre = self.__head.pre
            self.__head.pre = node
            cur = self.__head
            while self.__head != cur.next:
                cur = cur.next
            cur.next = node
            self.__head = node
            return True
        # is_empty() 链表是否为空
    def is_empty(self):
        return self.__head == None
# append(item) 链表尾部添加
    def append(self,item):
        node = Node(item)
        if self.__head == None: #空链表,加入第一个结点情况
            self.__head = node
            node.next = self.__head
            node.pre = self.__head
            return True
        elif self.__head.next == self.__head:   #链表只有一个结点,加入第二的结点情况
            self.__head.next = node
            node.pre = self.__head
            node.next = self.__head
            self.__head.pre = node
            return True
        else:   #链表有两个及以上结点,加入新的的结点情况
            cur = self.__head
            while self.__head != cur.next:
                cur = cur.next
            cur.next = node
            node.pre = cur
            node.next = self.__head
            self.__head.pre = node
            return True
# insert(pos, item) 指定位置添加
    def insert(self,pos,item):
        """
        默认pos =< 0 插入头部,pos >=已有链表长度 插入尾部 ,其余插入中间位置
        """
        node = Node(item)
        if self.__head == None: #空链表,加入第一个结点情况
            self.__head = node
            node.next = self.__head
            node.pre = self.__head
            return True
        elif self.__head.next == self.__head:   #链表只有一个结点,加入第二的结点情况
            if pos <= 0:    #pos =< 0 插入头部
                node.next = self.__head
                self.__head.pre = node
                node.pre = self.__head
                self.__head.next = node
                self.__head = node
                return True
            else:  #pos >= 1 插入尾部
                self.__head.next = node
                node.pre = self.__head
                node.next = self.__head
                self.__head.pre = node
                return True
        else:   #链表有两个及以上结点,加入新的的结点情况
            count = 1   #计算当前链表的长度
            cur_count = self.__head
            while cur_count.next!= self.__head:
                count += 1
                cur_count = cur_count.next
            if pos <= 0:    #pos =< 0 插入头部 (头插法长度大于等于2的情况)
                node.next = self.__head
                node.pre = self.__head.pre
                self.__head.pre = node
                cur = self.__head
                while self.__head != cur.next:
                    cur = cur.next
                cur.next = node
                self.__head = node
                return True
            elif pos >= count:  #pos >= count 插入尾部 (尾插法长度大于等于2的情况)
                cur = self.__head
                while self.__head != cur.next:
                    cur = cur.next
                cur.next = node
                node.pre = cur
                node.next = self.__head
                self.__head.pre = node
                return True
            else:   # 1<= pos <= count-1 的情况,插入中间位置
                cur = self.__head
                count = 0
                while count < pos-1:
                    count += 1
                    cur = cur.next
                cur.next.pre = node
                node.next = cur.next
                node.pre = cur
                cur.next = node
                return True
# remove(item) 删除节点
    def remove(self,item):
        if self.__head == None:  #空链表的情况
            return False
        if self.__head == self.__head.next:  #含有一个元素的情况的情况
            if  self.__head.elem != item:
                return False
            else:
                self.__head = None
                return  True
        if self.__head.elem == item:  #含有两个及以上,首节点为删除点
            if self.__head.pre == self.__head.next:    #含有两个
                self.__head.next.next = self.__head.next
                self.__head.next.pre = self.__head.next
                self.__head = self.__head.next
                # print(self.__head.elem,self.__head.next.elem)
                return True
            else:   #含有两个以上
                self.__head.pre.next = self.__head.next
                self.__head.next.pre = self.__head.pre
                self.__head = self.__head.next
                return True
        else:  #含有两个及以上,非首节点为删除点
            cur = self.__head
            while cur.next != self.__head:  #遍历寻找链表中间的元素是否相等要删除的元素
                if cur.elem != item:    #未找到,遍历下一个元素
                    cur = cur.next
                else:   #找到,进行删除操作
                    cur.next.pre = cur.pre
                    cur.pre.next = cur.next
                    return True
            if cur.elem != item:    #如果链表最后一个元素仍不相同,返回False
                return False
            else:   #链表最后一个元素,执行删除操作
                cur.pre.next = self.__head
                self.__head.pre = cur.pre
                return True
    # travel() 遍历链表
    def travel(self):
        if self.__head == None:#空链表情况
            return False
        cur = self.__head
        while cur != self.__head.pre:   #当前节点没有到达尾结点的情况
            print(cur.elem, end=" ")
            cur = cur.next
        print(cur.elem)
        return True
        # if self.__head == self.__head.next:#一个结点的链表情况
        #     print(self.__head.elem)
        #     return True
        # elif self.__head.pre == self.__head.next:  #含有两个
        #     print(self.__head.elem,self.__head.next.elem)
        #     return True
        # else: #含有两个以上
        #     cur = self.__head
        #     while cur != self.__head.pre:   #当前节点没有到达尾结点的情况
        #         print(cur.elem , end=" ")
        #         cur = cur.next
        #     print(cur.elem)
        #     return True

# length() 链表长度
    def length(self):
        if self.__head == None:
            return 0
        count = 1
        cur = self.__head
        while cur!= self.__head.pre:   #当前节点没有到达尾结点的情况
            count += 1
            cur = cur.next
        return count
# search(item) 查找节点是否存在
    def search(self,item):
        if self.__head == None:
            return False
        cur = self.__head
        while cur != self.__head.pre:   #当前节点没有到达尾结点的情况
            if cur.elem == item:
                return True
            else:
                cur = cur.next
        if cur.elem == item:   #当前节点到达尾结点的情况
            return True
        else:
            return False
dcll = DCLL()
print("search",dcll.search(0))
for i in range(2):
    print(dcll.insert(i,i))
dcll.travel()
print("search",dcll.search(0))
print("remove",dcll.remove(0))
print("search",dcll.search(0))
dcll.travel()
print("length",dcll.length())
print("search",dcll.search(5))
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值