Bird
0
0
DSA Cprogramming~10 mins

Creating a Singly Linked List from Scratch in DSA C - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating a Singly Linked List from Scratch
Start
Create head node with data
Set head.next = NULL
For each new data item
Create new node
Traverse list to last node
Set last_node.next = new_node
Set new_node.next = NULL
Repeat or End
We start by creating the first node as head. For each new item, we create a node, find the last node, and link the new node at the end.
Execution Sample
DSA C
#include <stdlib.h>

struct Node {
  int data;
  struct Node* next;
};

struct Node *head;

// Create head node
head = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = NULL;
This code creates the first node of the singly linked list with data 10 and sets its next pointer to NULL.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create head node with data=1010head -> Node(10)10 -> NULL
2Create new node with data=2010, 20new_node -> Node(20)10 -> NULL, 20 -> NULL
3Traverse to last node (10)10, 20current -> Node(10)10 -> NULL, 20 -> NULL
4Link last node next to new_node10, 2010.next -> 2010 -> 20 -> NULL
5Create new node with data=3010, 20, 30new_node -> Node(30)10 -> 20 -> NULL, 30 -> NULL
6Traverse to last node (20)10, 20, 30current -> Node(20)10 -> 20 -> NULL, 30 -> NULL
7Link last node next to new_node10, 20, 3020.next -> 3010 -> 20 -> 30 -> NULL
8End of insertions10, 20, 30No pointer changes10 -> 20 -> 30 -> NULL
💡 All nodes inserted; last node's next pointer is NULL indicating end of list.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 5After Step 7Final
headNULLNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)
new_nodeNULLNULLNode(20)Node(20)Node(30)Node(30)NULL
currentNULLNULLNULLNode(10)Node(10)Node(20)NULL
List Size0112233
Key Moments - 3 Insights
Why do we set the new node's next pointer to NULL?
Because the new node is the last node in the list, its next pointer must be NULL to mark the end. See steps 1, 2, 5 in execution_table where new nodes are created with next = NULL.
Why do we traverse to the last node before linking the new node?
We must find the current last node to link its next pointer to the new node. This is shown in steps 3 and 6 where current points to the last node before linking.
What happens if the list is empty when inserting the first node?
If the list is empty, head is NULL. We create the first node and assign it to head, as in step 1. No traversal is needed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the visual state of the list?
A10 -> 20 -> NULL
B10 -> NULL, 20 -> NULL
C20 -> 10 -> NULL
D10 -> 20 -> 30 -> NULL
💡 Hint
Check the Visual State column at step 4 in execution_table.
At which step does the list size become 3 according to variable_tracker?
AAfter Step 4
BAfter Step 5
CAfter Step 7
DAfter Step 2
💡 Hint
Look at the List Size row in variable_tracker and find when it reaches 3.
If we forget to set new_node->next = NULL when creating a new node, what will happen to the list?
AThe list will end correctly with NULL.
BThe list may have a cycle or undefined end.
CThe head pointer will be lost.
DThe new node will not be linked.
💡 Hint
Think about the importance of NULL in marking the end of the list as shown in key_moments.
Concept Snapshot
Creating a Singly Linked List:
- Define Node with data and next pointer
- Create head node with data and next = NULL
- For each new data:
  - Create new node with next = NULL
  - Traverse to last node
  - Link last node's next to new node
- Last node's next always NULL to mark list end
Full Transcript
To create a singly linked list from scratch, start by defining a node structure with data and a next pointer. Create the head node with the first data value and set its next pointer to NULL, indicating the end of the list. For each new data item, create a new node with next set to NULL. Then, traverse the list from head to find the last node whose next pointer is NULL. Link this last node's next pointer to the new node. Repeat this process for all new data items. The last node's next pointer always remains NULL to mark the end of the list. This process ensures a chain of nodes connected by next pointers, forming the singly linked list.