def push(self, data: T): if self.current_stack is None: new_stack = Stack() self.stacks.append(new_stack) self.current_stack = new_stack self.current_stack.push(data) elif self.current_stack.length == self.capacity: new_stack = Stack() self.stacks.append(new_stack) self.current_stack = new_stack self.current_stack.push(data) else: self.current_stack.push(data)
def test_stack_is_empty(): s = Stack() assert s.is_empty() s.push(1) assert not s.is_empty()
def test_stack_peek(): s = Stack() assert s.peek() is None s.push(1) assert s.peek() == 1
def test_stack_push(): s = Stack() s.push(1) assert s.top.data == 1 assert s.top.next == None s.push(2) assert s.top.data == 2 assert s.top.next.data == 1
def test_stack_init(): s = Stack() s.top = None
def test_stack_pop(): s = Stack() assert s.pop() is None s.push(1) assert s.pop() == 1 assert s.pop() is None s.push(1) s.push(2) s.push(3) assert s.pop() == 3 assert s.top.data == 2 assert s.top.next.data == 1
def __init__(self): self.s1 = Stack() self.s2 = Stack()
class MyQueue: def __init__(self): self.s1 = Stack() self.s2 = Stack() def add(self, data): self.s1.push(data) def remove(self): if self.is_empty(): return if not self.s2.is_empty(): return self.s2.pop() current = self.s1.pop() while not self.s1.is_empty(): self.s2.push(current) current = self.s1.pop() return current def is_empty(self): return self.s1.is_empty() and self.s2.is_empty()