Beispiel #1
0
def test_peek():
    nums = Stack()
    nums.top = Node(13)

    actual = nums.peek()
    expected = 13

    assert actual == expected
Beispiel #2
0
def test_pop():
    nums = Stack()

    # Stand Alone testing:
    nums.top = Node(1)
    actual = nums.pop()
    expected = 1
    assert actual == expected

    # Using methods from Stack class:
    nums.push(100, 13)
    actual = nums.pop()
    expected = 13
    assert actual == expected
Beispiel #3
0
def test_push_one_value():
    nums = Stack()
    nums.push(1)
    assert nums.top.value == 1
Beispiel #4
0
def test_peek_on_empty_stack():
    nums = Stack()
    actual = nums.peek()
    expected = 'Stack is empty'
    assert actual == expected
Beispiel #5
0
def test_emptying_stack_with_multi_pop():
    nums = Stack()

    # Stand Alone testing:
    nums.top = Node(1)
    temp = nums.top
    nums.top = Node(13)
    nums.top.next = temp

    nums.pop()
    nums.pop()

    actual = nums.pop()
    expected = 'Stack is empty'
    assert actual == expected

    # Using methods from Stack class:
    nums.push(100, 13)
    nums.pop()
    nums.pop()
    actual = nums.pop()
    expected = 'Stack is empty'
    assert actual == expected
Beispiel #6
0
def test_create_empty_stack():
    nums = Stack()
    assert nums.top == None
Beispiel #7
0
def test_pop_from_empty_stack():
    nums = Stack()
    actual = nums.pop()
    expected = 'Stack is empty'
    assert actual == expected
Beispiel #8
0
def test_push_multiple_values():
    nums = Stack()
    nums.push(1, 3, 5, 9)
    assert nums.top.value == 9
    assert nums.__str__() == 'top->9->5->3->1-> NULL'
Beispiel #9
0
    def __init__(self):

        self.front_stack = Stack()
        self.rear_stack = Stack()
        self.len = 0
Beispiel #10
0
class PseudoQueue():
    def __init__(self):

        self.front_stack = Stack()
        self.rear_stack = Stack()
        self.len = 0

    def enqueue(self, value):
        """
        Push Node(value) to front_stack
        """
        self.len += 1
        self.front_stack.push(value)

    def dequeue(self):
        """
        Dequeue an item from the queue.
         1) If both stacks are empty, return stack is empty.
         2) If rear_stack is empty
            While front_stack is not empty, push everything from front_stack to rear_stack.
         3) Pop the element from rear_stack and return it.
        """
        if self.rear_stack.isEmpty():
            while self.len > 0:
                self.rear_stack.push(self.front_stack.pop())
                self.len -= 1
            output = self.rear_stack.pop()

            while True:
                self.front_stack.push(self.rear_stack.pop())
                self.len += 1
                if self.rear_stack.isEmpty():
                    return output
        else:
            return "stack is empty!"

    def __str__(self):
        output = 'Rear ->'
        if self.front_stack.isEmpty():
            current = self.rear_stack.top
        else:
            current = self.front_stack.top
        while current:
            output += f"{{{current.value}}} -> "
            current = current.next
        output += " Front"
        return output