Abstract Data Types

What Is an ADT? Abstract Data Types With Examples

A queue is a clear ADT example: enqueue, dequeue and front are defined, while the code behind them can change. See a working Python version.

On this page
  1. A quick example
  2. The queue ADT
  3. A Python implementation
  4. Using the queue
  5. Swapping the implementation
  6. Two more examples
  7. How to design your own ADT
  8. Summary

A quick example

An abstract data type is a set of values and operations, defined without fixing how they are stored. A queue is a clear example. Its operations are enqueue (add to the back), dequeue (remove from the front) and front (look at the front). Items leave in the order they arrived, which is first in, first out (FIFO).

Those rules are the whole definition. Whether the queue is built on a linked list, a circular array or a deque is a separate decision.

The queue ADT

The queue ADT specifies:

  • enqueue(item): add an item to the back
  • dequeue(): remove and return the front item
  • front(): return the front item without removing it
  • is_empty(): report whether the queue has any items

Calling dequeue on an empty queue is an error case. A good definition says what happens. In the code below, it raises an exception.

A Python implementation

Python’s collections.deque supports fast appends and pops at both ends, so it is a good base for a queue:

from collections import deque


class Queue:
    def __init__(self):
        self._items = deque()

    def enqueue(self, item):
        self._items.append(item)

    def dequeue(self):
        if self.is_empty():
            raise IndexError("dequeue from empty queue")
        return self._items.popleft()

    def front(self):
        if self.is_empty():
            raise IndexError("front from empty queue")
        return self._items[0]

    def is_empty(self):
        return len(self._items) == 0

The leading underscore on _items is a convention that signals “internal, don’t touch.” Callers should use the four methods.

Using the queue

q = Queue()
q.enqueue("print job 1")
q.enqueue("print job 2")
print(q.dequeue())   # print job 1
print(q.front())     # print job 2
print(q.is_empty())  # False

Print job 1 was added first, so it leaves first. After that, print job 2 is at the front.

Swapping the implementation

Here is a second version that keeps the same interface but stores the items in a plain list:

class ListQueue:
    def __init__(self):
        self._items = []

    def enqueue(self, item):
        self._items.append(item)

    def dequeue(self):
        if self.is_empty():
            raise IndexError("dequeue from empty queue")
        return self._items.pop(0)

    def front(self):
        if self.is_empty():
            raise IndexError("front from empty queue")
        return self._items[0]

    def is_empty(self):
        return len(self._items) == 0

The calling code from the previous section works unchanged with ListQueue, and the behavior is the same. The cost differs. Asking what constitutes an array helps here: it is a block of slots reached by index, so pop(0) has to shift every remaining item one slot forward. That takes time proportional to the number of items. With a long queue, the deque version is the better choice. This is the core idea of an ADT: the interface stays put while the data structure changes.

Two more examples

Priority queue. A priority queue is an ADT where the item with the highest priority leaves first, regardless of when it arrived. Its operations are typically insert and remove-the-top. It is commonly built on a binary heap, which is a tree-shaped structure stored in an array, and its operations are usually logarithmic in the number of items. If you want the details of that structure, the question what is a heap data structure is a good next step.

Map. A map, or dictionary, ADT stores key-value pairs and supports put, get and remove by key. A hash table or a balanced search tree can implement it. Callers see the same operations either way.

How to design your own ADT

When you define an ADT, write down:

  1. The values. What kind of data the ADT holds.
  2. The operations. Each method, with its inputs and results.
  3. The rules. Ordering, uniqueness or any other constraints.
  4. The error cases. What happens when the collection is empty or a key is missing.
  5. The expected cost. How fast each operation should be, which guides your choice of data structure.

Settle those first, and only then choose the storage.

Summary

  • An ADT defines values, operations and rules without fixing the storage.
  • A queue is a simple example: enqueue, dequeue and front, with first-in, first-out behavior.
  • Two implementations can satisfy the same ADT with different costs.
  • Designing an ADT starts with the interface and rules, then moves on to storage.

The complete guide to abstract data types

ADT vs. Data Structure: What's the Difference?
Read the guide

Keep exploring

Guides from other topics

All topics →