二者均是抽象数据类型( Abstract Data Type, ADT )
堆栈在 Python 中包含两种方式,分别是数组结构(以List仿真数组结构)和链表结构
判断是否为空堆栈
def is_empty():
global top
if top == -1: # 顶端
return True
else:
return False
入栈
def push(data):
global top
global MAXSTACK # 堆栈最大容量
global stack # stack = [None] * MAXSTACK 声明堆栈
if top >= MAXSTACK - 1:
return "堆栈已满"
else:
top += 1
stack[top] = data
出栈
def pop():
global top
global stack
if is_empty():
return "堆栈是空的"
else:
data = stask[top]
top -= 1
return data
用链表实现堆栈
算法复杂。动态改变链表长度,有效利用内存资源。
class Node:
def __init__(self):
self.data = 0
self.next = None
是否为空
def is_empty():
global top
if top is None:
return True
else:
return False
入栈
def push(data):
global top
new_add_node = Node()
new_add_node.data = data
new_add_node.next = top # 新的数据放在头部
top = new_add_node
出栈
def pop():
global top
if is_empty():
return "堆栈是空的"
else:
ptr = top
top = top.next # 从头部删除
data = ptr.data
return data
T = 2^n - 1
def hanoi(n, p1=1, p2=2, p3=3):
if n == 1:
print("盘子从 %d 移到 %d" % (p1, p3))
else:
hanoi(n-1, p1, p3, p2)
print('盘子从 %d 移到 %d' % (p1, p3))
hanoi(n-1, p2, p1, p3)
八皇后问题
在棋盘放入一个新皇后,且这个位置不会被就皇后吃掉(不限定走几格:直吃、横吃、对角斜吃),便将新皇后压入堆栈。
如果放置新皇后的行(或列)的 8 个位置都没有办法放置一个新皇后(放入任何一个位置都会被吃掉),就不许从堆栈中弹出前一个皇后的位置,并在该行(或该列)重新寻找另一个位置,将新位置压入堆栈(如果找不到,就回溯道前一行寻找前一个皇后的另一个新位置)。回溯( Backtracking )算法。
global queen
global number
EIGHT = 8
queen = [None] * 8
number = 0
def print_table():
global number
x = y = 0
number += 1
print('第 %d 组解' % number)
for x in range(EIGHT):
for y in range(EIGHT):
if x == queen[y]:
print('<q>', end='')
else:
print('<->', end='')
print('')
input('\n..按下任意键继续..\n')
def attack(row, col):
global queen
i = 0
atk = 0
offset_row = offset_col = 0
while atk != 1 and i < col:
offset_col = abs(i - col)
offset_row = abs(queen[i] - row)
if queen[i] == row or offset_row == offset_col:
atk = 1
i += 1
return atk
def decide_position(value):
global queen
i = 0
while i < EIGHT:
if attack(i, value) != 1:
queen[value] = i
if value == 7:
print_table()
else:
decide_position(value+1)
i += 1
decide_position(0)
用数组实现队列
算法简单。缺点数组大小无法根据队列的实际需求来动态申请,只能声明固定的大小。
用 front 和 rear 两个指针来分别指向队列的前端和末尾。
NAXSIZE = 4
queue = [0] * MAXSIZE
front = -1
rear = -1
添加一个元素,将 rear 值加 1;入队
def enqueue(item):
global rear
global front
global queue
if rear == MAXSIZE - 1:
print('队列已满')
else:
rear += 1
queue[rear] = item # 将新数据加到队列的末尾
出队
def dequeue():
global rear
global front
global queue
global MAXSIZE
if front == rear:
print('队列已空')
else:
front += 1
item = queue[front]
返回队首值
def front_value():
global rear
global front
global queue
if front == rear:
print('这是空队列!')
else:
print(queue[front])
入队
def enqueue(item):
global front
global rear
new_data = Node()
new_data.value = item
if rear == None:
front = new_data
else:
rear.next = new_data # 将新元素连接到队列末尾
rear = new_data # 将 rear 指向新元素,这是新队列的队尾
new_data.next = None
出队
def dequeue():
global front
global rear
if front == rear:
print('队列已空')
else:
front = front.next
一端加入,两端取出
def enqueue(value):
global front
global rear
node = Node()
node.data = value
node.next = None
if rear == None: # 检查是否为空队列
front = node
else:
rear.next = node # 将节点加入到队列的末尾
rear = node # 将尾指针指向新加入的节点
def dequeue(action):
global front
global rear
# 从前端取出数据
if not(front == None) and action == 1:
if front == rear:
rear = None
value = front.data
front = front.next
return value
# 从队列末尾取出
elif not(rear == None) and action == 2:
startNode = front
value = rear.data
# 查找队列末尾节点的前一个节点
tempNode = front
while front.next != rear and front.next != None:
front = front.next
tempNode = front
front = startNode # 新前端指针
rear = tempNode # 新尾端指针
# 队列中仅剩下最后一个节点时,取出后将 front 和 rear 指向 None
if front.next == None or rear.next == None:
front = None
rear = None
return value
else:
return -1
优先队列( Priority Queue )
假如元素时可任意加入,但先输出优先级高者( HPOF, Highest Priority Out First )
当各元素按输入先后顺序为优先级时,就是普通队列。若以输入顺序的倒序为优先级,则是堆栈。
常见的堆栈应用:
堆栈:一组相同数据类型数据的集合,所有的操作都在堆栈顶端进行,具有“先进后出”的特性。
队列的应用:
原文:https://www.cnblogs.com/catyuang/p/11760499.html