📖 Scenario: You are building a simple program to manage a list of tasks. Each task is stored in a doubly linked list node. You want to add new tasks to the end of the list.
🎯 Goal: Learn how to insert a new node at the end of a doubly linked list and print the list from start to end.
📋 What You'll Learn
Create a doubly linked list node structure with data, prev, and next pointers.
Initialize an empty doubly linked list with a head pointer.
Write a function to insert a new node at the end of the doubly linked list.
Write a function to print the doubly linked list from head to end.
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in many applications like browser history, music playlists, and undo-redo features where you need to move forward and backward easily.
💼 Career
Understanding linked lists is fundamental for software developers, especially for roles involving system programming, embedded systems, or any application requiring efficient dynamic data management.
Progress0 / 4 steps
1
Create the doubly linked list node and initialize head
Define a struct called Node with an int data, a Node* called prev, and a Node* called next. Then create a Node* variable called head and set it to NULL.
DSA C
Hint
Use typedef struct Node to define the node. Initialize head as NULL to show the list is empty.
2
Create a function to insert a new node at the end
Write a function called insertAtEnd that takes an int data parameter. Inside, create a new Node* called newNode using malloc. Set newNode->data to data, and both newNode->prev and newNode->next to NULL. If head is NULL, set head to newNode. Otherwise, traverse the list to find the last node, then set its next to newNode and newNode->prev to that last node.
DSA C
Hint
Remember to handle the empty list case by setting head to the new node. Otherwise, find the last node and link the new node after it.
3
Create a function to print the doubly linked list
Write a function called printList that starts from head and uses a while loop to print each node's data followed by -> . Stop when the current node is NULL. After the loop, print NULL.
DSA C
Hint
Use a temporary pointer to walk through the list from head to NULL. Print each node's data followed by an arrow.
4
Insert nodes and print the list
Call insertAtEnd three times to add nodes with data 10, 20, and 30. Then call printList() to display the list.
DSA C
Hint
Call insertAtEnd with 10, 20, and 30. Then call printList() to see the list.
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 list structure
A doubly linked list has nodes connected forward and backward, allowing traversal in both directions.
Step 2: Purpose of insertion at end
Inserting at the end means adding a new node after the last node, extending the list.
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 happens at the start
Mixing up singly and doubly linked list operations
2. Which of the following is the correct way to update the 'next' pointer of the last node when inserting a new node at the end in C?
easy
A. last->next = newNode;
B. newNode->next = last;
C. last->prev = newNode;
D. newNode->prev = NULL;
Solution
Step 1: Identify the last node's next pointer
The last node's 'next' pointer should point to the new node to link it at the end.
Step 2: Correct pointer assignment
Assigning 'last->next = newNode;' correctly links the new node after the last node.
Final Answer:
last->next = newNode; -> Option A
Quick Check:
Last node's next points to new node [OK]
Hint: Last node's next points to new node [OK]
Common Mistakes:
Assigning newNode->next to last (wrong direction)
Changing last->prev instead of last->next
Setting newNode->prev to NULL instead of last
3. What will be the output of the following code snippet after inserting a node with value 5 at the end of the doubly linked list 1 <-> 3 <-> 4?
Step 1: Check pointer assignments for doubly linked list
In doubly linked list, newNode->prev must point to the last node (temp) after insertion.
Step 2: Identify missing pointer update
The code sets temp->next = newNode but does not set newNode->prev = temp, breaking backward link.
Final Answer:
Missing setting newNode->prev pointer -> Option C
Quick Check:
newNode->prev must be set [OK]
Hint: Always set newNode->prev to last node [OK]
Common Mistakes:
Forgetting newNode->prev assignment
Assuming singly linked list logic applies
Not handling empty list case
5. Given a doubly linked list with nodes 10 <-> 20 <-> 30, write the sequence of pointer changes when inserting a new node with value 40 at the end. Which of the following correctly describes the final connections?
hard
A. newNode->prev = NULL; newNode->next = NULL; last->next = NULL
B. newNode->next = last; last->prev = newNode; newNode->prev = NULL
C. last->prev = newNode; newNode->next = last; newNode->prev = NULL
D. last->next = newNode; newNode->prev = last; newNode->next = NULL
Solution
Step 1: Identify last node and new node pointers
Last node is 30. New node 40 should be linked after 30 with next NULL.
Step 2: Correct pointer assignments
Set last->next = newNode to link forward, newNode->prev = last for backward link, and newNode->next = NULL as it is new end.