Sub topic 9
Data structure or ADT? Keeping the terms apart
This chapter assumes you have read the earlier chapters on interfaces, specifications, and the stack implementations. It also assumes you can read basic Python.
Two words, two jobs
People use "data structure" and "abstract data type" as if they meant the same thing. They do not.
- An abstract data type (ADT) is a set of operations plus the behavior they promise. It says what you can do and what happens. It says nothing about storage.
- A data structure is a concrete arrangement of data in memory, such as an array, a linked chain of nodes, or a hash table. It is a way to store values and reach them at a known cost.
An ADT is a promise. A data structure is one way to keep that promise.
| Row | ADT | Data structure |
|---|---|---|
| Describes | Operations and their behavior | Memory layout and access paths |
| Answers | "What does it do?" | "How is it stored?" |
| Examples | Stack, queue, set, map | Array, linked list, hash table, binary tree |
| Cost | Not part of the promise | Determines the cost |
The mapping is many-to-many
One ADT can sit on several data structures. ListStack uses an array-backed list, and LinkedStack uses a chain of nodes. Both satisfy the same specification.
One data structure can also serve several ADTs. Here a single Python list backs both a stack and a queue:
class ListStack:
def __init__(self):
self._items = []
def push(self, item):
self._items.append(item)
def pop(self):
if not self._items:
raise IndexError("pop from empty stack")
return self._items.pop()
class ListQueue:
def __init__(self):
self._items = []
def enqueue(self, item):
self._items.append(item)
def dequeue(self):
if not self._items:
raise IndexError("dequeue from empty queue")
return self._items.pop(0)
s, q = ListStack(), ListQueue()
for x in (1, 2, 3):
s.push(x)
q.enqueue(x)
print(s.pop(), q.dequeue()) # 3 1The storage is the same. The behavior differs: the stack returns the newest item, and the queue returns the oldest. The behavior comes from the ADT's operations, not from the list.
Common errors in naming
- Calling a linked list an ADT. A linked list is a data structure. It exposes its nodes and its pointer layout. A "List" ADT, with operations like
get(i)andinsert(i, x), could be implemented by a linked list or by an array. - Confusing the name with the type. Python's built-in
listis a concrete data structure, a dynamic array. It also offers many operations at once, such asinsert,pop(0), and slicing. A client that uses all of them depends on far more than a Stack promises. - Treating the data structure as the specification. Saying "the stack is an array" fixes the storage as part of the contract. Clients can then rely on it, and you can no longer change it safely.
- Assuming the ADT dictates speed. The specification fixes results, not cost.
ListQueue.dequeueabove is correct, butpop(0)shifts every remaining item, so it takes O(n) time. A later chapter compares costs.
A quick test
Ask whether you could swap the storage without a client noticing. If the answer is yes, you are talking about an ADT. If the answer depends on exposed details like indexes or node links, you are talking about a data structure.
Exercise
A teammate says, "Our Queue is a linked list." Rewrite this in precise terms, and say why the precise version is better for clients.
Show answer
"Our Queue ADT is implemented with a linked list as its data structure." The Queue is the specification of enqueue, dequeue, and their behavior. The linked list is one storage choice. The precise version tells clients to rely only on the Queue operations, so the team can later switch to a different structure without changing client code.