MCS-208 · Unit 3

Lists

Section 1 of 8

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

3.1The List as an Abstract Data Type

Covers IGNOU MCS-208 §3.0, 3.2

Before writing any code for a list, we should be clear about what a list must do, separately from how it will be stored. This separation is called an abstract data type.

Abstract data types

An ADT is a contract. A program that uses only the ADT’s operations keeps working when the storage method changes, for example from an array to a linked list. Efficiency (time and space) belongs to an implementation, not to the ADT itself.

The list ADT

“Ordered” means each element has a position (first, second, …), not that the values are sorted. All elements have the same type T; to keep records with several fields (name, date of joining, salary), T is a structure.

OperationWhat it does
create(L)Make L an empty list
is_empty(L)True if L has no elements
is_full(L)True if no more elements can be added (only meaningful for fixed-size storage)
size(L)Number of elements nn
insert(L, pos, x)Put x at position pos; elements from pos onward move one place back
delete(L, pos)Remove the element at pos; later elements move one place forward
search(L, x)Position of x, or “not found”
traverse(L)Visit every element in order (e.g. to print it)

Two ways to implement a list

Contiguous (array) implementationLinked implementation
StorageElements next to each other in an array, plus a countEach element in a separate node holding the data and a pointer to the next node
MemoryFixed size chosen in advanceAllocated with malloc one node at a time
Access to element iiO(1)O(1)O(i)O(i): follow ii links
Insert/delete at a known placeO(n)O(n): shift elementsO(1)O(1): change a few pointers

Key takeaways

  • An ADT = a set of values + operations, specified without implementation details.
  • The list ADT is a finite ordered sequence of elements of one type with create, insert, delete, search, size, traverse and empty/full tests.
  • The same ADT can be implemented with an array or with linked nodes.
  • Efficiency belongs to the implementation, not to the ADT.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which statement best describes an abstract data type?

Show answer

Answer: B. A set of values and operations defined by their behaviour, not their implementation

An ADT is a specification. A typedef struct is one possible implementation of it, not the ADT itself.

Question 2 · multiple choice

After create(L), insert(L, 1, 5), insert(L, 1, 8), insert(L, 2, 3) (positions from 1), what is L?

Show answer

Answer: C. ⟨8, 3, 5⟩

⟨5⟩ → ⟨8, 5⟩ → 3 goes to position 2: ⟨8, 3, 5⟩. Appending each value at the end would give ⟨5, 8, 3⟩, but insert puts the value at the given position.

Question 3 · multiple choice

For which implementation is is_full a meaningful operation?

Show answer

Answer: A. The array implementation

An array has a fixed capacity. A linked list is full only when the computer runs out of memory, which malloc reports by returning NULL.

Question 4 · multiple choice

What does “ordered” mean in “a list is a finite ordered sequence”?

Show answer

Answer: C. Each element has a definite position: first, second, and so on.

Order refers to positions, not values. A list may be unsorted and may contain duplicates.

Question 5 · multiple choice

A list ⟨A, C, D⟩ has delete(L, 1) and then insert(L, 3, 'E') applied. What is the result?

Show answer

Answer: D. ⟨C, D, E⟩

Deleting position 1 gives ⟨C, D⟩; position 3 is just after the end, so E is appended. ⟨C, E, D⟩ would put E at position 2.

Question 6 · short answer

Define an abstract data type and give the specification of the list ADT with its operations.

Show worked answer

An ADT is a set of values together with operations on them, specified by what the operations do, independent of how the data is stored or how the operations are coded. List ADT: a list of type T is a finite ordered sequence ⟨a₁, …, aₙ⟩ of elements of type T (n ≥ 0). Operations: create (make an empty list), is_empty, is_full (fixed-size implementations), size (return n), insert(pos, x) (put x at position pos, later elements move back), delete(pos) (remove the element at pos, later elements move forward), search(x) (return the position of x or “not found”), traverse (visit every element in order). It can be implemented with an array or a linked list.

↑ ↓ move↵ openTab switch type