顺序栈
三个基本属性
-
栈的存储数据data
-
栈的最大存储量maxSize
-
栈顶top
Python实现
# Python 2.7
class sqStack(object):
# 初始化
def __init__(self, maxSize):
self.data = [None] * maxSize
self.maxSize = maxSize
self.top = -1
# 获取顺序栈长度
def get_length(self):
return self.top + 1
# 清空顺序栈
def clear(self):
for i in range(self.get_length()):
self.data[i] = None
self.top = -1
# 是否为空
def is_empty(self):
return self.top == -1
# 是否满
def is_full(self):
return self.get_length() == self.maxSize
# 进栈
def push(self, elem):
if self.is_full():
print 'Stack is full!'
else:
self.top += 1
self.data[self.top] = elem
# 出栈
def pop(self):
if self.is_empty():
print 'Stack is empty!'
else:
self.top -= 1
return self.data[self.top + 1]
# 取栈顶元素
def get_top(self):
if self.is_empty():
print 'Stack is empty!'
else:
return self.data[self.top]
# 由栈顶打印栈元素
def show_stack(self):
for i in range(self.get_length()-1, -1, -1):
print self.data[i]
链式栈
节点的基本属性
-
数据域data
-
后继指针next
链式栈的基本属性
-
栈顶
-
链式栈的长度
Python实现
# Python 2.7
# 节点
class Node(object):
def __init__(self, data, next=None):
self.data = data
self.next = next
# 链表
class lkStack(object):
# 初始化
def __init__(self):
self.top = Node(None)
self.length = 0
# 是否为空
def is_empty(self):
return self.length == 0
# 获取链表长度
def get_length(self):
return self.length
# 进栈
def push(self, elem):
node = Node(elem, self.top)
self.top = node
self.length += 1
# 出栈
def pop(self):
if self.length == 0:
print 'lkStack is empty!'
else:
top = self.top
self.top = self.top.next
self.length -= 1
return top.data
# 展示链表
def show(self):
if self.is_empty():
print 'lkList is empty!'
else:
p = self.top
for i in range(self.length):
print p.data
p = p.next
栈的应用–四则运算表达式求值
后缀(逆波兰)表示法
-
中缀表示法:9+(3-1)*3+10/2
-
后缀表示法:9 3 1 - 3 * + 10 2 / +
中缀转后缀
# Python 2.7
# 将中缀字符串转换成数组
def str2list(calc_str):
# calc_str = '9+(3-1)*3+10/2'
symbol_list = ['+', '-', '*', '/', '(', ')']
calc_list = [calc_str]
for symbol in symbol_list:
str_list = []
for part in calc_list:
part_list = part.split(symbol)
temp = []
for item in part_list:
if item: # 非空时追加
temp.append(item)
temp.append(symbol)
del temp[-1]
if temp: # 非空时追加
str_list.extend(temp)
calc_list = str_list
return calc_list
# 中缀转后缀
def mid_after(calc_str):
mid2after = lkStack()
mid_list = str2list(calc_str)
after_list = []
symbol_list = ['+', '-', '*', '/', '(', ')']
symbol_value = {'(':-2, None:-1, '+':0, '-':0, '*':1, '/':1}
for item in mid_list:
if item not in symbol_list:
after_list.append(item)
else:
if item == ')':
while mid2after.top.data != '(':
top = mid2after.pop()
after_list.append(top)
top = mid2after.pop()
else:
if item == '(':
mid2after.push(item)
else:
if symbol_value[item] > symbol_value[mid2after.top.data]:
mid2after.push(item)
if symbol_value[item] < symbol_value[mid2after.top.data]:
while symbol_value[item] <= symbol_value[mid2after.top.data]:
top = mid2after.pop()
after_list.append(top)
mid2after.push(item)
while not mid2after.is_empty():
top = mid2after.pop()
after_list.append(top)
return after_list
通过后缀表达式求值
# Python 2.7
def after_calc(after_list):
symbol_list = ['+', '-', '*', '/']
stack = lkStack()
for item in after_list:
if item not in symbol_list:
stack.push(item)
else:
var1 = float(stack.pop())
var2 = float(stack.pop())
if item == '+':
result = var2 + var1
elif item == '-':
result = var2 - var1
elif item == '*':
result = var2 * var1
else:
result = var2 / var1
stack.push(str(result))
return float(stack.top.data)
测试
# Python 2.7
calc_str = '12+((12-7)*(11-2)-6)*12'
calc_list = str2list(calc_str)
after_list = mid_after(calc_str)
result = after_calc(after_list)