双向循环链表(python代码)
class Node():
def __init__(self,elem):
self.elem = elem
self.pre = None
self.next = None
class DCLL():
def __init__(self):
self.__head = None
def add(self,item):
node = Node(item)
if self.__head == None:
self.__head = node
node.next = self.__head
node.pre = self.__head
return True
elif self.__head.next == self.__head:
node.next = self.__head
self.__head.pre = node
node.pre = self.__head
self.__head.next = node
self.__head = node
return True
else:
node.next = self.__head
node.pre = self.__head.pre
self.__head.pre = node
cur = self.__head
while self.__head != cur.next:
cur = cur.next
cur.next = node
self.__head = node
return True
def is_empty(self):
return self.__head == None
def append(self,item):
node = Node(item)
if self.__head == None:
self.__head = node
node.next = self.__head
node.pre = self.__head
return True
elif self.__head.next == self.__head:
self.__head.next = node
node.pre = self.__head
node.next = self.__head
self.__head.pre = node
return True
else:
cur = self.__head
while self.__head != cur.next:
cur = cur.next
cur.next = node
node.pre = cur
node.next = self.__head
self.__head.pre = node
return True
def insert(self,pos,item):
"""
默认pos =< 0 插入头部,pos >=已有链表长度 插入尾部 ,其余插入中间位置
"""
node = Node(item)
if self.__head == None:
self.__head = node
node.next = self.__head
node.pre = self.__head
return True
elif self.__head.next == self.__head:
if pos <= 0:
node.next = self.__head
self.__head.pre = node
node.pre = self.__head
self.__head.next = node
self.__head = node
return True
else:
self.__head.next = node
node.pre = self.__head
node.next = self.__head
self.__head.pre = node
return True
else:
count = 1
cur_count = self.__head
while cur_count.next!= self.__head:
count += 1
cur_count = cur_count.next
if pos <= 0:
node.next = self.__head
node.pre = self.__head.pre
self.__head.pre = node
cur = self.__head
while self.__head != cur.next:
cur = cur.next
cur.next = node
self.__head = node
return True
elif pos >= count:
cur = self.__head
while self.__head != cur.next:
cur = cur.next
cur.next = node
node.pre = cur
node.next = self.__head
self.__head.pre = node
return True
else:
cur = self.__head
count = 0
while count < pos-1:
count += 1
cur = cur.next
cur.next.pre = node
node.next = cur.next
node.pre = cur
cur.next = node
return True
def remove(self,item):
if self.__head == None:
return False
if self.__head == self.__head.next:
if self.__head.elem != item:
return False
else:
self.__head = None
return True
if self.__head.elem == item:
if self.__head.pre == self.__head.next:
self.__head.next.next = self.__head.next
self.__head.next.pre = self.__head.next
self.__head = self.__head.next
return True
else:
self.__head.pre.next = self.__head.next
self.__head.next.pre = self.__head.pre
self.__head = self.__head.next
return True
else:
cur = self.__head
while cur.next != self.__head:
if cur.elem != item:
cur = cur.next
else:
cur.next.pre = cur.pre
cur.pre.next = cur.next
return True
if cur.elem != item:
return False
else:
cur.pre.next = self.__head
self.__head.pre = cur.pre
return True
def travel(self):
if self.__head == None:
return False
cur = self.__head
while cur != self.__head.pre:
print(cur.elem, end=" ")
cur = cur.next
print(cur.elem)
return True
def length(self):
if self.__head == None:
return 0
count = 1
cur = self.__head
while cur!= self.__head.pre:
count += 1
cur = cur.next
return count
def search(self,item):
if self.__head == None:
return False
cur = self.__head
while cur != self.__head.pre:
if cur.elem == item:
return True
else:
cur = cur.next
if cur.elem == item:
return True
else:
return False
dcll = DCLL()
print("search",dcll.search(0))
for i in range(2):
print(dcll.insert(i,i))
dcll.travel()
print("search",dcll.search(0))
print("remove",dcll.remove(0))
print("search",dcll.search(0))
dcll.travel()
print("length",dcll.length())
print("search",dcll.search(5))