📖 Scenario: Imagine you have a row of boxes where you keep your favorite toys. Sometimes you want to add a new toy in the middle or remove one without moving all the others. Using a linked list is like having a chain of boxes where each box knows where the next one is, so you can easily add or remove toys without shifting everything.
🎯 Goal: You will create a simple linked list of toy names to understand why linked lists are useful. You will see how adding toys one by one works and how the list looks after adding them.
📋 What You'll Learn
Create a linked list node structure with a toy name and a pointer to the next node
Create the first node with the toy name "Car"
Add two more nodes with toy names "Doll" and "Ball" linked together
Print the linked list to show the order of toys
💡 Why This Matters
🌍 Real World
Linked lists are used in real life when you want to keep items in order but also want to add or remove items easily, like a playlist of songs or a chain of tasks.
💼 Career
Understanding linked lists is important for software developers because many programs use them to manage data efficiently, especially when the size of data changes often.
Progress0 / 4 steps
1
Create the first linked list node
Define a struct called Node with a char array toy[20] and a pointer next to Node. Then create a variable head of type Node* and allocate memory for it. Set head->toy to "Car" and head->next to NULL.
DSA C
Hint
Use typedef struct Node to define the node. Use malloc to create the first node. Use strcpy to copy the toy name.
2
Add two more nodes to the linked list
Create two new nodes called second and third using malloc. Set their toy values to "Doll" and "Ball" respectively. Link head->next to second and second->next to third. Set third->next to NULL.
DSA C
Hint
Use malloc to create new nodes. Use strcpy to set toy names. Link nodes by setting next pointers.
3
Traverse the linked list to print toy names
Create a pointer current and set it to head. Use a while loop to go through the list until current is NULL. Inside the loop, print the toy name in current->toy followed by " -> ". Move current to current->next.
DSA C
Hint
Use a while loop to move through the list. Print each toy name followed by an arrow. End with printing "NULL".
4
Print the final linked list output
Run the program to print the linked list showing the toys in order: "Car -> Doll -> Ball -> NULL".
DSA C
Hint
Run the program and check the printed output matches the linked list order.
Practice
(1/5)
1. Why do we use a linked list instead of an array in some cases?
easy
A. Because linked lists are faster to access elements by index
B. Because linked lists allow easy insertion and deletion without shifting elements
C. Because linked lists store elements in contiguous memory locations
D. Because linked lists use less memory than arrays always
Solution
Step 1: Understand array limitations
Arrays store elements in continuous memory, so inserting or deleting in the middle requires shifting elements, which is slow.
Step 2: Understand linked list advantages
Linked lists store elements in nodes connected by pointers, so insertion and deletion can be done by changing pointers without shifting.
Final Answer:
Because linked lists allow easy insertion and deletion without shifting elements -> Option B
Quick Check:
Linked list insertion/deletion is easier than array shifting [OK]
Hint: Linked lists avoid shifting elements on insert/delete [OK]
Common Mistakes:
Thinking linked lists use less memory always
Believing linked lists store elements contiguously
Assuming linked lists have faster index access
2. Which of the following is the correct way to declare a node structure for a singly linked list in C?
easy
A. struct Node { int data; Node* next; };
B. struct Node { int data; int next; };
C. struct Node { int data; struct Node* next; };
D. struct Node { int data; struct Node next; };
Solution
Step 1: Check pointer usage in struct
In C, to refer to the same struct type inside itself, we use 'struct Node* next;' as a pointer to next node.
Step 2: Identify correct syntax
struct Node { int data; struct Node* next; }; correctly declares 'struct Node* next;' while others either use wrong types or missing 'struct' keyword.
Final Answer:
struct Node { int data; struct Node* next; }; -> Option C
Quick Check:
Use 'struct Node* next;' for linked list node pointer [OK]
Hint: Use 'struct Node* next;' to link nodes in C [OK]
Common Mistakes:
Omitting 'struct' keyword before Node pointer
Using 'int' instead of pointer for next
Declaring next as struct instead of pointer
3. What will be the output of the following C code snippet that creates a linked list with two nodes and prints their data?