单链表
class Item:
def __init__(self,v):
self.data=v
self.next=None
class LinkList:
def __init__(self):
self.head=Item(None)
self.count=0
def append(self,v):
e=Item(v)
a=self.head
while (a.next!=None):
a=a.next
a.next=e
self.count=self.count+1
def insert(self,pos,v):
if pos<=self.count:
e=Item(v)
i=0
a=self.head
while(i<pos-1):
a=a.next
i=i+1
e.next=a.next
a.next=e
self.count=self.count+1
def delete(self,pos):
if pos<=self.count:
i=0
a=self.head
while(i<pos-1):
a=a.next
i=i+1
p=a.next
a.next=p.next
del(p)
self.count=self.count-1
def print_item(sef,pos):
if pos<=self.count:
i=0
a=self.head
while(i<pos):
a=a.next
i=i+1
print(a.data)
def print_all(self):
a=self.head
while(a.next):
a=a.next
print(a.data)
myll=LinkList()
myll.append(123)
myll.append(3455)
myll.print_all()
myll.delete(2)
myll.print_all()
循环单链表
class Item:
def __init__(self,v):
self.data=v
self.next=None
class CycleLinkList:
def __init__(self):
self.head=Item(None)
self.count=0
def append(self,v):
e=Item(v)
a=self.head
i=0
while (i<self.count):
a=a.next
i=i+1
a.next=e
h=(self.head)
e.next=h.next
self.count=self.count+1
def insert(self,pos,v):
if pos<=self.count:
e=Item(v)
i=0
a=self.head
while(i<pos-1):
a=a.next
i=i+1
e.next=a.next
a.next=e
self.count=self.count+1
def delete(self,pos):
if pos<=self.count:
i=0
a=self.head
while(i<pos-1):
a=a.next
i=i+1
p=a.next
a.next=p.next
del(p)
self.count=self.count-1
def print_item(sef,pos):
if pos<=self.count:
i=0
a=self.head
while(i<pos):
a=a.next
i=i+1
print(a.data)
def print_all(self):
i=0
a=self.head
while(i<self.count):
a=a.next
print(a.data)
i=i+1
myll=CycleLinkList()
myll.append(123)
myll.append(3455)
myll.print_all()
myll.delete(2)
myll.print_all()
双向链表
class Item:
def __init__(self,v):
self.data=v
self.next=None
self.pre=None #
class DoubleLinkList:
def __init__(self):
self.head=Item(None)
self.count=0
def append(self,v):
e=Item(v)
a=self.head
while (a.next!=None):
a=a.next
a.next=e
e.pre=a #
self.count=self.count+1
def insert(self,pos,v):
if pos<=self.count:
e=Item(v)
i=0
a=self.head
while(i<pos-1):
a=a.next
i=i+1
a.next.pre=e#
e.next=a.next
a.next=e
e.pre=a#
self.count=self.count+1
def insert2(self,pos,v):
if pos<=self.count:
e=Item(v)
i=0
a=self.head
while(i<pos):
a=a.next
i=i+1
e.pre=a.pre
e.next=a
a.pre.next=e
a.pre=e
self.count=self.count+1
def delete(self,pos):
if pos<=self.count:
i=0
a=self.head
while(i<pos-1):
a=a.next
i=i+1
p=a.next
a.next=p.next
if p.next:#
p.next.pre=a #
del(p)
self.count=self.count-1
def delete2(self,pos):
if pos<=self.count:
i=0
a=self.head
while(i<pos):
a=a.next
i=i+1
a.pre.next=a.next
if a.next:
a.next.pre=a.pre
del(a)
self.count=self.count-1
def print_item(sef,pos):
if pos<=self.count:
i=0
a=self.head
while(i<pos):
a=a.next
i=i+1
print(a.data)
def print_all(self):
a=self.head
while(a.next):
a=a.next
print(a.data)
myll=DoubleLinkList()
myll.append(123)
myll.append(3455)
myll.print_all()
myll.delete2(2)
myll.print_all()
双向循环链表
class Item:
def __init__(self,v):
self.data=v
self.next=None
self.pre=None
class DoubleLinkList:
def __init__(self):
self.head=Item(None)
self.count=0
def append(self,v):
e=Item(v)
a=self.head.next.pre
a.next=e
e.pre=a
e.next=self.head.next
self.head.next.pre=e
self.count=self.count+1
def insert(self,pos,v):
if pos<=self.count:
e=Item(v)
i=0
a=self.head
while(i<pos-1):
a=a.next
i=i+1
a.next.pre=e
e.next=a.next
a.next=e
e.pre=a
self.count=self.count+1
def insert2(self,pos,v):
if pos<=self.count:
e=Item(v)
i=0
a=self.head
while(i<pos):
a=a.next
i=i+1
e.pre=a.pre
e.next=a
a.pre.next=e
a.pre=e
self.count=self.count+1
def delete(self,pos):
if pos<=self.count:
i=0
a=self.head
while(i<pos-1):
a=a.next
i=i+1
p=a.next
a.next=p.next
if p.next:
p.next.pre=a
del(p)
self.count=self.count-1
def delete2(self,pos):
if pos<=self.count:
i=0
a=self.head
while(i<pos):
a=a.next
i=i+1
a.pre.next=a.next
if a.next:
a.next.pre=a.pre
del(a)
self.count=self.count-1
def print_item(sef,pos):
if pos<=self.count:
i=0
a=self.head
while(i<pos):
a=a.next
i=i+1
print(a.data)
def print_all(self):
a=self.head
i=0
while(i<self.count):
a=a.next
print(a.data)
i=i+1
myll=DoubleLinkList()
myll.append(123)
myll.append(3455)
myll.print_all()
myll.delete2(2)
myll.print_all()
静态链表
class StaticLinkList:
def __init__(self):
self.space=[[None,i+1] for i in range(100)]
self.space[99][1]=-1
self.free=0
self.head=-1
self.count=0
def append(self,v):
if self.free!=-1:
new_pos=self.free
self.free=self.space[new_pos][1]
self.space[new_pos][0]=v
self.space[new_pos][1]=-1
cur=self.head
if cur==-1:
self.head=new_pos
else:
while(cur!=-1):
before=cur
cur=self.space[cur][1]
self.space[before][1]=new_pos
self.count=self.count+1
def insert(self,pos,v):
if pos<=self.count:
new_pos=self.free
self.free=self.space[new_pos][1]
self.space[new_pos][0]=v
cur=self.head
i=1
while(i<pos-1):
i=i+1
cur=self.space[cur][1]
e=self.space[cur][1]
self.space[cur][1]=new_pos
self.space[new_pos][1]=e
self.count=self.count+1
def delete(self,pos):
if pos<=self.count:
cur=self.head
i=1
while(i<pos-1):
i=i+1
cur=self.space[cur][1]
e=self.space[cur][1]
self.space[cur][1]=self.space[e][1]
self.space[e][1]=self.free
self.free=e
self.count=self.count-1
def print_item(sef,pos):
if pos<=self.count:
cur=self.head
i=1
while(i<pos):
i=i+1
cur=self.space[cur][1]
print(self.space[cur][0])
def print_all(self):
cur=self.head
while(cur!=-1):
print(self.space[cur][0])
cur=self.space[cur][1]
myll=StaticLinkList()
myll.append(123)
myll.append(3455)
myll.print_all()
myll.delete(2)
myll.print_all()