Implement Queue using Linked List
Learning DSA practice problem on Onlearn.
Difficulty: medium.
Topics: Implementation of Queue using Singly Linked List, Data Structures, Queue, Linked List, Node, Pointer, Singly Linked List, Time Complexity, Space Complexity, Functions, stack operations, queue operations, node operations, queue, conditional logic, tracking values, data structures, linked list, Queue (FIFO), Queue Operations, Static vs. Dynamic Allocation, Node.
"""Implement Queue using Singly Linked List Problem Statement: Implement a Queue data structure using a singly linked list. Your implementation should support the following operations: Enqueue(value): Adds an element value to the rear of the queue. Dequeue(): Removes and returns the element from the front of the queue. If the queue is empty, handle this appropriately (e.g., print an error message and return a sentinel value like 1). Peek(): Returns the element at the front of the queue without removing it. If the queue is empty, handle this appropriately (e.g., print an error message and return a sentinel value like 1). Empty(): Returns true if the queue is empty, false otherwise. Size(): Returns the current number of elements in the queue. Input Specification: Operations will be performed on your implemented Queue. Output Specification: The Dequeue() and Peek() methods should return the appropriate values or indicate an empty queue. Enqueue() adds the value and typically prints a confirmation. Size() returns the integer size. Empty() returns a boolean. Constraints: No explicit constraints on the range of values or number of operations are provided, so assume standard integer ranges and reasonable number of operations. Sample Test Cases: Input (Sequence of Operations): 1. Enqueue(10) 2. Enqueue(20) 3. Enqueue(30) 4. Enqueue(40) 5. Enqueue(50) 6. Dequeue() 7. Size() 8. Peek() Expected Output (based on method calls): 10 Inserted into Queue 20 Inserted into Queue 30 Inserted into Queue 40 Inserted into Queue 50 Inserted into Queue 10 Removed From Queue The size of the Queue is 4 The Peek element of the Queue is 20 """