def buildParseTree(fpexp): fplist = fpexp.split() pStack = Stack() eTree = BinaryTree("") pStack.push(eTree) currentTree = eTree #入栈下降 for i in fplist: if i == '(': currentTree.insertLeft("") pStack.push(currentTree) currentTree = currentTree.getLeftChild() #入栈下降 elif i not in ['+', '-', '*', '/', ')']: currentTree.setRootVal(int(i)) parent = pStack.pop() currentTree = parent #出栈上升 elif i in ['+', '-', '*', '/']: currentTree.setRootVal(i) currentTree.insertRight("") pStack.push(currentTree) currentTree = currentTree.getRightChild() #入栈下降 elif i == ')': currentTree = pStack.pop() #出栈上升 else: raise ValueError return eTree
def xiaoxiaole(strings): s = Stack() list = [] for str in strings: # 如果与栈顶相同则消去栈顶, 不同则压入栈顶 if str == s.peek(): s.remove() else: s.push(str) if s.isEmpty(): return None else: while not s.isEmpty(): list.insert(0, s.pop()) # 取出栈中的每个元素,注意排列顺序 return "".join(list)
class Pin(): def __init__(self, name): self.stack = Stack() self.label = name def name(self): return self.label def isEmpty(self): return self.stack.isEmpty() def check(self, d): return self.stack.isEmpty() or d.get() < self.stack.peek().get() def peek(self): return self.stack.peek() def push(self, d): if self.check(d): self.stack.push(d) return True else: return False def pop(self): return self.stack.pop() def __str__(self): return self.name() + "=>" + str(self.stack)
def parChecker(sybomlString): s = Stack() balanced = True index = 0 while index < len(sybomlString) and balanced: symbol = sybomlString[index] if symbol == '(': s.push(symbol) elif s.isEmpty(): balanced = False else: s.pop() index += 1 if balanced and s.isEmpty(): return True else: return False
def divideBy2(decNumber): remStack = Stack() while decNumber > 0: rem = decNumber % 2 remStack.push(rem) decNumber = decNumber // 2 binString = "" while not remStack.isEmpty(): binString = binString + str(remStack.pop()) return binString
def divideByN(decNumber, N=2): digits = "0123456789ABCD" remStack = Stack() while decNumber > 0: rem = decNumber % N remStack.push(digits[rem]) decNumber = decNumber // N binString = "" while not remStack.isEmpty(): binString = binString + str(remStack.pop()) return binString
def parCheckers(sybomlString): s = Stack() balanced = True index = 0 while index < len(sybomlString) and balanced: symbol = sybomlString[index] if symbol in '([{': s.push(symbol) elif s.isEmpty(): balanced = False else: top = s.pop() if not matches(top, symbol): return False index += 1 if balanced and s.isEmpty(): return True else: return False
def infixTopostfix(infix): prec = {"*": 3, "/": 3, "+": 2, "-": 2, "(": 1, ")": 1} opstack = Stack() postfixlist = [] infixlist = infix.split() for token in infixlist: 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)
def infixToPostfit(infiexpr): prec = {} prec['*'] = 3 prec['/'] = 3 prec['+'] = 2 prec['-'] = 2 prec['('] = 1 opStack = Stack() postfitList = [] tokenList = infiexpr.split() print(tokenList) for token in tokenList: if token in "ABCDEFG" or token in "0123456789": postfitList.append(token) elif token == '(': opStack.push(token) elif token == ')': topToken = opStack.pop() while topToken != '(': postfitList.append(topToken) topToken = opStack.pop() else: while (not opStack.isEmpty()) and (prec[opStack.peek()] >= prec[token]): postfitList.append(opStack.pop()) opStack.push(token) while not opStack.isEmpty(): postfitList.append(opStack.pop()) return " ".join(postfitList)
import sys sys.path.append('../') from basic.stack import Stack s = Stack() print(s.isEmpty()) s.push(4) s.push('dog') print(s.peek()) s.push(True) print(s.size()) print(s.isEmpty()) s.push(8.4) print(s.pop()) print(s.pop()) print(s.size())
def __init__(self, name): self.stack = Stack() self.label = name
def setUp(self): self.stack = Stack()
class TestStack(TestCase): def setUp(self): self.stack = Stack() def test_is_empty(self): # stack = Stack() self.assertTrue(self.stack.is_empty()) def test_size(self): self.assertEqual(0, self.stack.size()) def test_pop(self): # stack = Stack() self.stack.push(1) self.assertEqual(1, self.stack.pop()) def test_top(self): self.stack.push(1) self.stack.push(2) self.assertEqual(2, self.stack.top()) def test_push(self): self.stack.push(1) self.assertEqual(1, self.stack.pop())