AQA 7517, section 4.2.1.4
Abstract data types
An abstract data type (ADT) describes a data structure by what it does, meaning its values and the operations you can perform on it, and not by how it is built. The programmer using it doesn't need to know how it works inside.
For a stack, the abstract description is: you can push an item, pop an item, peek at the top, and test whether it is empty, and the most recently pushed item comes out first. That description says nothing about whether it's stored in an array or a linked list.
The actual way of building it is the implementation, or concrete data structure. The same ADT can be implemented in several ways.
# One implementation of the stack ADT, using a Python list
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
Code that uses Stack only calls push, pop and is_empty. If you later rewrote the inside to use a different structure, that code would keep working. This is a form of abstraction and information hiding.
The ADTs in the specification
You should know the concept and uses of each of these:
| ADT | Idea | Typical use |
|---|---|---|
| Queue | First in, first out | Printing, scheduling |
| Stack | Last in, first out | Undo, subroutine calls |
| Graph | Nodes connected by edges | Maps, networks |
| Tree | A connected graph with no cycles | File systems, searching |
| Hash table | Keys mapped to positions | Fast lookup |
| Dictionary | Key-value pairs | Information retrieval |
| Vector | An ordered list of numbers | Points and directions |
You should be able to use them in simple situations. You also need to know how to represent them when a language doesn't provide them as built-in types. For example, a stack or queue can be built from an array plus a pointer (or two) to track the top or the front and rear. A graph can be built with an adjacency matrix or list, and a tree from nodes with pointers to their children.
Static and dynamic structures
| Static | Dynamic | |
|---|---|---|
| Size | Fixed when it is created | Grows and shrinks as needed |
| Memory | Reserved in advance, even if unused | Used only as needed |
| Speed | Fast, because the size is known and elements sit together | Can be slower, because memory has to be found as it grows |
| Risk | Can overflow when full, or waste memory when mostly empty | Can use all available memory if it keeps growing |
| Example | A fixed-size array | A linked list, or a Python list |
Advantages of static: simple, predictable memory use, and fast access to elements by index.
Disadvantages of static: the size must be guessed in advance. Too small and the data won't fit, and too large wastes memory.
Advantages of dynamic: flexible, so it can handle any amount of data up to the memory limit, with no wasted space.
Disadvantages of dynamic: more complex to implement, and managing memory as the structure changes takes extra time.
Creation and maintenance of queues, stacks and hash tables
You should be able to describe how data is created and maintained in each one.
Stack (last in, first out), using an array and a pointer to the top:
- Create: make an empty array, with the top pointer set to "empty".
- Push: check it isn't full, move the pointer up, and store the item there.
- Pop: check it isn't empty, read the item at the pointer, and move the pointer down.
Queue (first in, first out), using an array with a front and a rear pointer:
- Create: an empty array, with both pointers at the start.
- Add (enqueue): check it isn't full, store the item at the rear, and move the rear pointer on.
- Remove (dequeue): check it isn't empty, take the item at the front, and move the front pointer on.
- In a circular queue, a pointer that reaches the end of the array wraps round to position 0, so freed space is reused.
- In a priority queue, items are served by priority, with the first-come-first-served order used only to break ties.
Hash table: a hash function turns each key into a position in an array. To add an item, you compute the position and store it there. To find or delete an item, you compute the same position and go straight to it. If two keys give the same position (a collision), a rule such as trying the next free slot is used. See 4.2.6.1.
Queues and stacks are covered in detail in Queues and Stacks.
Worked example
Task: a game needs to store a player's last 5 moves, and discard the oldest when a new move is made. Which ADT, and static or dynamic?
A queue suits this: the oldest move (first in) is the one removed (first out). Since the size never exceeds 5, a static structure, such as a circular queue in a fixed array of 5, works well and avoids using extra memory.
Exam tip: to explain "abstract", say that the data type is defined by the operations that can be done on it, and not by how it is stored. If asked to compare static and dynamic, give a point for each side, not just a list for one.
Check yourself
What is an abstract data type?
A description of a data structure in terms of the values it holds and the operations you can perform on it, without saying how it is implemented.
Give one advantage and one disadvantage of a static structure
Advantage: simple with fast access and predictable memory use. Disadvantage: the size is fixed, so it may overflow if too small or waste memory if too large.
Give one advantage and one disadvantage of a dynamic structure
Advantage: it can grow and shrink as needed, so memory isn't wasted. Disadvantage: it is more complex to implement and can be slower because memory has to be managed as it changes.
Why is the same ADT able to have different implementations?
Because the ADT only specifies the operations and their behaviour, not how the data is stored, so any structure that provides those operations correctly can be used.