- 2068 Bhadra · 6 marks
Linked list considerations; array list in C
Short answer
Linked-list algorithm considerations include handling empty list, updating start for beginning insert/delete, setting new node's link before changing predecessor, checking malloc failure, freeing deleted nodes, and using trailing pointers safely. Array list in C uses a fixed array with n elements; insert checks overflow then shifts right, delete checks underflow then shifts left, and traverse prints list[0..n-1].
What are the considerations to be taken while developing an algorithm using linked list? Explain array implementation of list in C with suitable example.
Considerations while developing a linked-list algorithm
- Empty list: check
start == NULLbefore deleting, traversing or searching; handle the first insertion into an empty list separately. - Insertion/deletion at the beginning: the external pointer
startitself must be changed. - Insertion/deletion at the end: reach the last node (or keep a
rearpointer); the last node'snextmust beNULL. - Pointer order: when inserting, set the new node's link first and then change the predecessor's link, otherwise the rest of the list is lost. When deleting, save the node in a temporary pointer before bypassing it and then
free()it. - Memory allocation: check that
mallocdid not returnNULL(no space); release deleted nodes to avoid memory leaks. - Single node list: first node and last node are the same.
- Traversal safety: never use a pointer after it becomes
NULL; keep a trailing pointer (prev) when the previous node is needed. - Dangling pointers: do not access a node after freeing it.
- Doubly/circular lists: update both links (
prevandnext), and in circular lists make sure the loop terminates when the start is reached again. - Choose the variant (singly, doubly, circular, header node) suited to the operations needed.
Array implementation of a list in C
The elements are kept in an array; n stores the number of elements. Insert and delete shift the elements to keep them contiguous.
#include <stdio.h>
#define MAX 10
int list[MAX]; /* contiguous storage */
int n = 0; /* number of elements currently in the list */
int insert(int pos, int x) { /* pos = 0..n */
if (n == MAX || pos < 0 || pos > n) return 0; /* overflow / bad position */
for (int i = n - 1; i >= pos; i--) list[i + 1] = list[i]; /* shift right */
list[pos] = x;
n++;
return 1;
}
int delete_at(int pos) { /* pos = 0..n-1 */
if (n == 0 || pos < 0 || pos >= n) return 0; /* underflow / bad position */
for (int i = pos; i < n - 1; i++) list[i] = list[i + 1]; /* shift left */
n--;
return 1;
}
void traverse(void) {
for (int i = 0; i < n; i++) printf("%d ", list[i]);
printf("\n");
}
int main(void) {
insert(0, 10); insert(1, 30); insert(1, 20); /* 10 20 30 */
traverse();
delete_at(0); /* 20 30 */
traverse();
return 0;
}
Output:
10 20 30
20 30
Insertion of 20 at position 1 shifts 30 right; deleting position 0 shifts 20, 30 left.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Array implementation of list in C · Data Structure and Algorithm, chapter 3
- Linked stack overcoming array demerits · Data Structure and Algorithm, chapter 4
- Algorithm to create a single linked list · Data Structure and Algorithm, chapter 4
- Stack and queue using linked list · Data Structure and Algorithm, chapter 4 · asked 2×
- Doubly linked list: bank customer records · Data Structure and Algorithm, chapter 4
- Linear queue using array · Data Structure and Algorithm, chapter 2 · asked 2×
- Precautions while applying cement mortar · Civil Engineering Materials, chapter 6 · asked 3×
- Electrical considerations in conductor selection · Transmission and Distribution System Design, chapter 4 · asked 3×
- Considerations for siting an area substation · Transmission and Distribution System Design, chapter 7 · asked 2×
- User interface design considerations · Information System, chapter 5