Bird
0
0
DSA Cprogramming~30 mins

Insert at End of Doubly Linked List in DSA C - Build from Scratch

Choose your learning style9 modes available
Insert at End of Doubly Linked List
📖 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.