python核心编程:双向链表

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

class DLinkList(object):
        def __init__(self):
            self.__head = None
        #判断空列表
        def is_empty(self):
            return self.__head is None
        #长度
        def length(self):
            cur = self.__head
            count = 0
            while cur != None:
                count += 1
                cur = cur.next
            return count
        #遍历
        def travel(self):
            cur = self.__head
            while cur != None:
                print(cur.item,end="")
                cur = cur.next
            print("")
        #插入头部
        def add(self,item):
            node = Node(item)
            cur = self.__head
            if self.is_empty():
                self.__head = node
            else:
                node.next = self.__head
                node.next.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 != None:
                    cur = cur.next
                cur.next = node
                node.prev = cur
        #查找
        def search(self,item):
            cur = self.__head
            while cur != None:
                if cur.item == item:
                    return True
                cur = cur.next
            else:
                return False
        #指定位置插入
        def insert(self,pos,item):
            if pos <= 0:
                self.add(item)
            elif pos > (self.length()-1):
                self.append(item)
            else:
                node = Node(item)
                cur = self.__head
                count = 0
                while count < (pos-1):
                    count += 1
                    cur = cur.next
                node.next = cur.next
                node.next.prev = node
                node.prev = cur
                cur.next = node
        #删除
        def remove(self,item):
            if self.is_empty():
                return
            else:
                cur = self.__head
                #首个元素就是需要被删除的元素
                if cur.item == item:
                    #如果只有一个节点
                    if cur.next == None:
                        self.__head = None
                    else:
                        cur.next.prev = None
                        self.__head = cur.next
                    return
                while cur != None:
                    if cur.item == item:
                        cur.next.prev = cur.prev
                        cur.prev.next = cur.next
                        break
                    cur = cur.next

if __name__ == '__main__':
    ll = DLinkList()
    ll.add(1)
    ll.add(2)
    ll.append(3)
    ll.insert(2, 4)
    ll.insert(4, 5)
    ll.insert(0, 6)
    print("length:", ll.length())
    ll.travel()
    print(ll.search(3))
    print(ll.search(4))
    ll.remove(1)
    print("length:", ll.length())
    ll.travel()



运行结果:

length: 6
621435
True
True
length: 5
62435
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值