📖 Scenario: You are building a simple program to manage a list of tasks. Each task has a number and you want to store them in a doubly linked list so you can move forward and backward easily.
🎯 Goal: Create a doubly linked list with three nodes containing the values 10, 20, and 30. Initialize the list so that each node points correctly to the previous and next nodes.
📋 What You'll Learn
Define a struct called Node with an integer data and two pointers: prev and next
Create three nodes dynamically with values 10, 20, and 30
Link the nodes so that prev and next pointers are set correctly
Create a pointer called head that points to the first node
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in real-world applications like browser history navigation, music playlists, and undo-redo features where moving forward and backward is needed.
💼 Career
Understanding doubly linked lists is important for software developers working on systems that require efficient bidirectional data traversal and dynamic data management.
Progress0 / 4 steps
1
Define the Node structure and create three nodes
Define a struct called Node with an integer data and two pointers prev and next. Then create three nodes called node1, node2, and node3 using malloc. Assign the values 10, 20, and 30 to node1->data, node2->data, and node3->data respectively.
DSA C
Hint
Start by defining the Node struct with data, prev, and next. Then use malloc to create three nodes and assign their data values.
2
Create a head pointer and initialize it
Create a pointer called head of type Node* and set it to point to node1.
DSA C
Hint
Declare head as a pointer to Node and assign it to node1.
3
Link the nodes with prev and next pointers
Set node1->prev to NULL, node1->next to node2. Set node2->prev to node1, node2->next to node3. Set node3->prev to node2, and node3->next to NULL.
DSA C
Hint
Remember the first node's prev is NULL and the last node's next is NULL. Link the middle node's pointers to the nodes before and after it.
4
Print the doubly linked list forward
Use a Node* pointer called current to traverse the list from head. Print each node's data followed by ->. After the last node, print NULL.
DSA C
Hint
Use a while loop to move current from head to the end, printing each data value followed by ->. After the loop, print NULL.
Practice
(1/5)
1. What is the main purpose of a doubly linked list in data structures?
easy
A. To allow traversal in both forward and backward directions
B. To store data in a fixed-size array
C. To sort data automatically
D. To store data only in one direction
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 A
Quick Check:
Doubly linked list = bidirectional traversal [OK]
Hint: Remember: doubly means two directions [OK]
Common Mistakes:
Confusing doubly linked list with singly linked list
Thinking it sorts data automatically
Assuming it uses arrays internally
2. Which of the following is the correct way to declare a node structure for a doubly linked list in C?
easy
A. struct Node { int data; struct Node* next; };
B. struct Node { int data; struct Node* prev; struct Node* next; };
C. struct Node { int data; int next; int prev; };
D. struct Node { int data; struct Node next; struct Node prev; };
Solution
Step 1: Check node structure for doubly linked list
Each node must have data, a pointer to next node, and a pointer to previous node.
Step 2: Identify correct syntax
struct Node { int data; struct Node* prev; struct Node* next; }; correctly declares pointers to next and prev nodes using struct Node*.
Final Answer:
struct Node { int data; struct Node* prev; struct Node* next; }; -> Option B
Quick Check:
Node has data + prev pointer + next pointer [OK]
Hint: Pointers to prev and next must be struct Node* [OK]
Common Mistakes:
Using int instead of pointer for next/prev
Missing prev pointer in node
Declaring next/prev as struct Node instead of pointer
3. What will be the output of the following C code snippet that creates a doubly linked list with two nodes and prints their data?
Memory is allocated for node, initialized, then freed with free(node).
Step 2: Identify invalid access
After freeing, code tries to print node->data, which accesses freed memory causing undefined behavior.
Final Answer:
Accessing freed memory after free(node) -> Option A
Quick Check:
Using pointer after free = error [OK]
Hint: Never use pointer after free() [OK]
Common Mistakes:
Ignoring use-after-free errors
Forgetting to include stdlib.h (but not main error here)
Assuming free deletes pointer variable
5. You want to create a doubly linked list with nodes containing values 1, 2, and 3 in that order. Which code snippet correctly initializes this list with proper prev and next pointers?