Bird
Raised Fist0
DSA Typescriptprogramming~30 mins

Dequeue Using Linked List in DSA Typescript - Build from Scratch

Choose your learning style10 modes available
Dequeue Using Linked List
📖 Scenario: You are building a simple system to manage a line of people waiting for a service. Sometimes people join the line at the front or the back, and sometimes people leave from the front or the back. This system is called a dequeue (double-ended queue).We will use a linked list to keep track of the people in the line.
🎯 Goal: Create a dequeue using a linked list in TypeScript. You will add people to the front and back, remove people from the front and back, and finally show the current line.
📋 What You'll Learn
Create a linked list node class with a value and next/prev pointers
Create a Dequeue class with methods to add to front and back
Create methods to remove from front and back
Print the current state of the dequeue as a list of values
💡 Why This Matters
🌍 Real World
Dequeue structures are used in real-world systems like task schedulers, undo-redo features, and managing lines where people or items can join or leave from both ends.
💼 Career
Understanding how to implement and use dequeue with linked lists is important for software engineers working on performance-critical applications, real-time systems, and complex data management.
Progress0 / 4 steps
1
Create Node and Dequeue Classes
Create a class called Node with a constructor that takes a value of type string. It should have public properties value, next, and prev (both can be null initially). Then create a class called Dequeue with public properties head and tail initialized to null.
DSA Typescript
Hint

Define Node with value, next, and prev. Then define Dequeue with head and tail set to null.

2
Add Methods to Insert at Front and Back
Inside the Dequeue class, add two methods: addFront(value: string): void and addBack(value: string): void. These methods should create a new Node and add it to the front or back of the linked list, updating head and tail accordingly.
DSA Typescript
Hint

Use addFront to add a new node before the current head. Use addBack to add a new node after the current tail.

3
Add Methods to Remove from Front and Back
Inside the Dequeue class, add two methods: removeFront(): string | null and removeBack(): string | null. These methods should remove the node from the front or back of the linked list, update head and tail, and return the removed node's value. Return null if the dequeue is empty.
DSA Typescript
Hint

To remove from front, move head to next node and update prev. To remove from back, move tail to previous node and update next. Return removed values or null if empty.

4
Print the Current Dequeue
Add a method print(): void inside the Dequeue class that prints the values of all nodes from head to tail separated by -> and ending with null. Then create a Dequeue instance, add "Alice" to front, "Bob" to back, "Carol" to front, remove one from back, and finally print the dequeue.
DSA Typescript
Hint

Traverse from head to tail, join values with -> , and end with null. Then test by adding and removing nodes as instructed.

Practice

(1/5)
1. What is the main advantage of using a linked list to implement a dequeue compared to an array?
easy
A. It allows insertion and deletion from both ends in constant time.
B. It uses less memory than arrays.
C. It automatically sorts the elements.
D. It prevents duplicate elements.

Solution

  1. Step 1: Understand dequeue operations

    A dequeue supports insertion and deletion from both front and rear ends.
  2. Step 2: Compare linked list and array

    Linked lists allow adding/removing nodes at both ends in constant time, unlike arrays which may require shifting elements.
  3. Final Answer:

    It allows insertion and deletion from both ends in constant time. -> Option A
  4. Quick Check:

    Linked list dequeue = O(1) insert/delete both ends [OK]
Hint: Linked list supports fast add/remove at both ends [OK]
Common Mistakes:
  • Thinking arrays allow constant time insert/delete at front
  • Confusing memory usage advantage
  • Assuming automatic sorting
2. Which of the following is the correct TypeScript method signature to insert an element at the front of a dequeue implemented using a linked list?
easy
A. insertFront(value: number): void
B. addRear(value: number): number
C. pushFront(): void
D. enqueueFront(value: string): string

Solution

  1. Step 1: Identify method purpose and parameter

    Inserting at front requires a method that takes a value (number) and returns nothing (void).
  2. Step 2: Match signature with correct naming and types

    insertFront(value: number): void matches: method name 'insertFront', parameter 'value: number', return type 'void'. Others have wrong names, return types, or parameter types.
  3. Final Answer:

    insertFront(value: number): void -> Option A
  4. Quick Check:

    Insert front method = insertFront(value: number): void [OK]
