📖 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 value and next properties
Create a Queue class with head and tail properties
Implement an enqueue method to add nodes at the end
Print the queue values from head to tail after enqueue operations
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting for service or tasks waiting to be processed.
💼 Career
Understanding how to implement queues with linked lists helps in software development roles that require efficient data handling and memory management.
Progress0 / 4 steps
1
Create Node and Queue classes
Create a class called Node with a constructor that takes a value of type number and sets this.value and this.next to null. Then create a class called Queue with head and tail properties both initialized to null.
DSA Typescript
Hint
Think of Node as one person in the line and Queue as the whole line.
2
Add enqueue method to Queue
Inside the Queue class, add a method called enqueue that takes a value of type number. Create a new Node with this value. If tail is not null, set tail.next to the new node. Then update tail to the new node. If head is null, set head to the new node.
DSA Typescript
Hint
Remember to update both head and tail when the queue is empty.
3
Enqueue values 10, 20, and 30
Create a new Queue instance called queue. Use the enqueue method to add the values 10, 20, and 30 to the queue.
DSA Typescript
Hint
Use the enqueue method three times with the exact values.
4
Print the queue values from head to tail
Write a printQueue function that takes a Queue instance and prints the values from head to tail separated by -> and ending with null. Then call printQueue(queue) to display the queue contents.
DSA Typescript
Hint
Use a loop to visit each node starting from head and build a string to print.
Practice
(1/5)
1. What does the enqueue operation do in a linked list implementation of a queue?
easy
A. Removes the first element from the linked list
B. Adds a new element at the end (tail) of 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
Enqueue means adding an element to the queue.
Step 2: Identify linked list position for enqueue
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 B
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
Mixing linked list positions
2. Which of the following is the correct TypeScript syntax to create a new node for enqueue in a linked list?
easy
A. newNode = new Node();
B. let newNode = Node(value);
C. const newNode = new Node(value);
D. const newNode = Node.new(value);
Solution
Step 1: Recall TypeScript class instantiation
To create an object, use new ClassName(params).
Step 2: Check options for correct syntax
Only const newNode = new Node(value); correctly creates a new Node instance.
Final Answer:
const newNode = new Node(value); -> Option C
Quick Check:
Use 'new' keyword to instantiate class [OK]
Hint: Use 'new' keyword to create class instances in TypeScript [OK]
Common Mistakes:
Omitting 'new' keyword when creating objects
Using function call syntax without 'new'
Incorrect property access like Node.new
Not declaring variable with let or const
3. What will be the state of the queue after executing the following enqueue operations on an empty linked list queue?
enqueue(10); enqueue(20); enqueue(30);
medium
A. 10 -> 20 -> 30 -> null
B. 30 -> 20 -> 10 -> null
C. null -> 10 -> 20 -> 30
D. 20 -> 10 -> 30 -> null
Solution
Step 1: Understand enqueue order
Enqueue adds elements at the tail, so order is preserved as inserted.
Step 2: Trace each enqueue
After enqueue(10): 10 -> null After enqueue(20): 10 -> 20 -> null After enqueue(30): 10 -> 20 -> 30 -> null
Final Answer:
10 -> 20 -> 30 -> null -> Option A
Quick Check:
Enqueue order = insertion order [OK]
Hint: Enqueue adds at tail, so order stays same as insertion [OK]
Common Mistakes:
Reversing order of elements after enqueue
Confusing enqueue with push at head
Misreading linked list direction
Ignoring null at the end
4. Consider this enqueue function snippet for a linked list queue:
A. It does not update the previous tail's next pointer to newNode
B. It incorrectly sets head to null when queue is empty
C. It creates newNode without value
D. It sets tail to null after enqueue
Solution
Step 1: Analyze enqueue logic
When queue is empty, head and tail should point to newNode.
Step 2: Check linking of nodes
Code sets tail = newNode but does not link old tail's next to newNode, breaking chain.
Final Answer:
It does not update the previous tail's next pointer to newNode -> Option A
Quick Check:
Link old tail's next to newNode [OK]
Hint: Always link old tail's next to new node when enqueueing [OK]
Common Mistakes:
Forgetting to update old tail's next pointer
Not handling empty queue case properly
Setting tail to null accidentally
Creating node without value parameter
5. You have a linked list queue with elements: 5 -> 15 -> 25 -> null. After enqueue(35), what will be the correct linked list representation and tail node value?
hard
A. 5 -> 15 -> 25 -> 35 -> null, tail value = 25
B. 35 -> 5 -> 15 -> 25 -> null, tail value = 25
C. 5 -> 15 -> 25 -> null, tail value = 25
D. 5 -> 15 -> 25 -> 35 -> null, tail value = 35
Solution
Step 1: Understand enqueue effect on queue
Enqueue adds new element at tail, so 35 goes after 25.
Step 2: Update tail pointer
Tail must point to the new last node, which holds 35.
Final Answer:
5 -> 15 -> 25 -> 35 -> null, tail value = 35 -> Option D
Quick Check:
Enqueue adds at tail, tail updates to new node [OK]
Hint: Tail always points to last enqueued node [OK]
Common Mistakes:
Adding new node at head instead of tail
Not updating tail pointer after enqueue
Confusing tail value with head value
Leaving tail pointing to old last node
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.