python数据结构与算法:双向链表

双向链表:

######################  P4.13-P4. 双向链表  ###########################
# import singlelinkList

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

# class DoublelinkList(singlelinkList): #继承
class DoublelinkList(object):
    def __init__(self, node = None):
        self.__head = node

    def is_empty(self):
        return  self.__head is None

    def travel(self):
        cur = self.__head
        while cur.next != None:
            print(cur.elem, end=" ")
            cur= cur.next
        print("")

    def length(self):
        """length of list 遍历 借助 Cursor 指针或者游标计数"""
        cur = self.__head
        count = 0
        while cur != None:# cur.next = None
              count+=1
              cur = cur.next
        return count

    def add(self, item):
        node = Node(item)
        node.next = self.__head
        self.__head = node
        node.next.prev = node

    def append(self, item):
        node = Node(item)  # node 实例化
        if self.is_empty():
            self._head = node
        else:
            cur = self.__head
            while cur.next != None:
                cur = cur.next
            cur.next = node
            node.prev = cur

    def insert(self, pos, item):
        """:param pos  从0 开始 """
        if pos < 0:
            self.add(item)
        elif pos > (self.length() - 1):
            self.append(item)
        else:
            cur = self.__head
            count =0
            while (count < pos):
                count += 1
                cur = cur.next
            node = Node(item)
            node.next = cur
            node.prev = cur.prev
            cur.prev = node
            cur.prev.next = node

    def remove(self, item):
        cur = self.__head
        while cur != None:
            if cur.elem == item:
                if cur == self.__head:
                   self.__head = cur.next
                   if cur.next:
                      cur.next.prev = None
                else:
                    cur.prev.next = cur.next
                    if cur.next:
                       cur.next.prev = cur.prev
                break
            else:
                pre = cur
                cur = cur.next

    def search(self, item):
        cur = self.__head
        while cur != None:
            if cur.elem == item:
                return True
            else:
                cur = cur.next
        return False


if __name__ =="__main__":
    ll = DoublelinkList()
    print(ll.is_empty())
    print(ll.length())
    ll.append(1)
    print(ll.is_empty())
    print(ll.length())

    ll.append(2)
    # ll.add(8)
    ll.append(3)
    ll.append(4)
    ll.append(5)
    ll.append(6)
    ll.insert(-1, 9)
    ll.insert(3, 100)
    ll.insert(8, 1234)
    ll.travel()
### 9 8 1 3 119 1 3 1234
    ll.remove(1234)
    ll.travel()

 

©️2020 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值