---
title: "Linked list considerations; array list in C"
subject: "Data Structure and Algorithm"
chapter: 3
key: "semester_4/data_structure_and_algorithm:linked-list-considerations-array-list"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/linked-list-considerations-array-list
site: BCT Engineering
---

# Linked list considerations; array list in C

Data Structure and Algorithm · Chapter 3: List

- Asked in: 2068 Bhadra, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/linked-list-considerations-array-list
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-3

## Question

What are the considerations to be taken while developing an algorithm using linked list? Explain array implementation of list in C with suitable example.

## 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].

## Answer

#### Considerations while developing a linked-list algorithm
1. **Empty list**: check `start == NULL` before deleting, traversing or searching; handle the first insertion into an empty list separately.
2. **Insertion/deletion at the beginning**: the external pointer `start` itself must be changed.
3. **Insertion/deletion at the end**: reach the last node (or keep a `rear` pointer); the last node's `next` must be `NULL`.
4. **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.
5. **Memory allocation**: check that `malloc` did not return `NULL` (no space); release deleted nodes to avoid memory leaks.
6. **Single node list**: first node and last node are the same.
7. **Traversal safety**: never use a pointer after it becomes `NULL`; keep a trailing pointer (`prev`) when the previous node is needed.
8. **Dangling pointers**: do not access a node after freeing it.
9. **Doubly/circular lists**: update both links (`prev` and `next`), and in circular lists make sure the loop terminates when the start is reached again.
10. 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.

```c
#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:
```text
10 20 30
20 30
```
Insertion of 20 at position 1 shifts 30 right; deleting position 0 shifts 20, 30 left.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. When inserting a node in a linked list, why should the new node's link be set before changing the predecessor's link?
   A. Otherwise the rest of the list is lost
   B. Otherwise the new node is freed
   C. Otherwise the list becomes circular automatically
   D. It makes insertion O(1) instead of O(n)
   Answer: A. If the predecessor's link is changed first, nothing remembers the old next node. (Key point: Pointer order)

2. What must be changed when inserting or deleting at the beginning of a linked list?
   A. Only the last node's next
   B. The external pointer start itself
   C. Every node's info field
   D. Nothing, as the first node is special-cased by the compiler
   Answer: B. The head pointer start refers to the first node, so it must be updated. (Key point: Insert at beginning)

3. In the array list insert(pos, x), what is done before storing x at list[pos]?
   A. Elements from 0 to pos are shifted left
   B. The array is sorted
   C. Elements from pos to n-1 are shifted one place right, starting from the last
   D. The array is reallocated with size n+1
   Answer: C. The loop runs i = n-1 down to pos copying list[i] to list[i+1] so nothing is overwritten. (Key point: Insertion in array list)

4. In the array implementation, which condition makes insert fail?
   A. n == 0
   B. pos equal to 0
   C. list[pos] already holds a value
   D. n == MAX or pos is outside 0..n
   Answer: D. A full array (n == MAX) or an invalid position makes insertion impossible. (Key point: Overflow check)

## Related in Data Structure and Algorithm

- [Array implementation of list in C](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aarray-list-c-limitations) — Data Structure and Algorithm, chapter 3
- [Linked stack overcoming array demerits](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astack-linked-overcome-array-demerits) — Data Structure and Algorithm, chapter 4
- [Algorithm to create a single linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Acreate-single-linked-list) — Data Structure and Algorithm, chapter 4
- [Stack and queue using linked list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astack-queue-singly-linked-list) — Data Structure and Algorithm, chapter 4, asked 2×
- [Doubly linked list: bank customer records](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adoubly-bank-customer) — Data Structure and Algorithm, chapter 4
- [Linear queue using array](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Alinear-queue-array-implementation) — Data Structure and Algorithm, chapter 2, asked 2×

## Related in other subjects

- [Precautions while applying cement mortar](https://www.bctengineer.com/question.md?key=semester_3%2Fcivil_engineering_materials%3Aprecautions-applying-cement-mortar) — Civil Engineering Materials, chapter 6, asked 3×
- [Electrical considerations in conductor selection](https://www.bctengineer.com/question.md?key=semester_8%2Ftransmission_and_distribution_system_design%3Aconductor-electrical-considerations) — Transmission and Distribution System Design, chapter 4, asked 3×
- [Considerations for siting an area substation](https://www.bctengineer.com/question.md?key=semester_8%2Ftransmission_and_distribution_system_design%3Aarea-substation-siting) — Transmission and Distribution System Design, chapter 7, asked 2×
- [User interface design considerations](https://www.bctengineer.com/question.md?key=semester_8%2Finformation_system%3Auser-interface-design) — Information System, chapter 5
