def __init__(self, tree): """ (MWayTree.Iterator, MWayTree) -> None Constructs an iterator for the given M-way tree. """ super(MWayTree.Iterator, self).__init__(tree) self._position = 0 self._stack = StackAsLinkedList()
def __init__(self, tree): """ (Tree.Iterator, Tree) -> None Constructs an iterator for the given tree. """ super(Tree.Iterator, self).__init__(tree) self._stack = StackAsLinkedList() if not tree.isEmpty: self._stack.push(tree)
def parsePostfix(input): """ (File) -> ExpresionTree Parses the given file into an expression tree. """ stack = StackAsLinkedList() for line in input.readlines(): for word in line.split(): if word == "+" or word == "-" \ or word == "*" or word == "/": result = ExpressionTree(word) result.attachRight(stack.pop()) result.attachLeft(stack.pop()) stack.push(result) else: stack.push(ExpressionTree(word)) return stack.pop()
def calculator(input, output): """ (File, File) -> None A very simple reverse-Polish calculator. """ stack = StackAsLinkedList() for line in input.readlines(): for word in line.split(): if word == "+": arg2 = stack.pop() arg1 = stack.pop() stack.push(arg1 + arg2) elif word == "*": arg2 = stack.pop() arg1 = stack.pop() stack.push(arg1 * arg2) elif word == "=": arg = stack.pop() output.write(str(arg) + "\n") else: stack.push(int(word))
def __init__(self): self.stack = StackAsLinkedList()
def __init__(self, numOfStack): self.stacks = [StackAsLinkedList() for i in range(numOfStack)]