Data Structures and Algorithms (DSA) is one of the most important courses in BSCS and BSIT, usually taught in the 3rd semester. This guide explains the main data structures in simple words, with real-life examples, time complexity and when to use each one.
What Is a Data Structure?
A data structure is a way of organising and storing data so that it can be used efficiently. Choosing the right structure makes a program faster and simpler. Data structures are usually divided into:
- Linear: elements are arranged in a sequence (array, linked list, stack, queue)
- Non-linear: elements are arranged in a hierarchy or network (tree, graph)
Big-O Notation in One Minute
Big-O describes how the running time grows as the input size n grows:
- O(1) – constant: same time for any n (accessing
arr[5]) - O(log n) – very slow growth, e.g. binary search
- O(n) – grows with n, e.g. checking every element
- O(n log n) – efficient sorting such as merge sort
- O(n²) – slow for big inputs, e.g. bubble sort
1. Array
Real-life example: numbered lockers in a row. An array stores elements of the same type in continuous memory, so any element can be reached directly by its index.
Access: O(1) | Search: O(n) | Insert/delete in the middle: O(n), because elements must shift.
Use when: the size is known and you need fast access by position.
2. Linked List
Real-life example: a treasure hunt where each clue tells you where the next one is. Each node stores data and a pointer to the next node.
struct Node {
int data;
Node* next;
};
Insert/delete at the head: O(1) | Access/search: O(n).
Use when: the size changes often and you insert or delete frequently. Types: singly, doubly and circular linked lists.
3. Stack (LIFO)
Real-life example: a pile of plates – the last plate placed on top is the first one taken off (Last In, First Out).
Main operations: push (add to top), pop (remove from top), peek (see the top). All are O(1).
Used in: undo in editors, the browser back button, function calls (call stack), checking balanced brackets and evaluating postfix expressions.
4. Queue (FIFO)
Real-life example: a line at the bank – the first person in line is served first (First In, First Out).
Main operations: enqueue (add at rear) and dequeue (remove from front), both O(1). Variations include the circular queue, priority queue and deque.
Used in: printer queues, CPU scheduling and breadth-first search.
5. Tree and Binary Search Tree
Real-life example: a family tree or the folders on your computer. A tree has a root node and child nodes. In a Binary Search Tree (BST), every left child is smaller and every right child is larger than its parent.
Search/insert/delete: O(log n) on average, O(n) in the worst case (when the tree becomes a straight line). Balanced trees such as AVL trees keep it O(log n).
Traversals: in-order (left, root, right – gives sorted output for a BST), pre-order and post-order.
6. Heap
A complete binary tree where each parent is larger (max-heap) or smaller (min-heap) than its children. Getting the max/min is O(1) and insert/delete is O(log n). Used in: priority queues and heap sort.
7. Hash Table
Real-life example: a dictionary where you jump straight to a word. A hash function converts a key into an index. Search, insert and delete are O(1) on average. Collisions are handled with chaining or open addressing.
8. Graph
Real-life example: a road map – cities are vertices and roads are edges. Graphs can be directed or undirected, weighted or unweighted, and are stored as an adjacency matrix or adjacency list.
Key algorithms: BFS (uses a queue), DFS (uses a stack or recursion) and Dijkstra's shortest path.
Quick Comparison Table
| Structure | Access | Search | Insert | Best for |
|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | Fast index access |
| Linked list | O(n) | O(n) | O(1) at head | Frequent insert/delete |
| Stack / Queue | Top/front only | O(n) | O(1) | LIFO / FIFO order |
| BST (balanced) | O(log n) | O(log n) | O(log n) | Sorted data |
| Hash table | – | O(1) avg | O(1) avg | Fast lookup by key |
Exam Tips for DSA
- Practise drawing diagrams: push/pop on a stack, inserting into a BST, BFS/DFS order on a graph.
- Learn the time complexity of each operation – it is asked in almost every paper.
- Dry-run code by hand with a small example before writing it.
- Use VisuAlgo to watch the algorithms animate step by step.
New to programming? Start with our C++ guides and see more free study resources.
FAQ
What is the difference between a stack and a queue?
A stack removes the most recently added item first (LIFO); a queue removes the oldest item first (FIFO).
Which data structure is fastest for searching?
A hash table gives O(1) average search. For sorted data, a balanced BST or binary search on a sorted array gives O(log n).
0 comments:
Post a Comment