python实现双向循环链表

p y t h o n 实 现 双 向 循 环 链 表 python实现双向循环链表 python

练习三部曲之一

# 1.节点构造
class Node(object):
    def __init__(self, elem):
        self.elem = elem  
        self.next = None  
        self.prev = None  

class DoubleCircleLinkedList(object):
    # 2.成员构造
    def __init__(self, node=None):
        self._head = node
        
    # 3.返回大小
    def length(self):
        if self.is_empty():
            return 0
        else:
            cur = self._head.next
            n = 1
            while cur != self._head:
                cur = cur.next
                n += 1
            return n

    # 4.判空
    def is_empty(self):
        return self._head is None

    # 5.增
    def addFirst(self, elem):
        node = Node(elem)
        if self.is_empty():  # 链表为空
            node.next = node
            node.prev = node
            self._head = node
        else:
            node.next = self._head
            node.prev = self._head.prev
            self._head.prev.next = node
            self._head.prev = node
            self._head = node

    def addLast(self, elem):
        if self.is_empty():
            self.addFirst(elem)
        else:
            node = Node(elem)
            cur = self._head.next
            while cur.next != self._head:
                cur = cur.next
            cur.next = node
            node.prev = cur
            node.next = self._head
            self._head.prev = node

    def add(self, pos, elem):
        if pos <= 0:
            self.addFirst(elem)
        elif pos >= self.length():
            self.addLast(elem)
        else:
            cur = self._head.next
            n = 1
            while cur.next != self._head:
                if n == pos:
                    break
                cur = cur.next
                n += 1
            node = Node(elem)
            node.prev = cur.prev
            cur.prev.next = node
            node.next = cur
            cur.prev = node
            
    # 6.删
    def remove(self, elem):
        if self.is_empty():
            return
        else:
            if self._head.elem == elem:
                if self.length == 1:
                    self._head = Node
                else:
                    self._head.prev.next = self._head.next
                    self._head.next.prev = self._head.prev
                    self._head = self._head.next
            cur = self._head.next
            while cur != self._head:
                if cur.elem == elem:
                    cur.prev.next = cur.next
                    cur.next.prev = cur.prev
                cur = cur.next
                
    # 7.查
    def travel(self):
        cur = self._head
        while cur.next != self._head:
            print(cur.elem, end=' ')
            cur = cur.next
        print(cur.elem)
        
    def search(self, elem):
        if self.is_empty():
            return False
        else:
            cur = self._head.next
            if self._head.elem == elem:
                return True
            else:
                while cur != self._head:
                    if cur.elem == elem:
                        return True
                    else:
                        cur = cur.next
                return False

练习三部曲之二

# 1.节点构造
class Node(object):
    def __init__(self, elem):


class DoubleCircleLinkedList(object):
    # 2.成员构造
    def __init__(self, node=None):

    # 3.返回大小

    # 4.判空
    def is_empty(self):

    # 5.增
    def addFirst(self, elem):

    def addLast(self, elem):

    def add(self, pos, elem):

    # 6.删
    def remove(self, elem):

    # 7.查
    def travel(self):

    def search(self, elem):

练习三部曲之三

# 1.节点构造


class DoubleCircleLinkedList(object):
    # 2.成员构造

    # 3.返回大小

    # 4.判空

    # 5.增



    # 6.删


    # 7.查
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值