MCS-208 · Unit 3

Lists

Section 2 of 8

Block 1 · Unit 3Lists11 min · Section 2 of 8

3.2Array Implementation of Lists

Covers IGNOU MCS-208 §3.3

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 10
typedef 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:

  1. If the list is full, or pos is not between 0 and count, report an error.
  2. For i from count - 1 down to pos: copy entry[i] to entry[i + 1].
  3. Put x into entry[pos] and increase count.

The loop moves count - pos elements: up to nn in the worst case (insert at the front), 0 at the end. Insertion is O(n)O(n).

Deletion

To delete the element at index pos:

  1. If the list is empty, or pos is not between 0 and count - 1, report an error.
  2. For i from pos up to count - 2: copy entry[i + 1] to entry[i].
  3. Decrease count.

This moves count - pos - 1 elements: deletion is also O(n)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)O(n) in the worst case; access by position is O(1)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 1 · multiple choice

An array list holds 6 elements. How many elements move when a new element is inserted at index 2?

Show answer

Answer: C. 4

Elements at indices 2, 3, 4, 5 move one place right: count - pos = 6 − 2 = 4. 2 would be the number of elements before the position, which stay where they are.

Question 2 · multiple choice

Which condition means an array list with capacity MAX is full?

Show answer

Answer: B. count == MAX

count elements occupy indices 0 to count - 1, so the array is full when count reaches MAX. entry[MAX] is outside the array and must never be read.

Question 3 · multiple choice

What does this program print?

C · q3-shift.c
#include <stdio.h>

int main(void)
{
    int a[6] = {1, 2, 3, 4};
    int count = 4, pos = 1;
    for (int i = pos; i < count; i++)    /* wrong direction */
        a[i + 1] = a[i];
    a[pos] = 9;
    count++;
    for (int i = 0; i < count; i++)
        printf("%d ", a[i]);
    printf("\n");
    return 0;
}
Show answer

Answer: B. 1 9 2 2 2

Shifting from the front copies 2 into a[2], then that 2 into a[3], then into a[4], so the old 3 and 4 are lost. The correct loop runs from count - 1 down to pos and gives 1 9 2 3 4.

Question 4 · multiple choice

What is the worst-case time to delete an element from an array list of nn elements?

Show answer

Answer: C. O(n)O(n)

Deleting the first element moves the other n−1n - 1 elements one place left. Deleting the last element is O(1)O(1), but that is the best case.

Question 5 · multiple choice

Trying to delete from an empty list is called:

Show answer

Answer: D. underflow

Underflow = removing from an empty structure; overflow = adding to a full one.

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)O(n) in the worst case.

↑ ↓ move↵ openTab switch type