📖 Scenario: Imagine you are managing a line of people waiting to buy tickets. Each person joins the end of the line. We will represent this line using a linked list, where each node is a person.
🎯 Goal: You will build a simple linked list in C and learn how to add a new person (node) at the end of the line (tail insert).
📋 What You'll Learn
Create a linked list node structure with an integer data field and a next pointer
Create an initial linked list with two nodes having values 10 and 20
Create a function to insert a new node at the end of the linked list
Insert a new node with value 30 at the end
Print the linked list to show the final order
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like managing playlists, undo functionality in editors, and dynamic memory allocation.
💼 Career
Understanding linked lists and how to insert nodes is fundamental for software developers working with data structures and system programming.
Progress0 / 4 steps
1
Create the initial linked list with two nodes
Create a struct called Node with an int data and a Node* next. Then create two nodes called head and second. Set head->data to 10, second->data to 20, and link head->next to second. Set second->next to NULL.
DSA C
Hint
Remember to use malloc to create nodes and link them by setting the next pointer.
2
Create a function to insert a node at the end
Create a function called insertAtEnd that takes Node** head and int data. Inside, create a new node with data and next as NULL. If the list is empty (*head == NULL), set *head to the new node. Otherwise, traverse the list using a Node* temp until temp->next == NULL, then set temp->next to the new node.
DSA C
Hint
Use a double pointer Node** head to modify the head pointer if the list is empty.
3
Insert a new node with value 30 at the end
Call the function insertAtEnd with the address of head and the value 30 to add a new node at the end of the list.
DSA C
Hint
Call insertAtEnd with &head and 30 inside main.
4
Print the linked list to show the final order
Create a Node* variable called temp and set it to head. Use a while loop to traverse the list while temp != NULL. Inside the loop, print temp->data followed by -> . Move temp to temp->next. After the loop, print NULL.
DSA C
Hint
Use a while loop to print each node's data followed by -> , then print NULL at the end.
Practice
(1/5)
1. What does the 'Insert at End' operation do in a singly linked list?
easy
A. Adds a new node at the end (tail) of the list
B. Deletes the last node of the list
C. Adds a new node at the beginning (head) of the list
D. Searches for a node at the end of the list
Solution
Step 1: Understand the operation
Inserting at the end means adding a new node after the current last node.
Step 2: Identify the effect on the list
The new node becomes the new tail, extending the list by one node.
Final Answer:
Adds a new node at the end (tail) of the list -> Option A
Quick Check:
Insert at end = Add node at tail [OK]
Hint: Insert at end means add after last node [OK]
Common Mistakes:
Confusing insert at end with insert at beginning
Thinking it deletes a node
Assuming it searches instead of inserts
2. Which of the following is the correct syntax to create a new node in C for tail insertion?
easy
A. newNode = malloc(sizeof(Node));
B. struct Node newNode = malloc(sizeof(struct Node));
C. Node newNode = malloc(sizeof(Node));
D. struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
Solution
Step 1: Understand node creation in C
We use malloc to allocate memory and cast it to pointer of struct Node.
Step 2: Check syntax correctness
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); correctly casts malloc result to (struct Node*) and uses sizeof(struct Node).
Final Answer:
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); -> Option D
A. temp = newNode; does not link newNode to the list
B. Missing malloc for newNode
C. newNode->next should not be NULL
D. Head pointer is not updated when list is empty
Solution
Step 1: Analyze the while loop and insertion
The code moves temp to last node, but then does temp = newNode; which only changes temp pointer.
Step 2: Identify correct linking
To link newNode, temp->next should be assigned newNode, not temp itself.
Final Answer:
temp = newNode; does not link newNode to the list -> Option A
Quick Check:
Link newNode by temp->next = newNode [OK]
Hint: Assign newNode to temp->next, not temp [OK]
Common Mistakes:
Assigning newNode to temp pointer instead of temp->next
Forgetting to set newNode->next to NULL
Not handling empty list case
5. You have a singly linked list with nodes 10 -> 20 -> 30 -> NULL. You want to insert 40 at the end using tail insertion. Which of the following code snippets correctly performs this insertion and updates the list?