📖 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 holds the task name and links to the previous and next tasks.We want to add new tasks to the end of this list.
🎯 Goal: Create a doubly linked list and write code to insert new tasks at the end of the list.Finally, print the list from start to end to see all tasks in order.
📋 What You'll Learn
Define a Node struct with string task, Node* prev, and Node* next.
Create a DoublyLinkedList class with a Node* head pointer.
Write a method insertAtEnd(string task) to add a new node at the end.
Write a method printList() to print all tasks from head to end.
Use exact variable and method names as specified.
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in many 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 development roles, especially when working with low-level data structures, memory management, or building efficient algorithms.
Progress0 / 4 steps
1
Create Node struct and DoublyLinkedList class with head pointer
Define a struct Node with string task, Node* prev, and Node* next. Then create a DoublyLinkedList class with a public Node* head pointer initialized to nullptr.
DSA C++
Hint
Start by defining the Node struct with the three members. Then create the DoublyLinkedList class with a head pointer set to nullptr.
2
Add insertAtEnd method to DoublyLinkedList
Inside the DoublyLinkedList class, write a public method void insertAtEnd(string task) that creates a new Node with the given task and inserts it at the end of the list. If the list is empty, set head to the new node. Otherwise, traverse to the last node and link the new node after it.
DSA C++
Hint
Create a new node with the given task. If head is nullptr, assign the new node to head. Otherwise, move to the last node and link the new node after it.
3
Add printList method to display tasks from head to end
Inside the DoublyLinkedList class, write a public method void printList() that starts from head and prints each task followed by " -> " until the end of the list. After the last task, print "null".
DSA C++
Hint
Start from head and loop through each node until nullptr. Print the task followed by " -> ". After the loop, print "null".
4
Insert tasks and print the list
In the main() function, create a DoublyLinkedList object called tasks. Use insertAtEnd to add these tasks in order: "Laundry", "Groceries", "Homework". Then call printList() on tasks to display the list.
DSA C++
Hint
Create the tasks list, insert the three tasks in order, then print the list to see the tasks connected with arrows.
Practice
(1/5)
1. What is the main purpose of inserting a node at the end of a doubly linked list?
easy
A. To add a new element after the last node
B. To remove the first node
C. To reverse the list
D. To find the middle node
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes connected forward and backward, allowing traversal in both directions.
Step 2: Purpose of insertion at the end
Inserting at the end means adding a new node after the current last node, updating pointers accordingly.
Final Answer:
To add a new element after the last node -> Option A
Quick Check:
Insert at end = Add after last node [OK]
Hint: Insert at end means add after last node [OK]
Common Mistakes:
Confusing insertion at end with deletion
Thinking insertion at end reverses the list
Assuming insertion happens at the start
2. Which of the following is the correct way to update the tail pointer when inserting a new node at the end of a doubly linked list in C++?
easy
A. tail = NULL;
B. tail = tail->prev;
C. tail = head;
D. tail = newNode;
Solution
Step 1: Identify the new tail after insertion
When a new node is added at the end, it becomes the new tail of the list.
Step 2: Update the tail pointer
Assign the tail pointer to point to the new node to reflect the updated end of the list.
Final Answer:
tail = newNode; -> Option D
Quick Check:
New tail points to newNode [OK]
Hint: Tail always points to the last inserted node [OK]
Common Mistakes:
Setting tail to previous node instead of new node
Resetting tail to head incorrectly
Assigning tail to NULL after insertion
3. What will be the output of the following code snippet after inserting 30 at the end of the doubly linked list?
struct Node {
int data;
Node* prev;
Node* next;
};
Node* head = new Node{10, NULL, NULL};
Node* tail = head;
Node* newNode = new Node{30, tail, NULL};
tail->next = newNode;
tail = newNode;
// Print list from head
Node* temp = head;
while(temp != NULL) {
std::cout << temp->data << " ";
temp = temp->next;
}
medium
A. 10 30
B. 30 10
C. 10
D. 30
Solution
Step 1: Initial list and insertion
Initially, list has one node with data 10. New node with 30 is added at end, linked properly.
Step 2: Traversing and printing
Starting from head (10), print data then move to next node (30), print data, then NULL ends loop.
Final Answer:
10 30 -> Option A
Quick Check:
List after insertion = 10 30 [OK]
Hint: Traverse from head to see all nodes in order [OK]
Common Mistakes:
Printing nodes in reverse order
Not updating tail->next pointer
Forgetting to update tail pointer
4. Identify the error in the following code snippet that inserts a node at the end of a doubly linked list:
void insertEnd(Node*& head, Node*& tail, int val) {
Node* newNode = new Node{val, NULL, NULL};
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
head = newNode; // Error here
}
}
medium
A. head should be updated to newNode
B. newNode->prev should be NULL
C. tail should be updated to newNode
D. tail->next should be NULL
Solution
Step 1: Analyze pointer updates in else block
tail->next points to newNode and newNode->prev points to tail correctly.
Step 2: Identify incorrect pointer assignment
head is wrongly updated to newNode; it should remain unchanged. Instead, tail should be updated to newNode.
Final Answer:
tail should be updated to newNode -> Option C
Quick Check:
Tail points to last node after insertion [OK]
Hint: Update tail, not head, when inserting at end [OK]
Common Mistakes:
Updating head instead of tail
Not linking newNode's prev pointer
Forgetting to set tail->next
5. Given a doubly linked list with nodes 5 <-> 10 <-> 15, write the sequence of node values after inserting 20 at the end and then 2 at the end. What is the final list?
hard
A. 2 <-> 5 <-> 10 <-> 15 <-> 20
B. 5 <-> 10 <-> 15 <-> 20 <-> 2
C. 5 <-> 10 <-> 15 <-> 2 <-> 20
D. 20 <-> 2 <-> 5 <-> 10 <-> 15
Solution
Step 1: Insert 20 at the end
Starting list: 5 <-> 10 <-> 15. After inserting 20 at end: 5 <-> 10 <-> 15 <-> 20.
Step 2: Insert 2 at the end
Now add 2 at end: 5 <-> 10 <-> 15 <-> 20 <-> 2.
Final Answer:
5 <-> 10 <-> 15 <-> 20 <-> 2 -> Option B
Quick Check:
Insertions add nodes at list end in order [OK]
Hint: Insertions add nodes at the end, order preserved [OK]
Common Mistakes:
Inserting new nodes at the start instead of end
Mixing order of inserted nodes
Confusing next and prev pointers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.