
A simple guide to arrays, linked lists, stacks and queues: what they are, how they differ, when to use each, how they look in code, and practice problems to try.
Arrays, linked lists, stacks and queues appear in almost every programming course and in many fresher interviews. They are the basic ways to store and organise data. Once you understand how each one stores and returns data, choosing between them becomes much easier, and harder topics like trees and graphs start to make sense too. This guide explains each one in simple words, with everyday pictures in mind.
Why data structures matter
A program is only as good as the way it keeps its data. If you need to find a student's marks quickly, undo the last action, or serve customers in order, the structure you choose decides how fast and how simple your solution is. Learning data structures is really learning to ask: "How will I use this data, and which structure makes that easy?"
Arrays
An array stores items one after another in memory, each with a position number called an index, usually starting at 0. Imagine a row of numbered lockers: if you know the locker number, you can open it straight away without checking the others.
- Good at: reading or changing an item by its index, which is very fast.
- Not so good at: inserting or deleting in the middle, because the items after it must shift.
- Fixed size: in many languages the size is set when the array is created. Dynamic arrays, like Python lists or Java ArrayList, grow automatically behind the scenes.
- Use it for: lists of marks, daily temperatures, pixels in an image, or anything accessed by position.
Linked lists
A linked list is a chain of small pieces called nodes. Each node holds a value and a pointer, which is a reference to the next node. The nodes can sit anywhere in memory; the pointers connect them. Think of a treasure hunt where each clue tells you where to find the next one.
- Good at: adding or removing a node once you are at the right place, because you only change a pointer or two.
- Not so good at: reaching the 50th item, since you must walk through the chain from the start.
- Types: singly linked (one direction), doubly linked (forward and backward) and circular lists.
- Use it for: lists that change often, or when you do not know the size in advance.
Stacks
A stack follows the rule "last in, first out", usually written LIFO. It is like a pile of plates: you add a plate on the top and take a plate from the top, so the last plate you placed is the first one to come off.
- Operations: push (add to the top), pop (remove from the top), peek (look at the top without removing) and checking if it is empty.
- Use it for: the undo feature in an editor, the browser back button, checking whether brackets in an expression are balanced, and the way programs keep track of function calls.
Queues
A queue follows the rule "first in, first out", written FIFO. It is like people standing in line at a ticket counter: the first person to join is the first to be served.
- Operations: enqueue (add at the back), dequeue (remove from the front), peek and checking if it is empty.
- Variants: circular queues, double-ended queues (deque) and priority queues, where the most important item leaves first.
- Use it for: print jobs, customer support tickets, task scheduling and breadth-first search.
A tiny example in code
Here is how a stack and a queue look in Python, using a list and the collections module:
stack = []
stack.append(1) # push
stack.append(2)
print(stack.pop()) # 2, the last one added
from collections import deque
queue = deque()
queue.append(1) # enqueue
queue.append(2)
print(queue.popleft()) # 1, the first one addedQuick comparison
| Structure | Access | Insert or remove | Order rule |
|---|---|---|---|
| Array | Fast by index | Slow in the middle | By position |
| Linked list | Slow, walk the chain | Fast when you hold the node | By links |
| Stack | Top only | Top only | Last in, first out |
| Queue | Front only | Back in, front out | First in, first out |
How to choose
- Need fast access by position? Use an array.
- Need to insert and delete often? Consider a linked list.
- Need to reverse order or undo? Use a stack.
- Need to process in arrival order? Use a queue.
Common mistakes beginners make
- Going past the end of an array, which causes an index error.
- Losing the rest of a linked list by changing a pointer in the wrong order.
- Popping from an empty stack or queue without checking.
- Memorising code instead of drawing the structure on paper first.
Practice problems
- Reverse an array in place, and reverse a linked list.
- Check if a string of brackets such as {[()]} is balanced, using a stack.
- Find the middle node of a linked list.
- Detect a cycle in a linked list.
- Build a queue using two stacks.
- Simulate a ticket counter with a queue.
Draw each problem on paper first. If you can show the steps with boxes and arrows, writing the code becomes much easier.
Ready for the next step?
Go deeper with our Data Structures and Algorithms program. If you would like guidance from people who work in the industry, join our free career guidance workshop or request a free career discovery session. We will look at your background and help you plan what to learn and build next.
LEARN THIS WITH US
Need career guidance from industry experts?
Join our free workshop or request a free career discovery session.



