class Stack:
    def __init__(self):
        self.items = LinkedList()

    def getItems(self):
        return self.items

    def size(self):
        return self.items.size()

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def isEmpty(self):
        return self.items.isEmpty()
Beispiel #2
0
class ListStack(Stack):
    """Creates a class of stack using the linked list.

    Attributes:
        list: the linked list of the data.
    """

    def __init__(self):
        """Initializes the class.
        """
        super(ListStack, self).__init__()
        self.__list = LinkedList()

    def purge(self):
        """Cleans the stack.
        """
        self.__list.purge()
        self._count = 0

    def push(self, obj):
        """Pushes an object into the stack.
        """
        self.__list.append(obj)
        self._count += 1

    def pop(self):
        """Pop an object up the stack.
        """
        if self._count == 0: raise IndexError
        result = self.__list.head.datum
        self.__list.remove(result)
        self._count -= 1
        return result

    def top(self):
        """Get the top object of a stack.
        """
        if self._count == 0: raise IndexError
        return self.__list.head.datum
Beispiel #3
0
class ListQueue(Queue):
    """Creates a class of queue using the list.

    Attributes:
        list: the list of the data.
    """

    def __init__(self):
        """Initializes the class.
        """
        super(ListQueue, self).__init__()
        self._list = LinkedList()

    def purge(self):
        """Cleans the queue.
        """
        self._list.purge()
        self._count = 0

    def head(self):
        """Gets the head element.
        """
        if self._count == 0: raise IndexError
        return self._list.head.datum

    def enqueue(self, obj):
        """Enqueue.
        """
        self._list.append(obj)
        self._count += 1

    def dequeue(self):
        """Dequeue.
        """
        result = self.head()
        self._list.remove(result)
        self._count -= 1
        return result