双指针问题--链表--双向链表

双向链表的特性:元素的由他的值和链各个指针组成,左指针指向上一元素, 右指针指向下一元素

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


class DoubleLink(object):
    def __init__(self):
        self.head = None

    def is_empty(self):
        return self.head == None

    def lenght(self):
        cur = self.head
        count = 0
        while cur != None:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        """遍历单链表"""
        if self.is_empty():
            return None
        else:
            cur = self.head
            while cur is not None:
                cur = cur.next

    def add(self, item):
        """向单链表头部添加节点"""
        node = Node(item)
        if self.is_empty():
            self.head = node
        else:
            node.next = self.head
            self.head.prev = node
            self.head = node

    def append(self, item):
        """向单链表尾部添加节点"""
        node = Node(item)
        if self.is_empty():
            self.head = node
        else:
            cur = self.head
            while cur.next is not None:
                cur = cur.next
            cur.next = node
            node.prev = cur

    def insert(self, item, pos):
        if pos >= self.lenght():
            self.append(item)
        elif pos <= 0:
            self.add(item)
        else:
            cur = self.head
            node = Node(item)
            count = 0
            while count < pos - 1:
                count += 1
                cur = cur.next
            node.next = cur.next
            node.prev = cur
            cur.next.prev = node
            cur.next = node

    def search(self, item):
        if self.is_empty():
            return None
        else:
            cur = self.head
            while cur is not None:
                if cur.item == item:
                    return True
                cur = cur.next
            return False

    def remove(self, item):
        if self.is_empty():
            return Node
        else:
            cur = self.head
            if cur.item == item:
                if cur.next is None:
                    self.head = Node
                else:
                    cur.next.prev = None
                    self.head = cur.next
                return True
            while cur is not None:
                if cur.item == item:
                    cur.prev.next = cur.next
                    cur.next.prev = cur.prev
                    break
                cur = cur.next


if __name__ == '__main__':
    dl = DoubleLink()
    print(dl.is_empty())
    print(dl.lenght())
    dl.add(1)
    dl.add(2)
    dl.add(3)
    dl.append(4)
    dl.append(5)
    dl.insert(6, 4)
    dl.insert(7, 5)
    dl.insert(7, 9)
    dl.insert(8, 0)
    dl.remove(8)
    dl.travel()
    print(dl.search(8))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值