📖 Scenario: You are building a simple program to manage a list of tasks. Each task is stored in a doubly linked list, where each node has a task name and links to the previous and next tasks.New tasks are always added at the beginning of the list, so the most recent task is at the front.
🎯 Goal: Create a doubly linked list and write code to insert new tasks at the beginning. Then print the list from start to end to see the order of tasks.
📋 What You'll Learn
Define a Node class with string data, Node prev, and Node next fields.
Create a DoublyLinkedList class with a Node head field.
Write a method InsertAtBeginning(string data) to add a new node at the start.
Write a method PrintList() to print all nodes from head to end.
Insert three tasks: "Task1", "Task2", "Task3" at the beginning in that order.
Print the list after all insertions.
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in applications like task schedulers, undo-redo features, and navigation history where you need to move forward and backward easily.
💼 Career
Understanding linked lists is fundamental for software developers working with data structures, memory management, and building efficient algorithms.
Progress0 / 4 steps
1
Create Node and DoublyLinkedList classes
Define a class called Node with a string data field, and two Node fields called prev and next. Then define a class called DoublyLinkedList with a Node head field initialized to null.
DSA C#
Hint
Think of Node as a box holding the task name and links to neighbors. DoublyLinkedList keeps track of the first box.
2
Add InsertAtBeginning method
Inside the DoublyLinkedList class, write a method called InsertAtBeginning that takes a string data parameter. Create a new Node with this data. If head is null, set head to the new node. Otherwise, set the new node's next to head, set head.prev to the new node, then update head to the new node.
DSA C#
Hint
Remember to update both next and prev links when inserting at the start.
3
Add PrintList method and insert tasks
Inside the DoublyLinkedList class, write a method called PrintList that starts from head and prints each node's data followed by " -> " until the end (null). Then insert three tasks by calling InsertAtBeginning with "Task1", "Task2", and "Task3" in that order.
DSA C#
Hint
Use a while loop to move through the list from head to the end.
4
Print the list after insertions
Add a line to call PrintList() on the list object after inserting the three tasks to display the current list.
DSA C#
Hint
Call list.PrintList() after all insertions to see the list.
Practice
(1/5)
1. What is the main purpose of inserting a node at the beginning of a doubly linked list?
easy
A. To add a new node as the first element of the list
B. To remove the last node from the list
C. To search for a node in the list
D. To reverse the entire list
Solution
Step 1: Understand the operation
Inserting at the beginning means adding a new node before the current first node.
Step 2: Identify the effect on the list
The new node becomes the head (first node), shifting others to the right.
Final Answer:
To add a new node as the first element of the list -> Option A
Quick Check:
Insert at beginning = Add first node [OK]
Hint: Insert at beginning means new node becomes the first [OK]
Common Mistakes:
Confusing insertion with deletion
Thinking insertion adds at the end
Mixing up search with insertion
2. Which of the following is the correct syntax to create a new node in C# for a doubly linked list?
easy
A. Node newNode = new Node(data);
B. newNode = Node(data);
C. Node newNode = Node(data);
D. Node newNode = new Node[];
Solution
Step 1: Recall C# object creation syntax
In C#, objects are created using the 'new' keyword followed by the constructor call.
Step 2: Match syntax with node creation
Correct syntax is 'Node newNode = new Node(data);' to create a new node with data.
Final Answer:
Node newNode = new Node(data); -> Option A
Quick Check:
C# new object = new ClassName() [OK]
Hint: Use 'new' keyword to create objects in C# [OK]
Common Mistakes:
Omitting 'new' keyword
Using array syntax for single node
Missing type declaration
3. Consider this code snippet inserting a node with value 10 at the beginning of a doubly linked list:
Node newNode = new Node(10);
newNode.next = head;
if (head != null) {
head.prev = newNode;
}
head = newNode;
What will be the printed list after insertion if the original list was: 20 <-> 30 <-> null?
medium
A. 30 <-> 20 <-> 10 <-> null
B. 20 <-> 30 <-> 10 <-> null
C. 10 <-> 30 <-> 20 <-> null
D. 10 <-> 20 <-> 30 <-> null
Solution
Step 1: Insert new node at beginning
New node with 10 points next to old head (20). Old head's prev points back to new node.
Step 2: Update head pointer
Head now points to new node (10), so list starts with 10.
Final Answer:
10 <-> 20 <-> 30 <-> null -> Option D
Quick Check:
New node at front = 10 first [OK]
Hint: New node points to old head; update old head's prev [OK]
Common Mistakes:
Not updating old head's prev pointer
Appending new node at end instead of beginning
Not updating head to new node
4. Identify the error in this code snippet for inserting at the beginning of a doubly linked list:
Node newNode = new Node(5);
newNode.next = head;
head.prev = newNode;
head = newNode;
medium
A. Incorrect assignment of newNode.next
B. Missing check if head is null before accessing head.prev
C. head should be assigned before setting newNode.next
D. newNode.prev should be set to head
Solution
Step 1: Analyze head.prev assignment
Code sets head.prev = newNode without checking if head is null, causing error if list is empty.
Step 2: Identify missing null check
Must check if head != null before setting head.prev to avoid null reference error.
Final Answer:
Missing check if head is null before accessing head.prev -> Option B
Quick Check:
Check head != null before head.prev [OK]
Hint: Always check head != null before accessing head.prev [OK]
Common Mistakes:
Ignoring null check on head
Setting pointers in wrong order
Confusing prev and next pointers
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25 <-> null. You want to insert a new node with value 1 at the beginning. After insertion, which of the following correctly represents the list and the pointers of the new node?