def is_balanced(parens: str): """Checks to see if a string of parens are balanced""" stack = Stack() try: for paren in parens: if stack.is_empty() or (not stack.is_empty() and paren == stack.peek()): stack.push(paren) else: stack.pop() return stack.is_empty() except IndexError: return False
class MyQueue: def __init__(self): self.stack1 = Stack(0) self.stack2 = Stack(0) self.stack1.pop() self.stack2.pop() self.use1 = True def add(self, value): if self.use1: self.stack1.push(value) else: self.stack2.push(value) def remove(self): if self.use1: self.use1 = not self.use1 while (not self.stack1.isEmpty()): self.stack2.push(self.stack1.pop()) return self.stack2.pop() else: self.use1 = not self.use1 while (not self.stack2.isEmpty()): self.stack1.push(self.stack2.pop()) return self.stack1.pop()
def reverse_level_order(self): to_traverse = [self.root] stack = Stack(100) while len(to_traverse) > 0: node = to_traverse[0] data = node.value stack.push(Node(data)) if node.right: to_traverse.append(node.right) if node.left: to_traverse.append(node.left) to_traverse.pop(0) traversal = [] for x in range(stack.size): traversal.append(stack.pop().get_value()) return traversal
def postfixeval(list): values = Stack() for token in list: if token in "0123456789": values.push(int(token)) else: if token in "()": continue else: op1 = values.pop() op2 = values.pop() result = calculate(token,op1,op2) values.push(result) return values.pop() print values
def parChecker(string): s=Stack() balanced=True index=0 while index<len(string) and balanced: symbol=string[index] if symbol in "([{": s.push(symbol) else: if s.isEmpty(): balanced=False else: top=s.pop() ## we have to see the top that we have removed matches the symbol if not matches(top,symbol): balanced=False index+=1 if balanced and s.isEmpty(): return True else: return False
def reverse_levelorder_print(self, start): if start is None: return queue = Queue() stack = Stack() queue.enqueue(start) traversal = "" while len(queue) > 0: node = queue.dequeue() stack.push(node) if node.right: queue.enqueue(node.right) if node.left: queue.enqueue(node.left) while len(stack) > 0: node = stack.pop() traversal += str(node.value) + "-" return traversal
def dfsPrint(self, start): if start is None: return stack = Stack() stack.push(start) traversal = "" while len(stack) > 0: traversal += str(stack.peek().value) + "->" node = stack.pop() if node.left: stack.push(node.left) if node.right: stack.push(node.right) return traversal
def dfsSize(self): if self.root is None: return 0 stack = Stack() head = self.root if head is not None: stack.push(head) count = 0 while len(stack) > 0: count += 1 node = stack.pop() if node.left is not None: stack.push(node.left) if node.right is not None: stack.push(node.right) return count
def reverse_stack(stack): new_stack=Stack() while(stack.isEmpty() is not True): new_stack.push(stack.pop()) return new_stack
from Stacks import Stack def reverse(stack): if stack.isEmpty()!=True: temp=stack.pop() reverse(stack) print(temp) insert_at_bottom(stack,temp) def reverse_stack(stack): new_stack=Stack() while(stack.isEmpty() is not True): new_stack.push(stack.pop()) return new_stack def insert_at_bottom(stack,element): if stack.isEmpty(): stack.push(element) else: temp=stack.pop() insert_at_bottom(stack,element) stack.push(temp) if __name__=="__main__": array=Stack() array.push(1) array.push(2) array.push(3) array.push(4) array.push(5) array.push(6) array.print_stack() reverse(array) array.print_stack()
class Stack(object): def __init__(self): self.ll = LinkedList() def isEmpty(self): return self.ll.isEmpty() def push(self, data): self.ll.addNode(Node(data)) def pop(self): self.ll.delEnd() def peek(self): return self.ll.returnEndElement() def size(self): return self.ll.size() s = Stack() s.push(133) s.push(24) s.push(123) s.push(12124) s.pop() print s.peek() print s.size()