单向循环链表:
class Node(object):
"""节点"""
def __init__(self, elem):
self.elem = elem
self.next = None
class SingleLinkList(self):
"""单向循环链表"""
def __init__(self, node = None):
self.__head = node
if node:
node.next = node
def is_empty(self):
"""链表是否为空"""
return self.__head == None
def length(self):
"""链表长度"""
if self.is_empty():
return 0
# cur游标,用来移动遍历节点
cur = self.__head
# count记录数量
count = 1
while cur.next != self.__head:
count += 1
cur = cur.next
return count
def travel(self):
"""遍历整个链表"""
if self.is_empty():
return
# cur游标,用来移动遍历节点
cur = self.__head
while cur.next != self.__head:
print(cur.elem, end = " ")
cur = cur.next
# 退出循环,cur指向尾节点,但尾节点的元素未打印
print(cur.elem)
def add(self, item):
"""在链表头部添加元素,头插法"""
node = Node(item)
if self.is_empty():
self.__head = node
node.next = node
else:
cur = self.__head
while cur.next != self.__head:
cur = cur.next
# 退出循环,cur指向尾节点
node.next = self.__head
self.__head = node
cur.next = node
def append(self, item):
"""在链表尾部添加元素,尾插法"""
node = Node(item)
if self.is_empty():
self.__head = node
node.next = node
else:
cur = self.__head
while cur.next != self.__head:
cur = cur.next
cur.next = node
node.next = self.__head
def insert(self, pos, item):
"""在指定的位置添加元素"""
# pos 指定位置,从0开始索引
node = Node(item)
if pos <= 0:
self.add(item)
elif pos > (self.length() - 1):
self.append(item)
else:
cur = self.__head
count = 0
# 循环结束,cur指向pos上一个节点
while count < (pos - 1):
count += 1
cur = cur.next
node.next = cur.next
cur.next = node
def remove(self, item):
"""删除节点"""
if self.is_empty():
return
cur = self.__head
pre = None
while cur.next != self.__head:
if cur.elem == item:
# 判断当前节点是不是头节点,头节点情况不同
if cur == self.__head:
# 头节点
# 找尾节点
rear = self.__head
while rear.next != self.__head:
rear = rear.next
self.__head = cur.next
rear.next = self.__head
else:
# 中间节点
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
# 退出循环,cur指向尾节点
if cur.next == item:
if cur == self.__head:
# 链表只有一个节点
self.__head = None
else:
pre.next = self.__head
def search(self, item):
"""查找节点是否存在"""
if self.is_empty():
return False
cur = self.__head
while cur.next != self.__head:
if cur.elem == item:
return True
else:
cur = cur.next
# 退出循环,cur指向尾节点
if cur.elem == item:
return True
return False
栈(stack):
用于保存线性数据的容器,特点是只能允许容器的一端进行加入数据和输出数据,按照后进先出的原理运作(last in first out)。没有了位置概念,保证任何时候任何访问、删除的元素都是最后存入的元素,确定了默认的访问顺序
push(item):添加一个新的元素到栈顶
pop():弹出栈顶元素
peek():返回栈顶元素(栈顶元素不变)
size():返回栈的元素个数
class Stack(object):
"""栈"""
def __init__(self):
self.__list = []
def push(self, item):
# 添加一个新的元素到栈顶
self.__list.append(item)
def pop(self):
# 弹出栈顶元素
self.__list.pop()
def peek(self):
# 返回栈顶元素
if self.__list:
return self.__list[-1]
else:
return None
def is_empty(self):
# 判断栈是否为空
return self.__list == []
def size(self):
# 返回栈的元素个数
return len(self.__list)
队列(queue):
取的那端为队头,添加的那端为队尾
enqueue(item):往队列中添加一个元素
dequeue():从队列头部删除一个元素
is_empty():判断是否为空
size():返回队列大小
class Queue(object):
"""队列"""
def __init__(self):
self.__list = []
def enqueue(self, item):
"""往队列中添加元素"""
self.__list.append(item)
def dequeue(self):
"""从队列头部删除一个元素"""
return self.__list.pop(0)
def is_empty(self):
"""判断是否为空"""
return self.__list == []
def size(self):
"""返回队列大小"""
return len(self.__list)
双端队列:
class Dequeue(object):
"""双端队列"""
def __init__(self):
self.__list = []
def add_front(self, item):
"""从头部往队列中添加元素"""
self.__list.insert(0, item)
def add_rear(self, item):
"""从尾部往队列中添加元素"""
self.__list.append(item)
def pop_front(self):
"""从队列头部删除一个元素"""
return self.__list.pop(0)
def pop_rear(self):
"""从队列尾部删除一个元素"""
return self.__list.pop()
def is_empty(self):
"""判断是否为空"""
return self.__list == []
def size(self):
"""返回队列大小"""
return len(self.__list)