def sort_stack(stack): tmp_stack = Stack() while not stack.is_empty(): val = stack.pop().data while not tmp_stack.is_empty(): if val < tmp_stack.peek(): # print("val (", val, ") < tmp_stack.peek() (", tmp_stack.peek(), ")") tmp = tmp_stack.pop().data stack.push(tmp) else: break tmp_stack.push(val) # print("tmp_stack : ", end="") # tmp_stack.print() # print("stack : ", end="") # stack.print() # # print("stack is empty? -", stack.is_empty()) while not tmp_stack.is_empty(): # val = tmp_stack.pop().data # print("val pushed back is ", val) # stack.push(val) stack.push(tmp_stack.pop().data)
def pop(self): last_stack = Stack() last_stack = self.get_last_stack() if last_stack: return last_stack.pop() if last_stack.is_empty(): self.stacks.remove(last_stack) return
def pop_at(self, index): stack_idx = Stack() stack_idx = self.stacks[index] popped = stack_idx.pop() if stack_idx.is_empty(): self.stacks.remove(stack_idx) # elif len(self.stacks) > index + 1: for i in range(index+1, len(self.stacks), 1): stack_idx.push(self.stacks[i].pop()) stack_idx = self.stacks[i] return popped
class MyQueue: def __init__(self): self.in_stack = Stack() self.out_stack = Stack() def push(self, value): self.in_stack.push(value) def pop(self): while self.in_stack.is_empty() is False: self.out_stack.push(self.in_stack.pop()) return self.out_stack.pop()