"""
线性表之链式存储结构 双向循环链表
基本操作:
初始化 __init__
线性表空判断 empty
清空线性表 clear
获取元素 get
是否存在数据 exist
插入操作 insert
删除操作 delete
线性表的长度 length
扩展操作
遍历链表 show
链表从头开始添加 add
链表添加到最后 append
链表删除最后元素 pop
"""
class Node(object):
def __init__(self, data=None):
self.data = data
self.prior = None
self.next = None
def __str__(self):
return "数据:{data},地址:{next}".format(data=self.data, next=id(self.prior))
class DoubleCycleLinkedList(object):
def __init__(self):
self._length = 0
self._head = Node()
self._head.next = self._head
self._head.prior = self._head
def head(self):
return self._head
def tail(self):
return self._head.prior
def length(self):
return self._length
def empty(self):
return self._head.prior == self._head
def exist(self, value):
"""
判断数据是否存在
时间复杂度O(n)
:param value:
:return:
"""
current = self._head.next
while current != self._head:
if current.data == value:
return True
current = current.next
return False
def get(self, index):
"""
根据下标获取值
时间复杂度O(n)
:param index:
:return:
"""
current = self._head.next
i = 0
while current != self._head:
if i == index:
return current.data
current = current.next
i += 1
return -1
def add(self, value):
"""
从头部插入
时间复杂度O(1)
:param value:
:return:
"""
node = Node(value)
node.next = self._head.next
node.prior = self._head
self._head.next.prior = node
self._head.next = node
self._length += 1
def append(self, value):
"""
从尾部插入
时间复杂度为O(1)
:param value:
:return:
"""
new_node = Node(value)
new_node.next = self._head
new_node.prior = self._head.prior
self._head.prior.next = new_node
self._head.prior = new_node
self._length += 1
def insert(self, index, value):
"""
在任意位置插入
:param index:
:param value:
:return:
"""
if index < 0:
raise Exception("插入位置错误, %d" % index)
new_node = Node(value)
cur = self._head
n = 0
while cur.next != self._head:
if n == index:
break
cur = cur.next
n += 1
new_node.next = cur.next
new_node.prior = cur
cur.next.prior = new_node
cur.next = new_node
self._length += 1
def index(self, value):
"""
获取元素的下标
:param value:
:return:
"""
current = self._head.next
i = 0
while current != self._head:
if current.data == value:
return i
i += 1
current = current.next
return -1
def delete(self, idx):
"""
根据下标删除元素
时间复杂度为O(n)
:param idx:
:return:
"""
if idx >= self._length:
raise Exception("数组越界")
current = self._head
i = 0
while current.next != self._head:
if i == idx:
current.next.prior = current
current.next = current.next.next
self._length -= 1
return
current = current.next
i += 1
def remove(self, value):
"""
删除指定的数据
:param value:
:return:
"""
cur = self._head
while cur.next != self._head:
if cur.next.data == value:
cur.next.prior = cur
cur.next = cur.next.next
self._length -= 1
return
cur = cur.next
raise Exception("元素不存在:", value)
def pop(self):
"""
删除最后一个元素
:return:
"""
cur = self._head.prior
data = cur.data
cur.prior.next = self._head
self._head.prior = cur.prior
self._length -= 1
return data
def show(self):
node = self._head.next
while node != self._head:
print(node.data, end=" ")
node = node.next
print()
if __name__ == '__main__':
l = DoubleCycleLinkedList()
l.add(1)
l.add(2)
l.add(3)
l.append(12)
l.insert(2, 10)
l.show()
print(l.pop())
print(l.pop())
print(l.tail().data)
l.show()