Consider a queue implemented using a linked list. After performing the following operations, what is the printed state of the queue?
Enqueue(10)
Enqueue(20)
Enqueue(30)
Dequeue()
Enqueue(40)
Print the queue from front to rear.
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class Queue { private Node front, rear; public Queue() { front = rear = null; } public void Enqueue(int item) { Node temp = new Node(item); if (rear == null) { front = rear = temp; return; } rear.next = temp; rear = temp; } public int Dequeue() { if (front == null) return -1; int item = front.data; front = front.next; if (front == null) rear = null; return item; } public string PrintQueue() { Node temp = front; string result = ""; while (temp != null) { result += temp.data + " -> "; temp = temp.next; } return result + "null"; } } Queue q = new Queue(); q.Enqueue(10); q.Enqueue(20); q.Enqueue(30); q.Dequeue(); q.Enqueue(40); Console.WriteLine(q.PrintQueue());
Remember that dequeue removes the front element.
Initially, queue is empty.
Enqueue 10, queue: 10 -> null
Enqueue 20, queue: 10 -> 20 -> null
Enqueue 30, queue: 10 -> 20 -> 30 -> null
Dequeue removes 10, queue: 20 -> 30 -> null
Enqueue 40, queue: 20 -> 30 -> 40 -> null
In a queue implemented using a linked list, what is the expected behavior when Dequeue() is called but the queue is empty?
Check the Dequeue method's return value when front is null.
The Dequeue method checks if the front node is null. If yes, it returns -1 to indicate the queue is empty and no element can be dequeued.
What is the bug in the following enqueue method for a linked list queue?
public void Enqueue(int item) {
Node temp = new Node(item);
if (front == null) {
front = temp;
}
rear.next = temp;
rear = temp;
}Think about what happens when the queue is empty and rear is null.
If the queue is empty, rear is null. Trying to access rear.next causes a NullReferenceException. The code should return after setting front and rear to temp.
Given the following operations on a linked list queue, what is the printed queue?
Enqueue(5)
Enqueue(15)
Enqueue(25)
Dequeue()
Dequeue()
Enqueue(35)
Print the queue from front to rear.
class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } class Queue { private Node front, rear; public Queue() { front = rear = null; } public void Enqueue(int item) { Node temp = new Node(item); if (rear == null) { front = rear = temp; return; } rear.next = temp; rear = temp; } public int Dequeue() { if (front == null) return -1; int item = front.data; front = front.next; if (front == null) rear = null; return item; } public string PrintQueue() { Node temp = front; string result = ""; while (temp != null) { result += temp.data + " -> "; temp = temp.next; } return result + "null"; } } Queue q = new Queue(); q.Enqueue(5); q.Enqueue(15); q.Enqueue(25); q.Dequeue(); q.Dequeue(); q.Enqueue(35); Console.WriteLine(q.PrintQueue());
Track the front pointer after each dequeue.
Queue after enqueues: 5 -> 15 -> 25 -> null
After first dequeue: removes 5, queue: 15 -> 25 -> null
After second dequeue: removes 15, queue: 25 -> null
Enqueue 35: queue: 25 -> 35 -> null
But note the code sets rear to null if front is null after dequeue. Here front is 25, so rear is not null.
Output is 25 -> 35 -> null
Which of the following is the best reason to use a linked list instead of an array to implement a queue?
Think about resizing and memory allocation.
Arrays have fixed size or require resizing which is costly. Linked lists grow dynamically without resizing overhead, making them suitable for queues with unknown or changing size.
