26: Implement a queue using an array or linked list.
⏳⌚ 00:00:00

Question:-

Implement a queue using an array or linked list.
Example 1:
Input: according to question
Output:
Queue Front : 40
Queue Rear : 50

Steps to solve:-

1. struct QNode:-
This structure defines the individual nodes that will make up the linked list. Each node contains an integer value (data) and a pointer to the next node (next).
2. struct Queue:-
This structure represents the queue itself and contains two pointers, front and rear, which point to the front and rear nodes of the queue, respectively. Initially, both front and rear are set to NULL, indicating an empty queue.
3. enQueue(int x):-
This method is used to add an element to the rear of the queue. It creates a new node with the provided integer value x and adds it to the rear of the queue by updating the rear pointer. If the queue is empty, it also updates the front pointer.
4. deQueue():-
This method is used to remove an element from the front of the queue. It checks if the queue is empty (when front is NULL) and returns if it is. Otherwise, it moves the front pointer to the next node and deletes the previous front node. If the front node becomes NULL after this operation (indicating that the queue is now empty), it updates the rear pointer as well.
5. main():-
In the main function, an instance of the Queue data structure, q, is created. Elements are then added to the queue using enQueue and removed using deQueue. The program prints the front and rear elements of the queue at the end of its execution.

solution->

View Code 1
Time Complexity = O(1)
Space Complexity = O(n)