Python数据结构知识之栈(二)
2018-01-22 本文已影响11人
withism
一、简单的Stack的实现和应用:
Stack.py
# Author: Allen Guo
# Data: 2018-01-22
# For github repos please check <https://github.com/wilixx/python_training> .
# For more about the author, see to <www.whoispm.com> but note the disclaimer there.
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
def __str__(self): # The modification of __str__() is for convenience of "print".
str = ''
for item in self.items:
str=str+item+' '
return str
Test Code
#------------Note that test goes first-------------------
# Stack_A = Stack()
# Stack_A.push()
# print Stack_A
# Stack_A.isEmpty()
# Stack_A.push('Mathbook')
# Stack_A.push('Chinese')
# Stack_A.push('French')
# Stack_A.push('English')
# Stack_A.push('Foo')
# Stack_A.push('Hello')
# Stack_A.push('Hello')
# Stack_A.push('Hello')
#
# print Stack_A
# Stack_A.pop()
# print Stack_A
# Stack_A.pop()
# print Stack_A
# Stack_A.pop()
# print Stack_A
# Stack_A.pop()
# print Stack_A
# Stack_A.pop()
# print Stack_A
# Stack_A.pop()
# print Stack_A
# print Stack_A.peek()
# print Stack_A.pop()
# print Stack_A.peek()
# print Stack_A.pop()
# print Stack_A.peek()
# print Stack_A.pop()
# print Stack_A.peek()
# print Stack_A.pop()
# print Stack_A.peek()
二、利用Stack检查括号:
ParChecker.py
# Author: Allen Guo
# Data: 2018-01-22
# For github repos please check <https://github.com/wilixx/python_training> .
# For more about the author, see to <www.whoispm.com> but note the disclaimer there.
from Stack import Stack
def parChecker(symbolString):
s = Stack()
balanced = True
index = 0
while index < len(symbolString) and balanced:
symbol = symbolString[index]
if symbol == "(":
s.push(symbol)
elif symbol == ")":
s.pop()
elif symbol == ")":
pass
index = index + 1
if balanced and s.isEmpty():
return True
else:
return False
print(parChecker('(((A)B))'))
print(parChecker('(()'))
三、解析数学表达式:(未完待续)
# Author: Allen Guo
# Data: 2018-01-22
# For github repos please check <https://github.com/wilixx/python_training> .
# For more about the author, see to <www.whoispm.com> but note the disclaimer there.
# There is still not very well. Waiting for when I am free...
from Stack import Stack
def infixToPostfix(infixexpr):
prec = {}
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
opStack = Stack()
postfixList = []
tokenList = infixexpr.split()
for token in tokenList:
if token in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" or token in "0123456789":
postfixList.append(token)
elif token == '(':
opStack.push(token)
elif token == ')':
topToken = opStack.pop()
while topToken != '(':
postfixList.append(topToken)
topToken = opStack.pop()
else:
while (not opStack.isEmpty()) and \
(prec[opStack.peek()] >= prec[token]):
postfixList.append(opStack.pop())
opStack.push(token)
while not opStack.isEmpty():
postfixList.append(opStack.pop())
return " ".join(postfixList)
print(infixToPostfix("A * B + C * D"))
print(infixToPostfix("( A + B ) * C - ( D - E ) * ( F + G )"))