📖 Scenario: Imagine you have a chain of boxes, each holding a number and a link to the next box. This is like a linked list. You want to look inside each box and write down the numbers in order.
🎯 Goal: You will create a simple linked list with three nodes, then write code to go through each node one by one and print the numbers stored inside.
📋 What You'll Learn
Create a struct called Node with an integer data and a pointer next to the next node
Create three nodes with data values 10, 20, and 30 linked together
Create a pointer called head that points to the first node
Write a loop to traverse the linked list starting from head
Print the data of each node followed by ->, ending with NULL
💡 Why This Matters
🌍 Real World
Linked lists are used in many programs to store items in order when the number of items can change. Traversing and printing helps us see the list contents.
💼 Career
Understanding linked lists and how to traverse them is a key skill for software developers, especially when working with low-level data structures or preparing for coding interviews.
Progress0 / 4 steps
1
Create the linked list nodes
Create a struct called Node with an integer data and a pointer next to Node. Then create three variables node1, node2, and node3 of type Node. Set their data values to 10, 20, and 30 respectively.
DSA C
Hint
Start by defining the Node struct with two members: data and next. Then create three variables of this struct and assign the data values.
2
Link the nodes and create the head pointer
Set node1.next to point to &node2, node2.next to point to &node3, and node3.next to NULL. Then create a pointer head of type struct Node* and set it to point to &node1.
DSA C
Hint
Remember to link each node's next to the address of the following node. The last node's next should be NULL. Then set head to the first node's address.
3
Traverse the linked list
Create a pointer current of type struct Node* and set it to head. Use a while loop that runs while current is not NULL. Inside the loop, print the data of current followed by ->. Then update current to current->next.
DSA C
Hint
Use a pointer to move through the list. Print each node's data followed by an arrow. Stop when you reach NULL.
4
Print NULL at the end
After the while loop, print NULL followed by a newline to show the end of the linked list.
DSA C
Hint
After the loop finishes, print NULL and a newline to show the list ends.
Practice
(1/5)
1. What is the main purpose of traversing a linked list in C?
easy
A. To delete the entire linked list
B. To visit each node and access its data
C. To create a new linked list
D. To reverse the linked list
Solution
Step 1: Understand traversal meaning
Traversal means visiting each node one by one in the linked list.
Step 2: Identify traversal purpose
The main goal is to access or print the data stored in each node.
Final Answer:
To visit each node and access its data -> Option B
Quick Check:
Traversal = Visit nodes [OK]
Hint: Traversal means visiting nodes to read data [OK]
Common Mistakes:
Confusing traversal with deletion
Thinking traversal creates new nodes
Assuming traversal reverses the list
2. Which of the following is the correct syntax to start traversing a linked list named head in C?
easy
A. Node* temp = &head;
B. struct Node temp = head;
C. struct Node* temp = head;
D. Node temp = head;
Solution
Step 1: Understand pointer usage
In C, linked list nodes are accessed via pointers, so temp must be a pointer to Node.
Step 2: Check syntax correctness
struct Node* temp = head; correctly declares temp as a pointer and assigns it to head.
Final Answer:
struct Node* temp = head; -> Option C
Quick Check:
Pointer assignment = struct Node* temp = head [OK]
Hint: Use pointer variable to traverse linked list [OK]
Common Mistakes:
Using struct Node instead of pointer
Taking address of head incorrectly
Missing struct keyword if needed
3. What will be the output of the following C code snippet that traverses and prints a linked list?