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.
Operation
What 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 n
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) implementation
Linked implementation
Storage
Elements next to each other in an array, plus a count
Each element in a separate node holding the data and a pointer to the next node
Memory
Fixed size chosen in advance
Allocated with malloc one node at a time
Access to element i
O(1)
O(i): follow i links
Insert/delete at a known place
O(n): shift elements
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 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.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/lists/the-list-as-an-abstract-data-type/ to read this page.