在说栈,队列以及链表之前,先来了解一下,顺序表:
顺序表:将元素顺序的存放在一块连续的存储区域内,元素之间是顺序关系,由他们的存储顺序标识,这样的数据结构叫做顺序表.
其中,python中典型的顺序表结构的数据结构便是列表(list).
python的列表是一种元素个数可变的线性表,它的元素在顺序表的外侧.列表的表头与元素是分开存储的,更改list元素时,表对象不发生变化,因此list的id值不变.
1.栈
栈(stack):又称为堆栈.是一种数据容器,也可以实现数据的增删改查.它不同于列表的特点在于其只有一个数据出入口,因此其数据遵循先进后出的数据处理原则.
python可以利用列表实现栈,代码如下:
class Stack(object):
def __init__(self):
self.stack = []
def add(self, item):
self.stack.append(item)
return self.stack
def remove(self):
self.stack.pop()
return self.stack
def is_empty(self):
return bool(self.stack)
def size(self):
return len(self.stack)
2.队列
队列:队列是可以允许一个数据口进数据,一个口出数据的一种数据结构,一般是先进先出的数据处理原则.
python实现队列的代码如下:
class Que(object):
def __init__(self):
self.que = []
def add(self, item):
self.que.insert(0, item)
return self.que
def remove(self):
self.que.pop()
return self.que
def is_empty(self):
return bool(self.que)
def size(self):
return len(self.que)
除了最基本的队列外,python还可以实现双向队列,代码如下:
class Deque(object):
def __init__(self):
self.deque = []
def add_head(self, item):
self.deque.insert(0, item)
return self.deque
def add_tail(self, item):
self.deque.append(item)
return self.deque
def remove_head(self):
self.deque.pop(0)
return self.deque
def remove_tail(self):
self.deque.pop()
return self.deque
def is_empty(self):
return bool(self.que)
def size(self):
return len(self.que)
3.链表
首先链表不是顺序表,而是由一系列存储数据元素的单元通过指针串联起来的数据结构.组成链表的基本单元被称为节点.节点起到保存数据和连接上下节点的功能.节点可以大致划分为两个功能区,数据域和指针域.其中数据域用来存储数据,指针域用来引用上一节点或者下一节点.其中,位于头部的节点被称为头结点,位于尾部的节点,叫做尾节点.链表大致可以分为单向链表,双向链表和循环链表.
单向链表,的每一个节点只有一个指针域,用于引用当前节点的下一节点.
实现代码如下:
# 节点对象
class Node(object):
def __init__(self, element):
self.element = element
self.next = None
# 链表对象
class LinkList(object):
def __init__(self, node=None):
self.head = node
def is_empty(self):
return self.head == None
def length(self):
cur = self.head
count = 0
while cur !=None:
count += 1
cur = cur.next
return count
def view(self):
cur = self.head
while cur != None:
print(cur.element)
cur = cur.next
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
def search(self, item):
cur = self.head
while cur != None:
if cur.element == item:
return True
else:
cur = cur.next
else:
return False
def add_head(self, item):
node = Node(item)
node.next = self.head
self.head = node
def remove(self, item):
por = None
cur = self.head
if item == self.head.element:
self.head = cur.next
else:
while cur != None:
if cur.element == item:
por.next = cur.next
break
else:
por = cur
cur = cur.next
def insert(self, item, index):
node = Node(item)
count = 0
por = None
cur = self.head
if index == 0:
self.add_head(item)
else:
while cur != None:
if count == index:
por.next = node
node.next = cur
break
else:
por = cur
cur = cur.next
count +=1
双向链表是具有两个指针域的链表,其两个指针域分别为next,pre,用来引用后一个节点或者前一个节点.
实现代码如下:
class Node(object):
def __init__(self, element):
self.element = element
self.next = None
self.pre = None
class LinkList(object):
def __init__(self, node=None):
self.head = node
def is_empty(self):
return self.head == None
def length(self):
cur = self.head
count = 0
while cur != None:
count += 1
cur = cur.next
return count
def view(self):
cur = self.head
while cur != None:
print(cur.element)
cur = cur.next
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 search(self, item):
cur = self.head
while cur != None:
if cur.element == item:
return True
else:
cur = cur.next
def add_head(self, item):
node = Node(item)
node.next = self.head
self.head.pre = node
self.head = node
def remove(self, item):
node = Node(item)
cur = self.head
if node == self.head:
self.head = self.head.next
self.head.pre = None
else:
while cur != None:
if cur.element == item:
cur.next.pre = cur.pre
cur.pre.next = cur.next
break
else:
cur = cur.next
def insert(self, item, index):
node = Node(item)
count = 0
cur = self.head
if index == 0:
self.add_head(item)
else:
while cur != None:
if count == index:
node.next = cur
cur.pre.next = node
node.pre = cur.pre
cur.pre = node
break
else:
cur = cur.next
count += 1
else:
self.append(item)
单向循环链表,是一种首尾相连的单向链表,其尾节点的next不再指向None,而是指向头结点.
代码实现如下:
class Node(object):
def __init__(self, element):
self.element = element
self.next = self
class LinkList(object):
def __init__(self, node=None):
self.head = node
def is_empty(self):
return self.head == None
def length(self):
cur = self.head
if self.is_empty():
return 0
else:
count = 1
while cur.next != self.head:
count += 1
cur = cur.next
return count
def view(self):
cur = self.head
while cur.next != self.head:
print(cur.element)
cur = cur.next
print(cur.element)
def append(self, item):
node = Node(item)
if self.is_empty():
self.head = node
else:
cur = self.head
while cur.next != self.head:
cur = cur.next
cur.next = node
node.next = self.head
def search(self, item):
cur = self.head
while cur != None:
if cur.element == item:
return True
else:
cur = cur.next
if cur.element == item:
return True
def add_head(self, item):
node = Node(item)
node.next = self.head
cur = self.head
while cur.next != self.head:
cur = cur.next
self.head = node
cur.next = node
def remove(self, item):
node = Node(item)
por = None
cur = self.head
if node == self.head:
self.head = cur.next
while cur.next != self.head:
cur = cur.next
cur.next = self.head
else:
while cur.next != self.head:
if cur.element == item:
por.next = cur.next
break
else:
por = cur
cur = cur.next
if cur == node:
cur.next = self.head.next
def insert(self, item, index):
node = Node(item)
count = 0
por = None
cur = self.head
if index == 0:
self.add_head(item)
else:
while cur.next != self.head:
if count == index:
por.next = node
node.next = cur
break
else:
por = cur
cur = cur.next
count +=1
else:
self.append(item)