def printStack(stack): #how does this temp stack help replace the original? Draw it out! temp = Stack() while not stack.isEmpty(): temp.push(stack.peek()) print(stack.peek()) #could also say print temp.peek() stack.pop() while not temp.isEmpty(): stack.push(temp.pop())
def testStack(): myStack = Stack() print("test Stack.pop()") result = myStack.pop() print("empty stack pop return", type(result)) print("push data: test string, hello") myStack.push("test string") myStack.push("hello") print("Stack:", myStack._Stack__list) print("Stack size:", myStack._Stack__size) result = myStack.pop() print("pop stack return:", result) print("Stack:", myStack._Stack__list) print("Stack size:", myStack._Stack__size)
class Display: def __init__(self): self.frm_display = tk.Frame() self.frm_display.pack(pady=5) self.disp = tk.Label(master=self.frm_display, bg="#343434", fg="White", font="Ubuntu 32", anchor='e') self.disp.config(width=16, height=3) self.disp.pack() self.history = Stack() def updateDisplay(self, text): self.disp.config(text=(self.disp["text"] + text)) self.history.push(self.disp["text"]) def displayAnswer(self, text): self.disp.config(text=text)
def post_order(node): st1 = Stack() st2 = Stack() if node != None: st1.push(node) while not st1.is_empty(): n = st1.pop() st2.push(n) if n.left != None: st1.push(n.left) if n.right != None: st1.push(n.right) while not st2.is_empty(): n = st2.pop() print(n.data, end=' ') print() return
def evaluatePostix(pfExpression): ops = { "+": operator.add, "\u2013": operator.sub, "/": operator.truediv, "*": operator.mul, "^": math.pow } functions = { "sin": math.sin, "cos": math.cos, "tan": math.tan, "ln": math.log, "sqrt": math.sqrt, "$": unaryNegative } stack = Stack() for i in range(len(pfExpression)): elem = pfExpression[i] if elem in functions.keys(): num = float(stack.pop()) stack.push(functions[elem](num)) elif elem in ops.keys(): a = float(stack.pop()) b = float(stack.pop()) r = ops[elem](b, a) stack.push(r) else: stack.push(float(elem)) result = stack.pop() return round(result, 10)
def pre_order(node): stk = Stack() if node != None: stk.push(node) while not stk.is_empty(): n = stk.pop() print(n.data, end=' ') if n.right != None: stk.push(n.right) if n.left != None: stk.push(n.left) print() return
def convertToRPN(expression): precedence = { "+": 2, "\u2013": 2, "*": 3, "/": 3, "^": 4, "sqrt": 4, "sin": 1, "cos": 1, "tan": 1, "ln": 1, "$": 5 } functions = ["sin", "cos", "tan", "ln", "$"] exp = parse(expression) operators = Stack() output = Queue() for elem in exp: if isfloat(elem) or elem.isdecimal(): print(elem) output.push(elem) elif elem == "(": operators.push(elem) elif elem == ")": while not operators.isEmpty(): topStack = operators.peep() if topStack == "(": operators.pop() break else: output.push(operators.pop()) elif elem in functions: operators.push(elem) else: while not operators.isEmpty(): topStack = operators.peep() if topStack == "(": operators.pop() elif precedence[elem] <= precedence[topStack]: output.push(operators.pop()) else: break operators.push(elem) while not operators.isEmpty(): output.push(operators.pop()) return output.returnQueueAsList()