Introduction to Abstract Data Types (ADT)

Computer Science AS · Data types and structures. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science AS topics

The idea

An abstract data type is a collection of data and a set of operations on that data. It says what can be done with the data, not how the data is stored.

A stack is last in, first out: items are added and removed at the same end, the top. A queue is first in, first out: items join at the rear and leave from the front. A linked list is a chain of nodes. Each node holds data and a pointer to the next node, so an item is inserted or deleted by changing pointers and no data is moved.

All three can be built from arrays together with integer variables used as pointers.

In simpler words

An abstract data type is a set of data plus the things you can do with it. It says what you can do, not how it is stored. A stack takes items from the top only, so the last one in comes out first. A queue lets the oldest item out first. A linked list joins items with pointers, so nothing needs moving.

An everyday comparison

A stack is a pile of plates: you add and take from the top. A queue is a line at a bus stop: the first person to arrive boards first. A linked list is a treasure hunt where each clue tells you where the next one is. The comparison stops working because in a program these are built from arrays and pointer variables, with fixed limits that can be full or empty.

Rules to remember

Common mistake

Removing from the wrong end. A queue gives up its oldest item and a stack gives up its newest item. Write down the contents after every operation.

In the exam

Name the structure and its rule: stack is LIFO, queue is FIFO. For each operation, state the checks first, then the pointer change: for push, check the stack is not full, add 1 to TopPointer, store the item. For pop, check not empty, take the item, subtract 1. For a circular queue, explain the pointer wrapping to the lower bound. For a linked list insert, state which pointer changes first. Show the contents after every operation when tracing.

Worked example

The values 4, 9 and 2 are added, in that order, to an empty stack and also to an empty queue. Two items are then removed from each. Which item is left in each structure?

  1. Stack: 2 was pushed last, so it is popped first, then 9. The item 4 is left.
  2. Queue: 4 joined first, so it leaves first, then 9. The item 2 is left.

Answer: The stack holds 4 and the queue holds 2.

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

← FilesProgramming basics →Take a Computer Science AS test