class Node:
def __init__(self,key,next=None):
self.key=key
self.next=next
class 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 res
l=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)
chap10 list 單鏈表實現
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.