A data structure is a way of storing data so that a program can use it easily and quickly. You already use data structures in daily life. A row of lockers, a pile of plates and a line at a ticket counter are all ways of keeping things in order.
This page explains data structures in simple words. You will see the four most common ones with real-life examples and short code, and learn why the choice matters.
Why data structures matter
Imagine 1,000 books thrown in a heap on the floor. To find one book, you check them one by one. Now imagine the same books on shelves, sorted by subject and title. You walk to the right shelf and pick it up.
The books are the same. Only the way they are stored changed, and finding one became much faster. That is what a data structure does for a program.
| Without a good structure | With a good structure |
|---|---|
| Search checks every item | Search jumps close to the answer |
| Adding an item may shift many others | Adding is quick |
| Code is messy and slow on big data | Code is clean and stays fast as data grows |
Every app you use depends on this. Your phone's contact list, the undo button in Word and the order queue in a food delivery app all use data structures inside.
Types of data structures
Data structures fall into two groups.
| Type | How items are arranged | Examples |
|---|---|---|
| Linear | One after another, in a sequence | Array, linked list, stack, queue |
| Non-linear | An item can link to many items | Tree, graph |
Hash tables (also called hash maps or dictionaries) store key and value pairs and find a value by its key very quickly. They are often taught as their own group.
Beginners should master the four linear ones first. Let us take them one at a time.
1. Array: a row of numbered lockers
Think of a row of lockers in a school corridor, numbered 0, 1, 2, 3 and so on. Each locker holds one item. If you know the number, you walk straight to it.
That is an array. Items sit side by side in memory, and each has an index. Reading any item by its index is very fast, no matter how long the array is.
marks = [78, 85, 62, 90]
print(marks[2]) # read the item at index 2
marks[2] = 70 # update it
print(marks)
print(len(marks))
Output:
62
[78, 85, 70, 90]
4
In Python, a list works like an array that can grow. In Java and C, a plain array has a fixed size that you choose when you create it.
Good at: reading any item by position. Weak at: inserting in the middle, because every item after that point must shift right.
Read more in what is an array.
2. Stack: a pile of plates
In a wedding buffet, clean plates are stacked one on top of another. You put a new plate on top. You take a plate from the top. You never pull one from the bottom.
That is a stack. It follows LIFO: Last In, First Out. The two main operations are push (add on top) and pop (remove from top).
stack = []
stack.append("Page 1") # push
stack.append("Page 2")
stack.append("Page 3")
print(stack.pop()) # pop removes the last item added
print(stack)
Output:
Page 3
['Page 1', 'Page 2']
Where you see it: the browser Back button, the undo button in editors, and the function call stack that tracks which function is running.
3. Queue: a line at the ticket counter
At a railway ticket counter, the first person in the line gets served first. New people join at the back.
That is a queue. It follows FIFO: First In, First Out. You enqueue at the back and dequeue from the front.
from collections import deque
queue = deque()
queue.append("Asha") # enqueue
queue.append("Vikram")
queue.append("Lakshmi")
print(queue.popleft()) # dequeue serves the first person
print(list(queue))
Output:
Asha
['Vikram', 'Lakshmi']
We use deque because removing from the front of a normal Python list is slow on big lists. deque removes from either end quickly.
Where you see it: printer jobs, customer support tickets, and messages waiting to be processed by a server. See stack vs queue for a side-by-side comparison.
4. Linked list: a train of coaches
A train is a chain of coaches. Each coach is joined to the next one. To add a coach in the middle, you unhook two coaches and hook the new one between them. Nothing else moves.
That is a linked list. Each item, called a node, stores its value and a link to the next node. Nodes can sit anywhere in memory.
class Node:
def __init__(self, value):
self.value = value
self.next = None
engine = Node("Engine")
coach1 = Node("S1")
coach2 = Node("S2")
engine.next = coach1
coach1.next = coach2
current = engine
while current is not None:
print(current.value, end=" -> ")
current = current.next
print("end")
Output:
Engine -> S1 -> S2 -> end
Good at: inserting or removing a node once you are at the right place. Weak at: reaching item number 500, because you must walk from the start, coach by coach.
The array vs linked list page compares the two in detail.
The four compared
| Structure | Real-life example | Rule | Fast at | Slow at |
|---|---|---|---|---|
| Array | Numbered lockers | Access by index | Reading by position | Inserting in the middle |
| Stack | Pile of plates | LIFO | Push and pop at the top | Reaching the bottom item |
| Queue | Ticket counter line | FIFO | Add at back, remove at front | Reaching the middle |
| Linked list | Train coaches | Each node points to the next | Insert or delete at a known node | Reaching item number n |
There is no "best" data structure. Each one is fast at some jobs and slow at others. Choosing well means asking: what will my program do most often?
Non-linear data structures in one minute
- Tree: like a family tree or a folder structure on your computer. One root, with children below it. Used for file systems and fast searching.
- Graph: like a city map, where places are points and roads connect them. Used for maps, social networks and route finding.
- Hash table: like the index at the back of a book. You look up a word and jump straight to the page. A Python dictionary and a Java HashMap are hash tables.
Data structures and algorithms go together
An algorithm is a set of steps to solve a problem, such as searching or sorting. Algorithms work on data structures. Binary search, for example, works well on a sorted array but not on a linked list, because it needs to jump to the middle item directly.
That is why courses and interviews treat them as one subject, DSA. The data structures and algorithms guide gives a full learning path, and what is an algorithm explains the other half.
How to start learning data structures
- Get comfortable with arrays and strings in one language.
- Learn stack and queue, and build each one yourself using an array.
- Build a linked list from scratch with insert, delete and print.
- Move to trees, hash tables and graphs.
- Solve problems on each structure, starting easy, such as finding the largest element in an array or reversing a linked list.
When you can explain each structure with an everyday example and write its basic operations, check your understanding with the DSA interview questions.
Frequently Asked Questions
What is a data structure in one line?
What are the two main types of data structures?
Which data structure should I learn first?
What is the difference between a data structure and an algorithm?
Do I need data structures for a job?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — See the Java Full Stack course in Hyderabad

