📖 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 a new customer to the end using the enqueue operation.
📋 What You'll Learn
Create a Node class with value and next properties
Create a Queue class with front and rear properties
Add an enqueue method to add a new node at the end
Print the queue values from front to rear after enqueue
💡 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 queues and linked lists is important for software development roles that involve data processing, task scheduling, and system design.
Progress0 / 4 steps
1
Create Node and Queue classes
Create a class called Node with a constructor that takes value and sets this.value and this.next to null. Then create a class called Queue with a constructor that sets this.front and this.rear to null.
DSA Javascript
Hint
Think of Node as a box holding a value and a pointer to the next box. The Queue keeps track of the first and last boxes.
2
Add enqueue method to Queue
Inside the Queue class, add a method called enqueue that takes a parameter value. This method should create a new Node with the given value.
DSA Javascript
Hint
Creating a new node is the first step to adding it to the queue.
3
Complete enqueue logic to add node at rear
In the enqueue method, check if this.rear is null. If yes, set both this.front and this.rear to newNode. Otherwise, set this.rear.next to newNode and then update this.rear to newNode.
DSA Javascript
Hint
If the queue is empty, the new node is both front and rear. Otherwise, link the new node at the end and update rear.
4
Print queue values from front to rear
Create a variable current and set it to this.front. Use a while loop to traverse the queue until current is null. Inside the loop, print current.value followed by ' -> '. After the loop, print 'null'. Then create a queue, enqueue values 10, 20, and 30, and print the queue.
DSA Javascript
Hint
Traverse from front to rear, adding each value to a string, then print it.
Practice
(1/5)
1. What does the enqueue operation do in a queue implemented using a linked list?
easy
A. Deletes the entire linked list
B. Removes an element from the front (head) of the linked list
C. Adds a new element at the end (tail) of the linked list
D. Adds a new element at the front (head) of the linked list
Solution
Step 1: Understand enqueue operation
Enqueue means adding an element to the queue.
Step 2: Identify position in linked list
In a linked list queue, new elements are added at the tail (end).
Final Answer:
Adds a new element at the end (tail) of the linked list -> Option C
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 the head instead of tail
Thinking enqueue removes elements
2. Which of the following is the correct way to create a new node for enqueue in JavaScript linked list?
easy
A. const node = { value: val, next: null };
B. const node = new Node(val); node.next = val;
C. const node = { val: value, next: undefined };
D. const node = Node(val, null);
Solution
Step 1: Understand node structure
A node should have a value and a pointer to next (null initially).
Step 2: Check syntax correctness
const node = { value: val, next: null }; correctly creates an object with value and next set to null.
What error will occur when enqueue is called on an empty queue?
medium
A. ReferenceError: this is not defined
B. SyntaxError: Unexpected token
C. No error, works fine
D. TypeError: Cannot read property 'next' of null
Solution
Step 1: Analyze empty queue condition
When queue is empty, this.tail is null or undefined.
Step 2: Identify error line
Code tries this.tail.next = node; but this.tail is null, causing TypeError.
Final Answer:
TypeError: Cannot read property 'next' of null -> Option D
Quick Check:
Accessing next on null causes TypeError [OK]
Hint: Check if tail exists before accessing next [OK]
Common Mistakes:
Ignoring null check for tail
Assuming no error on empty queue
Confusing this.head and this.tail usage
5. You want to implement enqueue in a linked list queue that keeps track of both head and tail. Which of these code snippets correctly enqueues a new value val?