class QueueTwoStack: def __init__(self): self.stack1 = Stack() self.stack2 = Stack() def enqueue(self, n): self.stack1.push(n) def dequeue(self): if self.stack2.isEmpty(): while not self.stack1.isEmpty(): self.stack2.push(self.stack1.pop()) return self.stack2.pop()
def eval_postfix(exp): n = len(exp) if n < 1: return None stack = Stack() for i in range(n - 1, -1, -1): if isOperand(exp[i]): stack.push(exp[i]) else: ele1 = stack.pop() ele2 = stack.pop() print(ele1, ele2, exp[i]) n = eval(ele2 + exp[i] + ele1) stack.push(str(int(n))) result = stack.pop() return result
class QueueOneStack: def __init__(self): self.stack = Stack() def enqueue(self, n): self.stack.push(n) def dequeue(self): if self.stack.isEmpty(): return None node = self.stack.pop() if self.stack.isEmpty(): print(node) else: self.dequeue() self.stack.push(node)
def validatePren(data): stack = Stack() opening = set('([{') closing = set(')]}') matches = { ')' : '(', ']' : '[', '}' : '{' } for i in data: if i in opening: stack.push(i) elif i in closing: last = stack.pop() if matches[i] != last: return False if stack.isEmpty(): return True return False
def __init__(self): self.stack1 = Stack() self.stack2 = Stack()
def __init__(self): self.stack = Stack()
class StackWithMin: def __init__(self): self.stack = Stack() self.minStack = Stack() def push(self, n): cur = self.minStack.peek() if cur is None or cur > n: self.stack.push(n) self.minStack.push(n) else: self.stack.push(n) self.minStack.push(cur) def pop(self): tmp = self.stack.pop() min = self.minStack.pop() return tmp def getMin(self): return self.minStack.peek()