What if you could add or remove items anywhere in a list instantly without rewriting everything?
Why Create and Initialize Doubly Linked List in DSA C#?
Imagine you have a list of tasks written on paper, and you want to add or remove tasks quickly from anywhere in the list. Doing this by rewriting the whole list every time is tiring and slow.
Manually rewriting or shifting tasks every time you add or remove one is slow and causes mistakes. You might lose track of tasks or mix up their order.
A doubly linked list lets you add or remove tasks anywhere easily by just changing a few links between tasks. You don't have to rewrite the whole list, making it fast and safe.
string[] tasks = new string[5]; tasks[0] = "Task1"; tasks[1] = "Task2"; // To add a task in middle, shift all later tasks manually
DoublyLinkedList list = new DoublyLinkedList(); list.AddLast("Task1"); list.AddLast("Task2"); // Add or remove tasks by changing links, no shifting needed
You can manage lists that change often, like playlists or undo histories, quickly and without errors.
Think of a music playlist where you can move songs up or down or remove them instantly without rewriting the whole list.
Manual list updates are slow and error-prone.
Doubly linked lists use links to add or remove items fast.
This makes managing changing lists easy and safe.
Practice
Solution
Step 1: Understand doubly linked list structure
A doubly linked list has nodes with pointers to both next and previous nodes.Step 2: Identify traversal capability
This structure allows moving forward and backward through the list easily.Final Answer:
To allow traversal in both forward and backward directions -> Option AQuick Check:
Doubly linked list = bidirectional traversal [OK]
- Confusing doubly linked list with arrays
- Thinking it sorts data automatically
- Assuming it only allows forward traversal
Solution
Step 1: Check node fields for doubly linked list
Node must have data, next pointer, and previous pointer of type Node.Step 2: Verify correct syntax and access modifiers
public class Node { public int data; public Node next; public Node prev; } declares public int data, and public Node next and prev correctly.Final Answer:
public class Node { public int data; public Node next; public Node prev; } -> Option CQuick Check:
Node class with data, next, prev = public class Node { public int data; public Node next; public Node prev; } [OK]
- Using int for next and prev instead of Node
- Missing prev pointer for doubly linked list
- Incorrect access modifiers
public class Node {
public int data;
public Node next;
public Node prev;
public Node(int d) { data = d; next = null; prev = null; }
}
public class DoublyLinkedList {
public Node head;
public void AddLast(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null) temp = temp.next;
temp.next = newNode;
newNode.prev = temp;
}
public void PrintForward() {
Node temp = head;
while (temp != null) {
Console.Write(temp.data + " -> ");
temp = temp.next;
}
Console.WriteLine("null");
}
}
class Program {
static void Main() {
DoublyLinkedList list = new DoublyLinkedList();
list.AddLast(10);
list.AddLast(20);
list.AddLast(30);
list.PrintForward();
}
}Solution
Step 1: Understand AddLast method behavior
AddLast adds nodes at the end, so nodes are 10, then 20, then 30 in order.Step 2: Trace PrintForward output
PrintForward prints from head to last node: 10 -> 20 -> 30 -> null.Final Answer:
10 -> 20 -> 30 -> null -> Option DQuick Check:
Forward print = 10 -> 20 -> 30 -> null [OK]
- Confusing forward and backward traversal
- Missing last node in output
- Printing in reverse order
public void AddLast(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
// Missing line here
}Solution
Step 1: Review AddLast logic
After finding last node, newNode is linked as next of temp.Step 2: Check doubly linked list pointer updates
For doubly linked list, newNode.prev must point back to temp to maintain backward link.Final Answer:
Missing setting newNode.prev = temp -> Option AQuick Check:
Set newNode.prev to temp for backward link [OK]
- Forgetting to set newNode.prev
- Reassigning head incorrectly
- Skipping loop to find last node
int[] arr = {1, 2, 3, 4};
DoublyLinkedList list = new DoublyLinkedList();
// Step 1: Add all elements from arr to list
// Step 2: Print list backward
// Which code snippet correctly does this?Solution
Step 1: Add elements in order using AddLast
Use foreach (int val in arr) list.AddLast(val); to build the list as 1 -> 2 -> 3 -> 4, preserving array order.Step 2: Traverse to last node and print backward
Node temp = list.head; while (temp.next != null) temp = temp.next; then while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.prev; } Console.WriteLine("null"); to print 4 -> 3 -> 2 -> 1 -> null.Final Answer:
foreach (int val in arr) list.AddLast(val); Node temp = list.head; while (temp.next != null) temp = temp.next; while (temp != null) { Console.Write(temp.data + " -> "); temp = temp.prev; } Console.WriteLine("null"); -> Option BQuick Check:
Build forward list + backward traversal from tail [OK]
- Adding elements in reverse order
- Printing forward instead of backward
- Using AddFirst which reverses order
Ready to prove it?
Pass the test and this pattern moves unknown
