Describe how a queue, stack and linked list can be implemented using arrays
10.4 Introduction to Abstract Data Types (ADT)
Abstract Data Types (ADTs) let us think about data and operations without worrying about the low‑level details. In this section we’ll look at three classic ADTs – queue, stack and linked list – and see how they can be built on top of simple arrays. 📚
Queues – “First In, First Out” (FIFO)
Imagine a line at a coffee shop. The first person to arrive is the first to be served. A queue behaves the same way: items are enqueued at the rear and dequeued from the front.
Array implementation (circular queue)
- Two indices:
front(where we remove) andrear(where we add). - When
rearreaches the end of the array, it wraps around to the start – hence “circular”. - Use a
sizecounter to detect full/empty conditions.
| Index | Value |
|---|---|
| 0 | A |
| 1 | B |
| 2 | C |
Key operations
enqueue(item)– add atrear, thenrear = (rear + 1) mod capacity.dequeue()– remove fromfront, thenfront = (front + 1) mod capacity.
Stacks – “Last In, First Out” (LIFO)
Think of a stack of plates. The last plate you put on top is the first one you take off.
A stack keeps a single index, top, pointing to the current top element.
Array implementation
- Array of fixed size.
- Initially
top = -1(empty). - Push:
top++; store item atarray[top]. - Pop: retrieve
array[top]; thentop--.
push(item)– add to top.pop()– remove from top.peek()– look at top without removing.
Linked Lists – Dynamic Chains
Picture a chain of beads. Each bead knows the next bead in the chain. In a linked list each element (node) stores data and an index (or pointer) to the next node. Using an array we can simulate this by storing nodes in an array and using an integer as the “pointer”.
Array‑based linked list
- Array
nodes[capacity]where each entry holds{data, nextIndex}. - Maintain a
freeListhead pointing to the first free node. - Insert: take node from
freeList, set its data, link it into the list. - Delete: unlink node, return it to
freeList.
Example: inserting at head
- Let
newNode = freeList,freeList = nodes[newNode].nextIndex. - Set
nodes[newNode].data = value. - Set
nodes[newNode].nextIndex = head. - Update
head = newNode.
Exam Tips & Quick Review
- Queues are FIFO – think line at a ticket counter.
- Stacks are LIFO – think stack of books.
- Linked lists use
nextpointers – think beads linked together. - Array implementations require careful index management (especially circular queues).
- Always check for overflow (full) and underflow (empty) conditions.
Good luck! 🚀
Summary Table
| ADT | Order | Key Index(es) | Typical Operations |
|---|---|---|---|
| Queue | FIFO | front, rear | enqueue, dequeue, peek |
| Stack | LIFO | top | push, pop, peek |
| Linked List (array‑based) | Any order | head, nextIndex, freeList | insert, delete, traverse |
Revision
Log in to practice.