linked queue Algorithm

The linked queue algorithm is a data structure that utilizes the principles of linked lists to implement a queue. A queue is a collection of elements that supports two primary operations: enqueue (insertion of an element at the rear end) and dequeue (removal of an element from the front end). The algorithm ensures that elements are processed in the order they are added, following the First-In-First-Out (FIFO) rule. In a linked queue, each element in the queue is a node that consists of the data and a reference (or pointer) to the next node in the sequence. The linked queue has two pointers, 'front' and 'rear', which point to the first and last elements in the queue, respectively. To perform the enqueue operation in a linked queue, a new node is created with the data to be inserted, and the 'next' reference of the new node is set to null. If the queue is empty, the 'front' and 'rear' pointers are set to the new node. If the queue is not empty, the 'next' reference of the current rear node is set to the new node, and the 'rear' pointer is updated to point to the new node. On the other hand, the dequeue operation involves removing the front element from the queue. The 'front' pointer is updated to point to the next node in the sequence, and the removed element's 'next' reference is set to null. If the queue becomes empty after dequeuing, the 'rear' pointer is also set to null. This algorithm ensures constant time complexity O(1) for both enqueue and dequeue operations, providing an efficient solution for managing elements in a queue.
""" A Queue using a Linked List like structure """
from typing import Any, Optional


class Node:
    def __init__(self, data: Any, next: Optional["Node"] = None):
        self.data: Any = data
        self.next: Optional["Node"] = next


class LinkedQueue:
    """
    Linked List Queue implementing put (to end of queue),
    get (from front of queue) and is_empty

    >>> queue = LinkedQueue()
    >>> queue.is_empty()
    True
    >>> queue.put(5)
    >>> queue.put(9)
    >>> queue.put('python')
    >>> queue.is_empty();
    False
    >>> queue.get()
    5
    >>> queue.put('algorithms')
    >>> queue.get()
    9
    >>> queue.get()
    'python'
    >>> queue.get()
    'algorithms'
    >>> queue.is_empty()
    True
    >>> queue.get()
    Traceback (most recent call last):
        ...
    IndexError: get from empty queue
    """

    def __init__(self) -> None:
        self.front: Optional[Node] = None
        self.rear: Optional[Node] = None

    def is_empty(self) -> bool:
        """ returns boolean describing if queue is empty """
        return self.front is None

    def put(self, item: Any) -> None:
        """ append item to rear of queue """
        node: Node = Node(item)
        if self.is_empty():
            # the queue contains just the single element
            self.front = node
            self.rear = node
        else:
            # not empty, so we add it to the rear of the queue
            assert isinstance(self.rear, Node)
            self.rear.next = node
            self.rear = node

    def get(self) -> Any:
        """ returns and removes item at front of queue """
        if self.is_empty():
            raise IndexError("get from empty queue")
        else:
            # "remove" element by having front point to the next one
            assert isinstance(self.front, Node)
            node: Node = self.front
            self.front = node.next
            if self.front is None:
                self.rear = None

            return node.data

LANGUAGE:

DARK MODE: