📖 Scenario: Imagine you are managing a playlist of songs on a music player. You want to understand when to use an array or a linked list to store your songs for easy adding, removing, and accessing.
🎯 Goal: You will create a simple array and a linked list to hold song names. Then, you will add songs, remove a song, and print the list to see how each data structure behaves.
📋 What You'll Learn
Create an array of 5 song names
Create a linked list node structure for songs
Add 3 songs to the linked list
Remove the second song from the linked list
Print the array and linked list contents
💡 Why This Matters
🌍 Real World
Music players, contact lists, and many apps use arrays or linked lists to store items depending on how often items change or need quick access.
💼 Career
Understanding when to use arrays or linked lists helps in writing efficient programs and choosing the right data structure for tasks like managing playlists, queues, or dynamic data.
Progress0 / 4 steps
1
Create an array of 5 song names
Create a char array called songs_array that can hold 5 song names, each up to 20 characters. Initialize it with these exact songs: "SongA", "SongB", "SongC", "SongD", and "SongE".
DSA C
Hint
Use a 2D char array to store multiple strings. Initialize it with the given song names inside curly braces.
2
Define a linked list node and create the head pointer
Define a struct Node with a char array song[20] and a pointer next to the next node. Then create a Node* pointer called head and set it to NULL.
DSA C
Hint
Define the node structure with a song name and a next pointer. Initialize the head pointer to NULL.
3
Add 3 songs to the linked list
Write a function add_song that takes a char* song name, creates a new node, copies the song name into it, and adds it to the end of the linked list starting at head. Then add these songs in order: "SongX", "SongY", "SongZ".
DSA C
Hint
Create a new node with malloc, copy the song name, and add it at the end of the list. Use a loop to find the last node.
4
Remove the second song from the linked list and print both lists
Write a function remove_second_song that removes the second node from the linked list starting at head. Then write a function print_songs_array to print all songs in songs_array, and a function print_linked_list to print all songs in the linked list. In main, call remove_second_song, then print the array and linked list songs.
DSA C
Hint
To remove the second node, adjust the first node's next pointer and free the removed node. Print the array by looping over indices, and print the linked list by traversing nodes.
Practice
(1/5)
1. Which data structure is better when you need fast random access to elements by index?
easy
A. Array
B. Linked List
C. Stack
D. Queue
Solution
Step 1: Understand random access in arrays
Arrays store elements in contiguous memory, so accessing any element by index is fast and direct.
Step 2: Compare with linked list access
Linked lists require traversal from the head to reach an element, making random access slower.
Final Answer:
Array -> Option A
Quick Check:
Random access = Array [OK]
Hint: For fast index access, choose arrays [OK]
Common Mistakes:
Thinking linked lists provide fast random access
Confusing stack or queue with random access
Ignoring memory layout differences
2. Which of the following is the correct way to declare an array of 5 integers in C?
easy
A. int arr{};
B. int arr();
C. int arr[5];
D. int arr = new int[5];
Solution
Step 1: Recall C array declaration syntax
In C, arrays are declared with type, name, and size in square brackets, like int arr[5];
Step 2: Check other options for errors
int arr(); uses parentheses which is a function declaration, C uses braces which is invalid, D uses 'new' which is C++ syntax.
Final Answer:
int arr[5]; -> Option C
Quick Check:
C array declaration = int arr[5]; [OK]
Hint: Use square brackets for arrays in C [OK]
Common Mistakes:
Using parentheses instead of brackets
Using C++ 'new' keyword in C
Using braces for array declaration
3. Consider the following C code snippet:
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* head = NULL;
// Insert 3 nodes at the beginning
for (int i = 1; i <= 3; i++) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = i;
newNode->next = head;
head = newNode;
}
// Print list
Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
What is the printed output?
medium
A. 1 -> 2 -> 3 -> NULL
B. 3 -> 2 -> 1 -> NULL
C. NULL
D. 3 -> 1 -> 2 -> NULL
Solution
Step 1: Understand insertion at beginning in linked list
Each new node is added at the head, so the last inserted node becomes the first in the list.
Step 2: Trace the loop values and list order
Insert 1: list is 1 -> NULL
Insert 2: list is 2 -> 1 -> NULL
Insert 3: list is 3 -> 2 -> 1 -> NULL
Final Answer:
3 -> 2 -> 1 -> NULL -> Option B
Quick Check:
Insertion at head reverses order [OK]
Hint: Insertion at head reverses order [OK]
Common Mistakes:
Assuming insertion appends at end
Confusing order of nodes after insertion
Ignoring pointer updates
4. The following code tries to insert a node at the end of a singly linked list but causes a runtime error. What is the error?
If *head is NULL, then temp = NULL. Accessing temp->next causes a runtime error (segmentation fault).
Step 2: Identify fix for empty list case
Need to check if *head is NULL before loop and assign newNode as head if true.
Final Answer:
Dereferencing NULL pointer when list is empty -> Option A
Quick Check:
Empty list causes NULL pointer error [OK]
Hint: Check for empty list before traversal [OK]
Common Mistakes:
Ignoring empty list case
Confusing memory leak with pointer error
Thinking missing return causes runtime error
5. You need to store a large number of elements where insertions and deletions happen frequently at random positions. Which data structure is more suitable and why?
A) Array because it uses less memory
B) Linked List because it allows efficient insertions and deletions
C) Array because it provides faster random access
D) Linked List because it uses contiguous memory
hard
A. Array because it uses less memory
B. Linked List because it uses contiguous memory
C. Array because it provides faster random access
D. Linked List because it allows efficient insertions and deletions
Solution
Step 1: Understand insertion and deletion costs
Arrays require shifting elements for insertions/deletions at random positions, which is costly.
Step 2: Compare with linked list behavior
Linked lists allow insertions and deletions by changing pointers without shifting elements, making them efficient for frequent changes.
Final Answer:
Linked List because it allows efficient insertions and deletions -> Option D
Quick Check:
Frequent insert/delete = Linked List [OK]
Hint: Frequent insert/delete? Choose linked list [OK]
Common Mistakes:
Choosing array for frequent insertions
Thinking linked list uses contiguous memory
Ignoring cost of shifting in arrays
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.