📘 CodingMarble Learn

Data Structures: Arrays, Lists, Stacks, Queues and Trees

A data structure is a way of organising data in memory so a program can use it well. Arrays keep items in numbered boxes for instant access by index. Linked lists chain nodes with pointers, so inserting is easy. Stacks work last-in-first-out, queues first-in-first-out. Dictionaries find values by key, and trees store data in levels so searching is fast. Choosing the right structure makes programs faster and simpler.

🎬 Step-by-step story

  1. Array: boxes in a row, each with an index starting at 0. To read A[3], the computer jumps straight to it in one step.
  2. Linked list: each node holds a value and a pointer to the next node. To insert 25, just change two pointers.
  3. Stack: push and pop only at the top. The last plate in is the first plate out. This is LIFO.
  4. Queue: join at the rear, leave from the front. First in, first out. This is FIFO, like a ticket line.
  5. Binary search tree: smaller values go left, bigger values go right. Finding 60 takes only 3 steps.
  6. Your turn: pick a stack or a queue, then add and remove items. Watch which one leaves first.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why do arrays start at index 0?

The index is the distance from the first box. The first box is 0 steps away, so its index is 0. Address = base + index × size.

If linked lists are easy to insert into, why use arrays at all?

Arrays jump straight to any index; linked lists must walk from the head. Pick arrays for fast reading, linked lists for lots of inserting.

Can I take an item from the middle of a stack?

Not as a stack. The rules only allow the top. If you need the middle, you need a different structure.

What is the difference between a stack and a queue in one line?

Stack: newest leaves first (LIFO). Queue: oldest leaves first (FIFO).

Why is a binary search tree fast?

Each comparison sends you left or right, skipping about half of the remaining values.

What happens if I remove from an empty stack or queue?

That is underflow. A good program checks isEmpty first. Try it in free play.

What is a data structure?

A data structure is a way to store and organise data so that we can use it well: find it, add to it, remove from it, sort it.

An abstract data type (ADT) describes what a structure does (its operations) without saying how it is built. A stack ADT promises push, pop, peek and isEmpty; it could be built from an array or a linked list.

Arrays, records, tuples and lists

An array stores items of the same type in neighbouring memory boxes. Each box has an index, usually starting at 0. Because the boxes are side by side, the computer can jump to A[i] in one step. But inserting in the middle means sliding every later item along.

A 2D array is a grid, like a table or a chessboard: M[row][col]. A string is an array of characters.

A record groups different kinds of data about one thing, such as a student: name (text), roll number (integer), marks (float). Each part is a field. A file stores many records permanently on disk.

In Python, a list [3, 8, 5] is a dynamic array that can grow; a tuple (3, 8, 5) cannot be changed after it is made.

A linked list is a chain of nodes. Each node stores a value and a pointer to the next node; the last points to null. The first node is the head. Inserting or deleting only changes pointers, so nothing slides; but to reach the 100th item you must walk through 99 nodes.

Stacks and queues

A stack is LIFO: last in, first out. You only touch the top.

Reverse Polish notation (RPN) writes the operator after its numbers: 3 4 + means 3 + 4. A stack evaluates it: push numbers; on an operator, pop two, calculate, push the result. RPN needs no brackets.

A queue is FIFO: first in, first out. Items join at the rear (enqueue) and leave from the front (dequeue). Uses: printer jobs, keyboard buffers, customer service lines, tasks waiting for the CPU. A circular queue reuses empty spaces at the start of a fixed array, and a priority queue lets urgent items go first.

Dictionaries, trees and graphs

A dictionary (map, hash table) stores key → value pairs, like {"Asha": 9876, "Ravi": 9123}. You find a value by its key, not by position. A hash function turns the key into a box number, so look-up is very fast.

A tree stores data in levels. The top node is the root; nodes below are children; nodes with no children are leaves. In a binary tree each node has at most two children.

