Every topic starts at New. Sign in and your practice counts.
A data structure is a specialized format for organizing, processing, retrieving, and storing data in a computer so that it can be used efficiently. Choosing the right data structure is critical for writing efficient algorithms.
Data structures are broadly classified as follows:
Data Structures
├── Primitive
│ └── int, float, char, boolean
└── Non-Primitive
├── Linear
│ ├── Static → Arrays
│ └── Dynamic → Linked Lists, Stacks, Queues
└── Non-Linear
└── Trees, Graphs
These are the basic data types built into a programming language. They hold a single value and are the building blocks for all other structures.
| Type | Example values |
|---|---|
'A', 'z' | |
True, |
These are derived from primitive types and can store multiple values or complex relationships.
Elements are arranged in a sequential order, each element has a unique predecessor and successor (except the first and last).
| Structure | Type | Description |
|---|---|---|
| Array | Static | Fixed-size, indexed collection of same-type elements |
| Linked List | Dynamic | Nodes connected by pointers; size changes at run-time |
| Stack | Dynamic | LIFO (Last In, First Out), push/pop operations |
| Queue | Dynamic | FIFO (First In, First Out), enqueue/dequeue operations |
Elements are not arranged sequentially. One element can connect to multiple others.
| Structure | Description |
|---|---|
| Tree | Hierarchical structure with a root node and child nodes |
| Graph | Network of nodes (vertices) connected by edges |
| Feature | Static (e.g., Array) | Dynamic (e.g., Linked List) |
|---|---|---|
| Size | Fixed at compile-time | Changes at run-time |
| Memory | Stack memory | Heap memory |
| Flexibility | Low | High |
| Access speed | Fast (direct index) | Slower (traversal needed) |
An Abstract Data Type (ADT) is a mathematical model for a data type defined by its behavior (operations) from the user's perspective, independent of any specific implementation.
An ADT answers "what" operations are supported, not "how" they are implemented.
Example, Stack ADT:
push(item), add item to toppop(), remove item from toppeek(), view top item without removingisEmpty(), check if stack is emptyThe Stack ADT can be implemented using an array or a linked list, the ADT definition remains the same.
Python provides built-in data structures that map to the concepts above:
| Python Structure | Category | Key Operations |
|---|---|---|
list | Linear, Dynamic | append(), remove(), insert(), indexing |
tuple | Linear, Static | Immutable sequence, indexing |
dict | Non-linear (hash map) | Key-value pairs, get(), update() |
set | Non-linear | Unique elements, union, intersection |
# Creating and manipulating a list
numbers = [10, 20, 30, 40, 50]
# Access by index
print(numbers[0]) # Output: 10
# Append (dynamic growth)
numbers.append(60)
print(numbers) # [10, 20, 30, 40, 50, 60]
# Insert at position
numbers.insert(2, 25)
print(numbers) # [10, 20, 25, 30, 40, 50, 60]
# Remove element
numbers.remove(25)
print(numbers) # [10, 20, 30, 40, 50, 60]
# Traverse
for num in numbers:
print(num)
stack = []
# Push
stack.append('A')
stack.append('B')
stack.append('C')
print(stack) # ['A', 'B', 'C']
# Pop
stack.pop() # Removes 'C'
print(stack) # ['A', 'B']
from collections import deque
queue = deque()
# Enqueue
queue.append('First')
queue.append('Second')
queue.append('Third')
# Dequeue
queue.popleft() # Removes 'First'
print(queue) # deque(['Second', 'Third'])
The choice of data structure directly affects:
For example, searching for an element in an unsorted array takes time, but a Binary Search Tree (BST) can reduce this to .
False