Пример #1
0
class LinkedQueue(Singlellist):
    """FIFO queue implementation using a single linked list for storage"""
    def __init__(self):
        """Create an empty queue"""
        self._data = Singlellist()

    def __len__(self):
        """Return the number of elements in the queue"""
        return len(self._data)

    def is_empty(self):
        """Return True if the queue is empty"""
        return len(self._data) == 0

    def first(self):
        """Return(but do not remove) the element at the front if the queue"""
        if self.is_empty():
            raise Empty("Queue is empty")
        return self._data.head.value

    def dequeue(self):
        """Remove and return the first element if the queue(i.e., FIFO).

        Raisee Empty exception if the dueue is empty"""
        if self.is_empty():
            raise Empty("Queue is empty")
        node = self._data.remove_first()
        return node.value

    def enqueue(self, e):
        """Add an element to the back of the queue"""
        self._data.add_last(e)
Пример #2
0
def reverse_sll_2(sll: Singlellist) -> Singlellist:
    if len(sll) == 1:
        return sll
    head = sll.remove_first()
    head.next = None
    reverse_sll_2(sll).tail.next = head
    sll.tail = head
    return sll
Пример #3
0
 def test_remove_first(self):
     sll = Singlellist([1, 2, 3])
     sll.remove_first()
     self.assertEqual(sll[0], 2)
     sll.remove_first()
     self.assertEqual(sll[0], 3)
     sll.remove_first()
     self.assertEqual(sll.is_empty(), True)
Пример #4
0
def count_num(sll: Singlellist) -> int:
    if sll.head is None:
        return 0
    sll.remove_first()
    return 1 + count_num(sll)
Пример #5
0
def reverse_sll_1(sll: Singlellist) -> Singlellist:
    if len(sll) == 1:
        return sll
    head = sll.remove_first()
    reverse_sll_1(sll).add_last(head.value)
    return sll
Пример #6
0
class LinkedStack(Singlellist):
    """
    LIFO Srtack implementation using Python list as underlyinh storage.
    """
    def __init__(self):
        """Create and empty stack.
        """
        self._data = Singlellist()

    def __len__(self):
        """Return the number of elements in the stack.
        Time Complexity: O(1)
        """
        return len(self._data)

    def __repr__(self):
        """
        Show the stack properly.
        Time Complexity: O(1)
        """
        if self.is_empty():
            s1 = '| ' + "".center(5) + ' |' + '\n'
            s2 = '-' * 9
            return s1 + s2
        else:
            s = []
            for i in range(len(self._data) - 1, -1, -1):
                ele = self._data[i]
                s1 = '| ' + ele.__repr__().center(5) + ' |' + '\n'
                s2 = '-' * 9 + '\n'
                s.append(s1 + s2)
            return ''.join(s)

    def is_empty(self):
        """Return True if the stack is empty
        Time Complexity: O(1)
        """
        return len(self._data) == 0

    def push(self, e):
        """Add element to the top of the stack
        Time Complexity: O(1)*
        Note: "*" in here means amortization
        """
        self._data.add_first(e)

    def top(self):
        """
        Return (but not remove) at the top of the stack.
        Raise Empty exception if the stack in empty.
        Time Complexity: O(1)
        """
        if self.is_empty():
            raise Empty("Stack in empty!")
        return self._data.head.value

    def pop(self):
        """
        Remove and return the element from the top of the stack(LIFO)
        Raise Empty exception if the stack is empty.
        Time Complexity: O(1)*
        """
        if self.is_empty():
            raise Empty("Stack is empty!")
        ele = self._data.head.value
        self._data.remove_first()
        return ele