单向循环链表
单链表的一个变形是单向循环链表,链表中最后一个节点的next域不再为None,而是指向链表的头节点。
代码实现
操作
is_empty() 判断链表是否为空
length() 返回链表的长度
travel() 遍历
add(item) 在头部添加一个节点
append(item) 在尾部添加一个节点
insert(pos, item) 在指定位置pos添加节点
remove(item) 删除一个节点
search(item) 查找节点是否存在
class Node:
def __init__(self,item):
self.item=item
self.next=None
class Link:
def __init__(self):
self.head=None
def is_empty(self):
return self.head==None
def length(self):
if self.is_empty():
return 0
cur=self.head
count=1
while cur.next!=self.head:
count+=1
cur=cur.next
return count
def travel(self):
if self.is_empty():
return
cur=self.head
print(cur.item)
while cur.next!=self.head:
cur=cur.next
print(cur.item)
print("")
def add(self,item):
node=Node(item)
if self.is_empty():
self.head=node
node.next=self.head
else:
node.next=self.head
cur=self.head
while cur.next!=self.head:
cur=cur.next
cur.next=node
self.head=node
def append(self,item):
node=Node(item)
if self.is_empty():
self.head=node
node.next=self.head
else:
cur=self.head
while cur.next!=self.head:
cur=cur.next
cur.next=node
node.next=self.head
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):
cur=cur.next
count+=1
node.next=cur.next
cur.next=node
def remove(self,item):
if self.is_empty():
return
cur=self.head
pre=None
if cur.item==item:
if cur.next!=self.head:
while cur.next!=self.head:
cur=cur.next
cur.next=self.head.next
self.head=self.head.next
else:
self.head=None
else:
while cur.next!=self.head:
if cur.item==item:
pre.next=cur.next
return
else:
pre=cur
cur=cur.next
if cur.item==item:
pre.next=self.head
else:
print("item not found")
def search(self,item):
if self.is_empty():
return False
cur=self.head
if cur.item==item:
return True
while cur.next!=self.head:
cur=cur.next
if cur.item==item:
return True
return False
双向链表
一种更复杂的链表是“双向链表”或“双面链表”。每个节点有两个链接:一个指向前一个节点,当此节点为第一个节点时,指向空值;而另一个指向下一个节点,当此节点为最后一个节点时,指向空值。
操作
is_empty() 链表是否为空
length() 链表长度
travel() 遍历链表
add(item) 链表头部添加
append(item) 链表尾部添加
insert(pos, item) 指定位置添加
remove(item) 删除节点
search(item) 查找节点是否存在
class Node:
def __init__(self,item):
self.item=item
self.next=None
self.pre=None
class Link:
def __init__(self):
self.head=None
def is_empty(self):
return self.head==None
def length(self):
if self.is_empty():
return 0
cur=self.head
count=1
while cur.next!=None:
count+=1
cur=cur.next
return count
def travel(self):
if self.is_empty():
return
cur=self.head
print(cur.item)
while cur.next!=None:
cur=cur.next
print(cur.item)
print("")
def add(self,item):
node=Node(item)
if self.is_empty():
self.head=node
else:
node.next=self.head
self.head.pre=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.pre=cur
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.pre=cur
cur.next.pre=node
cur.next=node
def remove(self,item):
if self.is_empty():
return
cur=self.head
if cur.item==item:
if cur.next==None:
self.head=None
else:
cur.next.pre=None
self.head=cur.next
return
while cur!=None:
if cur.item==item:
cur.pre.next=cur.next
cur.next.pre=cur.pre
return
cur=cur.next
print("item not found")
return
def search(self,item):
if self.is_empty():
return False
cur=self.head
while cur!=None:
if cur.item==item:
return True
cur=cur.next
return False