Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
def test_stack_is_empty():
    s = Stack()

    assert s.is_empty()

    s.push(1)

    assert not s.is_empty()
Ejemplo n.º 3
0
def test_stack_peek():
    s = Stack()

    assert s.peek() is None

    s.push(1)

    assert s.peek() == 1
Ejemplo n.º 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
Ejemplo n.º 5
0
def test_stack_init():
    s = Stack()

    s.top = None
Ejemplo n.º 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
Ejemplo n.º 7
0
 def __init__(self):
     self.s1 = Stack()
     self.s2 = Stack()
Ejemplo n.º 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()