📖 Scenario: Imagine you are managing a line of customers waiting to buy tickets. You want to add new customers to the end of the line efficiently. This line is represented using a linked list where each node holds a customer's ID.
🎯 Goal: You will build a simple linked list queue and add new customers to the end using the enqueue operation.
📋 What You'll Learn
Create a Node struct with an integer data and a pointer to the next Node
Create a Queue class with pointers to the front and rear nodes
Implement an enqueue method to add a new node at the rear
Print the queue contents after enqueue operations
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting, print jobs, or tasks waiting to be processed.
💼 Career
Understanding linked list queues is important for software developers working on systems that require efficient data processing and task scheduling.
Progress0 / 4 steps
1
Create Node struct and Queue class with front and rear pointers
Create a struct Node with an int data and a Node* next. Then create a class Queue with public Node* front and Node* rear pointers initialized to nullptr in the constructor.
DSA C++
Hint
Use a constructor inside Node to set data and initialize next to nullptr. In Queue, set front and rear to nullptr in the constructor.
2
Add enqueue method to Queue class
Inside the Queue class, add a public method void enqueue(int val) that creates a new Node with val. If rear is nullptr, set both front and rear to the new node. Otherwise, set rear->next to the new node and update rear to the new node.
DSA C++
Hint
Remember to handle the case when the queue is empty by setting both front and rear to the new node. Otherwise, link the new node at the end and update rear.
3
Create main function and enqueue values
Write a main function that creates a Queue object called q. Use q.enqueue(10), q.enqueue(20), and q.enqueue(30) to add these values to the queue.
DSA C++
Hint
Create the queue object and call enqueue three times with the values 10, 20, and 30.
4
Print the queue contents after enqueue
In the main function, after enqueueing, use a Node* pointer called temp to traverse from q.front to nullptr. Print each node's data followed by " -> ". After the loop, print "null".
DSA C++
Hint
Use a while loop to move through the linked list starting at q.front. Print each data followed by " -> ". After the loop, print "null".
Practice
(1/5)
1. What does the enqueue operation do in a linked list implementation of a queue?
easy
A. Adds an element at the end (rear) of the linked list
B. Removes an element from the front of the linked list
C. Deletes the entire linked list
D. Adds an element at the front (head) of the linked list
Solution
Step 1: Understand enqueue operation in queue
Enqueue means adding an element to the queue.
Step 2: Identify linked list position for enqueue
In a linked list queue, enqueue adds at the rear (end) to maintain FIFO order.
Final Answer:
Adds an element at the end (rear) of the linked list -> Option A
Quick Check:
Enqueue = Add at rear [OK]
Hint: Enqueue always adds at the rear in linked list queue [OK]
Common Mistakes:
Confusing enqueue with dequeue operation
Adding element at front instead of rear
Thinking enqueue removes elements
2. Which of the following is the correct syntax to create a new node and enqueue it in a linked list queue in C++?