In a binary search tree (BST), every value in the left subtree is smaller than the node and every value in the right subtree is bigger. To search, compare and go left or right; each step throws away about half of what is left, so even a million well-balanced items need only about 20 comparisons.

A graph is a set of vertices joined by edges, with no fixed top. Maps, social networks and the internet are graphs.

Choosing a structure: need instant access by position → array; lots of inserting in the middle → linked list; undo or reverse → stack; fair waiting line → queue; look-up by name → dictionary; fast sorted search → BST.

Key formulas and definitions

Worked examples

1. A = [12, 7, 30, 45, 9, 18]. What is A[3], and what is the last index?

Indexes start at 0, so A[3] = 45. There are 6 items, so the last index is 6 − 1 = 5.

2. Start with an empty stack. push(4), push(9), push(2), pop(), push(7), pop(), pop(). What is left, and what was popped in order?

After pushes: [4, 9, 2]. pop → 2. push 7 → [4, 9, 7]. pop → 7. pop → 9. Left: [4]. Popped in order: 2, 7, 9.

3. Empty queue. enqueue(4), enqueue(9), enqueue(2), dequeue(), enqueue(7), dequeue(). What is left?

[4, 9, 2] → dequeue removes 4 → [9, 2] → enqueue 7 → [9, 2, 7] → dequeue removes 9 → [2, 7].

4. Evaluate the RPN expression 5 3 + 2 × using a stack.

push 5, push 3 → '+' pops 3 and 5, pushes 8 → push 2 → '×' pops 2 and 8, pushes 16. Answer: 16 (same as (5 + 3) × 2).

5. Insert 35 into the BST with root 50, left child 30 (children 20, 40) and right child 70. Where does it go?

35 < 50 → go left to 30. 35 > 30 → go right to 40. 35 < 40 → it becomes the left child of 40.

6. An integer array starts at memory address 1000 and each integer takes 4 bytes. Where is A[5]?

Address = 1000 + 5 × 4 = 1020.

Common mistakes

Practice quiz

1. Which structure is LIFO?
2. In an array of 10 items, the last index is:
3. Which structure stores key–value pairs?
4. In a binary search tree, values smaller than a node go:
5. A printer handling jobs in the order they arrive uses a:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What are the main types of data structures?

Linear: arrays, lists, linked lists, stacks, queues. Non-linear: trees and graphs. Also records and dictionaries (hash tables).

What is the difference between a stack and a queue?

A stack is LIFO (last in, first out) and uses one end. A queue is FIFO (first in, first out): add at the rear, remove from the front.

What is the difference between an array and a linked list?

An array stores items side by side with instant access by index but slow middle inserts. A linked list chains nodes with pointers: easy inserts, but slow access to a given position.

Where this is taught

NetherlandsHAVO 4 (bovenbouw, 2e fase)Foundations
NetherlandsHAVO 4 (bovenbouw, 2e fase)Information
NetherlandsVWO 4 (bovenbouw, 2e fase)Foundations
NetherlandsVWO 5Information
PolandLiceum ogólnokształcące, klasa IIIDesigning and programming algorithms (I + II)
RomaniaClasa a IX-aConceptual organisation of data
RomaniaClasa a IX-aConceptual organisation of data
RomaniaClasa a IX-aConceptual organisation of data
RomaniaClasa a XI-aData structures
England (GCSE, A level)Year 124.2 Fundamentals of data structures (part 1)
England (GCSE, A level)Year 134.2 Fundamentals of data structures (A-level)
England (GCSE, A level)Year 134.3 Fundamentals of algorithms
Japan高校(専門学科)1〜3年Programming for Information Systems
South Korea중학교 2학년Algorithms and programming
South Korea고등학교 2학년Algorithms and programming
FrancePremièreData representation
FranceTerminaleData structures
China高二Sel.1 Data and data structures

Learn first

Learn next

Related lessons

All Computer Science lessons