📖 Scenario: Imagine you want to keep a list of your favorite fruits in order. You want to add fruits one by one and see the list grow. A singly linked list is a simple way to do this, where each fruit points to the next one.
🎯 Goal: You will build a singly linked list in C from scratch. You will create nodes, link them, and print the list to see all fruits in order.
📋 What You'll Learn
Define a struct for a node with a string and a pointer to the next node
Create three nodes with the fruits: "Apple", "Banana", and "Cherry"
Link the nodes to form a list: Apple -> Banana -> Cherry -> NULL
Print the list showing each fruit followed by an arrow, ending with NULL
💡 Why This Matters
🌍 Real World
Linked lists are used in many programs to manage lists of items where size can change, like playlists, task lists, or undo history.
💼 Career
Understanding linked lists is fundamental for software developers, especially when working with low-level data structures or preparing for technical interviews.
Progress0 / 4 steps
1
Define the Node Structure and Create the First Node
Define a struct called Node with a char* called data and a pointer to Node called next. Then create a variable called head of type Node* and assign it a new node with data set to "Apple" and next set to NULL.
DSA C
Hint
Use typedef struct Node to define the node. Use malloc to create the first node and set its data and next.
2
Create Two More Nodes and Link Them
Create two more Node* variables called second and third. Assign each a new node with data set to "Banana" and "Cherry" respectively, and next set to NULL. Then link the nodes by setting head->next to second and second->next to third.
DSA C
Hint
Use malloc to create second and third nodes. Set their data and next. Link them by setting head->next and second->next.
3
Write a Loop to Print the Linked List
Create a Node* variable called current and set it to head. Use a while loop that runs while current is not NULL. Inside the loop, print current->data followed by " -> ". Then update current to current->next. After the loop, print "NULL\n".
DSA C
Hint
Use a while loop with current to go through the list. Print each node's data and then move to the next node.
4
Print the Final Linked List Output
Add a printf statement to print the entire linked list in the format: Apple -> Banana -> Cherry -> NULL by running the loop you wrote.
DSA C
Hint
Run the program to see the linked list printed as Apple -> Banana -> Cherry -> NULL.
Practice
(1/5)
1. What is the main purpose of a singly linked list in data structures?
easy
A. To store elements with direct access by index like an array
B. To store elements in a sequence where each element points to the next
C. To store elements in a sorted order automatically
D. To store elements with multiple pointers to previous nodes
Solution
Step 1: Understand singly linked list structure
A singly linked list stores elements where each node points only to the next node, forming a chain.
Step 2: Compare with other data structures
Unlike arrays, singly linked lists do not provide direct index access, and unlike doubly linked lists, nodes have only one pointer.
Final Answer:
To store elements in a sequence where each element points to the next -> Option B
Quick Check:
Singly linked list = sequence with next pointers [OK]
Hint: Remember: singly linked list nodes have one next pointer only [OK]
Common Mistakes:
Thinking singly linked list allows direct index access
Confusing singly linked list with doubly linked list
Assuming automatic sorting in singly linked list
2. Which of the following is the correct way to define a node structure for a singly linked list in C?
easy
A. typedef struct Node { int data; struct Node* next; } Node;
B. typedef struct Node { int data; Node* next; } Node;
C. struct Node { int data; struct Node next; };
D. struct Node { int data; struct Node* prev; };
Solution
Step 1: Check pointer usage in struct
The next pointer must be a pointer to struct Node, so use struct Node* next;.
Step 2: Verify typedef correctness
typedef struct Node { int data; struct Node* next; } Node; correctly uses typedef and pointer syntax for next node.
Final Answer:
typedef struct Node { int data; struct Node* next; } Node; -> Option A
Quick Check:
Node struct uses pointer to struct Node for next [OK]
Hint: Use 'struct Node* next;' inside struct for next pointer [OK]
Common Mistakes:
Omitting 'struct' keyword before Node* inside struct
Using 'Node* next;' without typedef defined yet
Using 'struct Node next;' instead of pointer
Using 'prev' pointer instead of 'next' for singly linked list
3. What will be the output of the following C code snippet that creates a singly linked list with two nodes and prints their data?
C. head is not initialized before accessing its members
D. struct Node definition is incorrect
Solution
Step 1: Check pointer initialization
head is declared but not assigned memory before accessing its members.
Step 2: Understand consequences
Accessing head->data without allocation causes undefined behavior or crash.
Final Answer:
head is not initialized before accessing its members -> Option C
Quick Check:
Pointer must be allocated before use [OK]
Hint: Always allocate memory before using pointer members [OK]
Common Mistakes:
Assuming pointer is auto-initialized
Confusing next pointer direction
Thinking data must be pointer type
5. You want to create a singly linked list from scratch that stores integers 1, 2, 3 in order. Which sequence of operations correctly builds the list?
hard
A. Allocate node1, set data=1, set node1->next=NULL, allocate node2, set data=2, set node2->next=NULL, allocate node3, set data=3, set node3->next=NULL
B. Allocate node1, set data=1, allocate node2, set data=2, allocate node3, set data=3, link node3->next=node2, link node2->next=node1, set node1->next=NULL
C. Allocate node1, set data=1, link node1->next=node2, allocate node2, set data=2, link node2->next=node3, allocate node3, set data=3, set node3->next=NULL
D. Allocate node1, set data=1, allocate node2, set data=2, link node1->next=node2, allocate node3, set data=3, link node2->next=node3, set node3->next=NULL
Solution
Step 1: Allocate nodes and assign data in order
Nodes must be allocated and data assigned before linking.
Step 2: Link nodes in correct sequence
Link node1 to node2, node2 to node3, and node3 to NULL to end list.
Final Answer:
Allocate node1, set data=1, allocate node2, set data=2, link node1->next=node2, allocate node3, set data=3, link node2->next=node3, set node3->next=NULL -> Option D
Quick Check:
Correct order and linking builds list 1->2->3->NULL [OK]
Hint: Link nodes only after allocating and setting data [OK]
Common Mistakes:
Linking nodes before allocation
Not setting last node's next to NULL
Linking nodes in reverse order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.