📖 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.
🎯 Goal: Build a simple linked list queue and add new elements to the end using the enqueue operation.
📋 What You'll Learn
Create a Node class with data and next attributes
Create a Queue class with front and rear attributes initialized to None
Implement an enqueue method to add nodes at the end of the queue
Print the queue elements from front to rear after enqueue operations
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting or tasks waiting to be processed.
💼 Career
Understanding linked list queues helps in software development for managing tasks, requests, or data streams efficiently.
Progress0 / 4 steps
1
Create Node and Queue classes
Create a class called Node with an __init__ method that takes data and sets self.data = data and self.next = None. Also create a class called Queue with an __init__ method that sets self.front = None and self.rear = None.
DSA Python
Hint
Define two classes: Node for each element and Queue to manage the linked list.
2
Add enqueue method to Queue
Inside the Queue class, create a method called enqueue that takes self and data. Inside it, create a new Node with data. If self.rear is None, set both self.front and self.rear to the new node. Otherwise, set self.rear.next to the new node and then update self.rear to the new node.
DSA Python
Hint
Check if the queue is empty by testing self.rear. If empty, set both front and rear to the new node. Otherwise, link the new node at the end and update rear.
3
Add method to print queue elements
Inside the Queue class, create a method called print_queue that starts from self.front and prints each node's data followed by ' -> ' until it reaches None. After the loop, print 'None'.
DSA Python
Hint
Use a loop to go through each node starting from front and print the data with arrows until the end.
4
Enqueue elements and print the queue
Create a Queue object called q. Use q.enqueue(10), q.enqueue(20), and q.enqueue(30) to add three elements. Then call q.print_queue() to display the queue.
DSA Python
Hint
Create the queue, add three numbers, then print the queue to see the order.
Practice
(1/5)
1. What does the enqueue operation do in a linked list implementation of a queue?
easy
A. Adds a new element at the end (tail) of the linked list
B. Removes the first element from the linked list
C. Adds a new element at the beginning (head) of the linked list
D. Deletes the last element from the linked list
Solution
Step 1: Understand enqueue operation in queue
Enqueue means adding an element to the queue, which is done at the end (tail) to maintain FIFO order.
Step 2: Linked list enqueue behavior
In a linked list, adding at the tail means creating a new node and linking it as the last node.
Final Answer:
Adds a new element at the end (tail) of the linked list -> Option A
Quick Check:
Enqueue = Add at tail [OK]
Hint: Enqueue always adds at the tail in linked list queue [OK]
Common Mistakes:
Confusing enqueue with dequeue operation
Adding new element at head instead of tail
Thinking enqueue removes elements
2. Which of the following is the correct syntax to create a new node and enqueue it at the tail in Python linked list?
easy
A. new_node = Node(data); new_node.next = tail; tail = new_node
B. new_node = Node(data); tail.next = new_node; tail = new_node
C. new_node = Node(data); new_node.next = head; head = new_node
D. new_node = Node(data); head.next = new_node; head = new_node
Solution
Step 1: Create new node with data
We create a new node object with the given data.
Step 2: Link new node at tail and update tail pointer
Set current tail's next to new node, then update tail to new node.
5. You want to implement a queue using a linked list that supports enqueue and dequeue in O(1) time. Which of the following changes is necessary to the enqueue method to handle the edge case when the queue is empty?
hard
A. Set both head and tail to the new node when queue is empty
B. Set only tail to the new node when queue is empty
C. Set new node's next to head when queue is empty
D. Set tail.next to head when queue is empty
Solution
Step 1: Understand queue empty state
When queue is empty, both head and tail pointers are None.
Step 2: Enqueue first element must update both pointers
To maintain O(1) enqueue and dequeue, head and tail must both point to the new node.
Step 3: This ensures correct linking and prevents errors
Without setting both, dequeue or next enqueue operations may fail or cause errors.
Final Answer:
Set both head and tail to the new node when queue is empty -> Option A
Quick Check:
Empty queue enqueue sets head and tail [OK]
Hint: Empty queue needs head and tail set to new node [OK]
Common Mistakes:
Setting only tail or only head on empty enqueue
Linking new node incorrectly to head or tail
Not handling empty queue edge case
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.