The simplest way to implement the list ADT is an array that holds the elements in order, plus a counter that says how many places are in use. This page builds that implementation and analyses its cost.
The structure
A contiguous list
#define MAX 10typedef struct { int entry[MAX]; /* the elements, in positions 0 .. count-1 */ int count; /* how many positions are in use */} List;
The elements occupy entry[0] to entry[count - 1]; the rest of the array is free. The list is empty when count == 0 and full when count == MAX; trying to add to a full list is called overflow, and trying to remove from an empty list is underflow.
Insertion
To insert x at index pos in a list of count elements:
If the list is full, or pos is not between 0 and count, report an error.
For i from count - 1 down to pos: copy entry[i] to entry[i + 1].
Put x into entry[pos] and increase count.
The loop moves count - pos elements: up to n in the worst case (insert at the front), 0 at the end. Insertion is O(n).
Deletion
To delete the element at index pos:
If the list is empty, or pos is not between 0 and count - 1, report an error.
For i from pos up to count - 2: copy entry[i + 1] to entry[i].
Decrease count.
This moves count - pos - 1 elements: deletion is also O(n) in the worst case.
Check Your Progress, question 5: the overflow test
The list is full when every array place is used, that is when count == MAX:
Overflow test
int is_full(const List *l){ return l->count == MAX; /* MAX = capacity of the array */}
Testing the count avoids the confusion of 0-based indices: when the list is full the last index used is MAX - 1, not MAX.
Key takeaways
An array list = array + counter; elements are in entry[0..count-1].
Insertion shifts elements right starting from the last one; deletion shifts left starting after the position.
Both are O(n) in the worst case; access by position is O(1).
Check overflow (count == MAX), underflow (count == 0) and the position before changing anything.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write a C function that deletes the element at index pos of an array list (structure with int entry[MAX] and int count) and returns 1 on success, 0 on error.
Show worked answer
int delete_at(List *l, int pos){ if (l->count == 0 || pos < 0 || pos >= l->count) return 0; /* underflow or bad position */ for (int i = pos; i < l->count - 1; i++) l->entry[i] = l->entry[i + 1]; /* close the gap */ l->count--; return 1;}
It moves count - pos - 1 elements, so it is O(n) in the worst case.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/lists/array-implementation-of-lists/ to read this page.