chap10 list 单链表实现

来源:互联网 发布:xp深度优化工具 编辑:程序博客网 时间:2024/05/16 19:15
class Node:    def __init__(self,key,next=None):        self.key=key        self.next=nextclass LinkList:    def __init__(self):        self.nil=Node('error',None)        self.nil.next=self.nil    def insert(self,x):        x.next=self.nil.next        self.nil.next=x    def delete(self,x):        prev,cur=self.nil,self.nil.next        while(cur!=x):            prev,cur=cur,cur.next            if(prev==self.nil):                return None        prev.next=cur.next    def delete_key(self,x):        prev,cur=self.nil,self.nil.next        while cur!=self.nil and cur.key!=x:            prev,cur=cur,cur.next        if cur==self.nil:            return None        prev.next=cur.next    def __repr__(self):        res=''        cur=self.nil.next        while(cur!=self.nil):            res+=(str(cur.key)+' ')            cur=cur.next        return resl=LinkList()for i in range(0,10):    print(i)    l.insert(Node(i))print(l)for i in range(0,10):    l.delete_key(i)    print(l)        

0 0