示例#1
0
文件: p3_3.py 项目: camirmas/ctci
 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)
示例#2
0
def test_stack_is_empty():
    s = Stack()

    assert s.is_empty()

    s.push(1)

    assert not s.is_empty()
示例#3
0
def test_stack_peek():
    s = Stack()

    assert s.peek() is None

    s.push(1)

    assert s.peek() == 1
示例#4
0
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
示例#5
0
def test_stack_init():
    s = Stack()

    s.top = None
示例#6
0
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
示例#7
0
 def __init__(self):
     self.s1 = Stack()
     self.s2 = Stack()
示例#8
0
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()