What Are Data Structures?
Explore data structures in computer science: arrays, linked lists, stacks, queues, hash tables, and trees. Learn how each organises data, when to use it, with examples and a quiz.
Key takeaways
- A data structure is a way of organising data so it can be used efficiently
- Arrays give fast access by index; linked lists make inserting and removing easy
- Stacks are last-in-first-out; queues are first-in-first-out
- Hash tables give near-instant lookup by key; trees organise data in a branching hierarchy
Organising data well
Programs are mostly about data โ names, scores, messages, pixels โ and what you do with it. A data structure is simply a way of organising that data so it can be used efficiently. Choosing the right one can make a program run in a blink instead of grinding for minutes. Choosing the wrong one can make even a fast computer feel slow.
Think of it like organising a kitchen. Spices in a labelled rack, pots stacked in a cupboard, cutlery sorted in a drawer โ same items, but the organisation decides how quickly you can cook. This lesson tours the most important data structures and, crucially, when to reach for each. Their power becomes obvious once you've seen sorting and searching algorithms, because the structure you pick decides how fast those algorithms can run.
Arrays: the numbered row
An array stores items in a continuous row in memory, each with a numbered position called an index (usually starting at 0).
index: 0 1 2 3
value: [ 10 ][ 25 ][ 7 ][ 42 ]
The killer feature of an array is random access: because the items sit in a neat row, the computer can jump straight to position 2 in a single step, no matter how big the array is. That makes reading by index extremely fast โ O(1).
The trade-off is flexibility. Inserting a new item in the middle means shifting every item after it to make room, which is slow. And classic arrays have a fixed size set when they're created. You met arrays already in Lists and Arrays; here we focus on why their layout gives those strengths and weaknesses.
Linked lists: chained boxes
A linked list solves the array's insertion problem differently. Instead of one solid row, it's a chain of separate nodes, where each node holds a value and a pointer to the next node.
[ 10 | โ ] -> [ 25 | โ ] -> [ 7 | โ ] -> [ 42 | null ]
Because the nodes are linked by pointers, inserting or removing an item is easy: you just rewire a couple of pointers, no shifting required. That makes inserts and deletes fast.
The trade-off is the opposite of an array's. There's no random access: to reach the 100th item you must follow the chain from the start, one node at a time โ O(n). So linked lists shine when you insert and remove a lot but rarely need to jump to an arbitrary position.
Stacks: last in, first out
A stack is a data structure with a strict rule: you can only add or remove items at the top. Adding is called push; removing is called pop. The last item you push on is the first one you pop off โ this is LIFO (Last In, First Out).
Picture a stack of plates: you add to the top and take from the top. You can't easily pull a plate from the bottom.
Stacks are everywhere in computing:
- โฌ ๏ธ The Undo button in an editor pushes each action onto a stack and pops the most recent one to undo it.
- ๐ The browser's Back button uses a stack of the pages you visited.
- ๐งฎ The computer uses a "call stack" to remember which function called which, so it knows where to return when a function finishes.
Queues: first in, first out
A queue is the opposite ordering. You add at the back (enqueue) and remove from the front (dequeue), so the first item in is the first item out โ FIFO (First In, First Out).
This models a real-world line of people: whoever arrives first gets served first. Queues are used for:
- ๐จ๏ธ A printer's job queue โ documents print in the order they were sent.
- ๐ซ Handling requests on a server, fairly, in arrival order.
- ๐ฎ Buffering keystrokes or events so none get lost when the computer is busy.
Stacks and queues are deliberately restricted โ that's their strength. By limiting how you add and remove, they make certain behaviours simple and predictable.
Hash tables: lookup by key
What if you want to look something up by a label rather than a number? A phone book maps names to numbers; you don't care that "Jordan" is item 4,012 โ you just want Jordan's number. A hash table (also called a dictionary or map) does exactly this.
It works using a hash function: a calculation that turns your key (like "Jordan") into a number, which tells the computer roughly where to store the value. Later, the same key hashes to the same spot, so lookup is near-instant โ O(1) on average.
"Jordan" --hash--> slot 3 --> stores "555-0192"
"Maria" --hash--> slot 7 --> stores "555-0455"
Hash tables power dictionaries in Python, objects in JavaScript, database indexes, and caches. The trade-off: items aren't kept in any sorted order, and occasionally two keys hash to the same slot (a "collision"), which the structure must handle carefully. You've seen this idea in action with dictionaries in Python โ under the hood, that's a hash table.
Trees: branching hierarchies
A tree organises data in a branching hierarchy, like a family tree or a folder structure. It starts at a single root node, and each node can have child nodes below it. Nodes with no children are called leaves.
(root)
/ \
(child) (child)
/ \ \
(leaf) (leaf) (leaf)
Trees are perfect for data that's naturally hierarchical โ the files and folders on your computer, the tags nested inside a web page, or a tournament bracket. A special kind called a binary search tree keeps values in sorted order so you can search, insert, and delete in about O(log n) steps, combining fast lookup and easy updates. Even the AI behind some game opponents explores possible moves using a tree of future positions.
It's all about trade-offs
Here's the big lesson. There's no single "best" data structure โ each one trades strengths in one operation for weaknesses in another:
| Structure | Great at | Weak at |
|---|---|---|
| Array | Fast access by index | Inserting in the middle; fixed size |
| Linked list | Fast insert/remove | No random access |
| Stack | LIFO add/remove | Only reach the top |
| Queue | FIFO add/remove | Only reach the ends |
| Hash table | Near-instant lookup by key | No sorted order; collisions |
| Tree | Hierarchy; sorted searching | More complex to implement |
Skilled programmers don't memorise these โ they reason about them. They ask: "Which operations will this program do most often?" and pick the structure whose strengths match. That single decision often matters more than any clever trick in the code itself.
Try it yourself
Make these structures concrete:
- ๐ Build a stack with a deck of cards: push cards onto a pile, then pop them off and watch them come back in reverse order. Then form a queue of friends and serve them in arrival order to feel the FIFO rule.
- ๐ Create a tiny hash table on paper: invent a simple rule (e.g. "use the first letter of the name") to decide which of several boxes a name goes in, then test how quickly you can find a name later.
- ๐ณ Draw your computer's folders as a tree, with the root at the top and files as leaves. Notice how the branching structure makes any file reachable by following one path.
- ๐ป In Python, implement a stack and a queue using a list (
append/popfor the stack;append/pop(0)for the queue), then write a short program that uses one to reverse a word.
Once you can match a problem to the right data structure, you've gained one of the most valuable instincts in all of programming.
Quick quiz
Test yourself and earn XP
What is a data structure?
A data structure is a method of organising and storing data so that operations on it are efficient.
What is the main advantage of an array?
Arrays store items in a row in memory, so you can jump straight to any item by its index in one step.
How does a stack work?
A stack is LIFO: the last item pushed on is the first one popped off, like a stack of plates.
What does a queue model?
A queue is FIFO: first in, first out, just like a line of people waiting their turn.
What is a hash table good for?
A hash table uses a key to compute where a value is stored, giving near-instant lookup on average.
FAQ
No, but they work as a team. A data structure is how you organise the data; an algorithm is the sequence of steps that operates on it. The right data structure can make an algorithm dramatically faster, which is why computer scientists study both together.
Ask what operations you'll do most. Need fast lookup by position? An array. Lots of inserting and removing at the ends? A linked list, stack, or queue. Fast lookup by a label or key? A hash table. Hierarchical or sorted data? A tree. Each structure trades strengths in one operation for weaknesses in another.
Keep exploring
More in Coding