Пример #1
0
 def test_LinkedList(self):
     linked_list = LinkedList()
     self.assertEqual(linked_list.size(), 0)
     linked_list_num = LinkedList(Node(1))
     self.assertEqual(linked_list_num.pop(), 1)
     self.assertEqual(linked_list_num.push(1), None)
     self.assertEqual(linked_list_num.head.data, 1)
     self.assertEqual(linked_list_num.head.next, None)
     self.assertEqual(linked_list_num.push(2), None)
     self.assertEqual(linked_list_num.pop(), 2)
     self.assertEqual(linked_list_num.size(), 1)
     self.assertEqual(linked_list_num.is_empty(), False)
Пример #2
0
 def test_5(self):
     """ test pop() """
     l = LinkedList()
     n1 = Node(5)
     n2 = Node(3)
     l.append(n1)
     l.append(n2)
     n = l.pop()
     self.assertEqual(n._value, 5)
Пример #3
0
class StackLinked:
    """ A stack data structure implemented with a singly linked list.

    Attributes:
        capacity(int): The size limiter of the stack.
        head(LinkedList): The LinkedList that stores our nodes.
        num_items(int): The number of items in the stack.
    """
    def __init__(self, capacity):
        self.capacity = capacity
        self.items = LinkedList()
        self.num_items = 0

    def __repr__(self):
        return "StackLinked({}, {}, {})".format(self.capacity, self.items,
                                                self.num_items)

    def __eq__(self, other):
        return (isinstance(self, StackLinked) == isinstance(
            other, StackLinked) and self.capacity == other.capacity
                and self.items == other.items
                and self.num_items == other.num_items)

    def is_empty(self):
        """ Checks if the stack is empty.

        Args:
            self(StackLinked): The current object referencing this method.

        Returns:
            boolean: True if the stack is empty, else false.
        """
        return self.num_items == 0

    def is_full(self):
        """ Checks if the stack is full.

        Args:
            self(StackLinked): The current object referencing this method.

        Returns:
            boolean: True if the stack is full, else false.
        """
        return self.num_items == self.capacity

    def push(self, item):
        """ Add an item to the stack. If the stack is full return Error.

        Args:
            self(StackLinked): The current object referencing this method.
            item(Any): The data that is going to be pushed to the stack

        Returns:
            None: Does not return anything
        """
        if self.is_full():
            raise IndexError
        self.items.push(item)
        self.num_items += 1

    def pop(self):
        """ Removes an item from the top of the stack. If the stack is empty
        return None.

        Args:
            self(StackLinked): The current object referencing this method.

        Returns:
            Any: The data that was removed from the top of the stack.
        """
        if self.is_empty():
            raise IndexError
        self.num_items -= 1
        return self.items.pop()

    def peek(self):
        """ The item at the top of the Stack.

        Args:
            self(StackLinked): The current object referencing this method.

        Returns:
            *: The data at the top of the Stack.
        """
        return self.items.head.get_data()

    def size(self):
        """ The size of the StackLinked.

        Args:
            self(StackLinked): The current object referencing this method.

        Returns:
            int: Number of items in the Stack.
        """
        return self.items.num_items