Hint: Method to add front takes value and returns void [OK]
Common Mistakes:
  • Using wrong method names like pushFront
  • Wrong return types like number or string
  • Incorrect parameter types
3. Consider the following TypeScript code snippet for a dequeue using linked list:
const dq = new Dequeue();
dq.insertRear(10);
dq.insertFront(20);
dq.insertRear(30);
dq.deleteFront();
dq.insertFront(40);
dq.deleteRear();
dq.display();
What will be the printed output?
medium
A. 10 -> 20 -> 30 -> null
B. 40 -> 10 -> null
C. 20 -> 10 -> 30 -> null
D. 40 -> 20 -> null

Solution

  1. Step 1: Trace each operation on dequeue

    Start empty. insertRear(10): 10 insertFront(20): 20 -> 10 insertRear(30): 20 -> 10 -> 30 deleteFront(): removes 20, now 10 -> 30 insertFront(40): 40 -> 10 -> 30 deleteRear(): removes 30, now 40 -> 10
  2. Step 2: Display final dequeue state

    Prints elements from front to rear: 40 -> 10 -> null
  3. Final Answer:

    40 -> 10 -> null -> Option B
  4. Quick Check:

    After operations, dequeue = 40 -> 10 -> null [OK]
Hint: Trace each insert/delete step carefully [OK]
Common Mistakes:
  • Forgetting deleteFront removes front element
  • Mixing order of insertFront and insertRear
  • Not updating links after deleteRear
4. The following TypeScript method for deleting the rear node in a dequeue implemented using a singly linked list has a bug. What is the bug?
deleteRear(): number | null {
  if (!this.rear) return null;
  let temp = this.front;
  while (temp.next !== this.rear) {
    temp = temp.next;
  }
  const value = this.rear.value;
  this.rear = temp;
  this.rear.next = null;
  return value;
}
medium
A. It causes an infinite loop when the list is empty.
B. It incorrectly updates the front pointer.
C. It returns the wrong value after deletion.
D. It does not handle the case when there is only one node.

Solution

  1. Step 1: Analyze loop and pointers

    The loop finds the node before rear by checking temp.next !== rear. If only one node exists, temp.next is null, so loop condition fails immediately.
  2. Step 2: Check single node case

    If only one node, front and rear point to same node. The code does not update front or rear to null, causing incorrect state after deletion.
  3. Final Answer:

    It does not handle the case when there is only one node. -> Option D
  4. Quick Check:

    Single node deleteRear needs special handling [OK]
Hint: Check single-node edge case in deleteRear [OK]
Common Mistakes:
  • Ignoring single-node list case
  • Assuming front pointer updates automatically
  • Overlooking null pointer exceptions
5. You want to implement a dequeue using a doubly linked list in TypeScript. Which of the following changes will improve the efficiency of deleting the rear node compared to a singly linked list implementation?
hard
A. Traverse from front to find the node before rear each time.
B. Use an array instead of linked list for rear deletion.
C. Maintain a rear pointer and use the previous pointer to delete rear in O(1) time.
D. Delete rear by shifting all elements forward.

Solution

  1. Step 1: Understand doubly linked list structure

    Each node has pointers to next and previous nodes, allowing backward traversal.
  2. Step 2: Use rear pointer and previous link

    With rear pointer and previous link, deleting rear node is direct and takes constant time without traversal.
  3. Final Answer:

    Maintain a rear pointer and use the previous pointer to delete rear in O(1) time. -> Option C
  4. Quick Check:

    Doubly linked list rear delete = O(1) using prev pointer [OK]
Hint: Use prev pointer in doubly linked list for fast rear delete [OK]
Common Mistakes:
  • Traversing from front wastes time
  • Using array shifts is inefficient
  • Ignoring doubly linked list advantages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard