Data Structure and AlgorithmsUnit 19 min read
Data Structures: Definitions, Types, Operations & Real-World Use
Unit 1 of Data Structure and Algorithms introduces the core concepts of data structures—what they are, why they matter, their classifications, and fundamental operations like insertion, deletion, and traversal—with visual examples from Nepalese apps (eSewa, Daraz) and everyday life.
What is a Data Structure?
A data structure is a way of organizing, storing, and accessing data efficiently in a computer program. It defines how data is laid out in memory and how operations (like searching or sorting) are performed on it.
Why Do We Need Data Structures?
- Efficiency: Different structures optimize for time/space (e.g., arrays for fast access, trees for hierarchical data).
- Abstraction: Hide implementation details (e.g., a queue abstracts FIFO order).
- Problem-Solving: Match the structure to the problem (e.g., graphs for routes, stacks for undo operations).
Types of Data Structures
Data structures are classified into two broad categories:
1. Primitive (Basic) Data Structures
Stores single values (no sub-structures). Examples:
- Integer: Stores whole numbers (e.g.,
int age = 25;). - Float/Double: Stores decimals (e.g.,
float price = 12.99;). - Character: Stores single letters/symbols (e.g.,
char grade = 'A';).
2. Non-Primitive (Compound) Data Structures
Combine multiple data items. Divided into:
A. Linear Data Structures
Elements are arranged sequentially. Examples:
- Array: Fixed-size, contiguous memory.
- Linked List: Dynamic-size, nodes with pointers.
- Stack: LIFO (Last-In-First-Out) order.
- Queue: FIFO (First-In-First-Out) order.
B. Non-Linear Data Structures
Elements are not sequential. Examples:
- Tree: Hierarchical (e.g., file systems, organization charts).
- Graph: Nodes connected by edges (e.g., social networks, maps).
- Hash Table: Key-value pairs for O(1) access.
Key Operations on Data Structures
All data structures support these core operations (time complexity varies by structure):
| Operation | Description | Example (Array) |
|---|---|---|
| Insertion | Add an element. | arr[2] = 100; |
| Deletion | Remove an element. | delete arr[1]; |
| Traversal | Visit all elements. | for (int x : arr) { ... } |
| Search | Find an element. | indexOf(5) |
| Sorting | Arrange elements in order. | sort(arr); |
Visualizing Data Structures
1. Array
An array is a contiguous block of memory storing elements of the same type.
Index: 0 1 2 3 4
Value: 10 20 30 40 50
Operations:
- Insertion at end: Append to
arr[n](O(1) if space exists). - Deletion from start: Shift all elements left (O(n)).
Example: Storing Daraz order IDs in a fixed-size array:
Index: 0 1 2 3
OrderID: 1001 1002 1003 1004
2. Linked List
A dynamic structure where each node points to the next.
Node 1 (Data: 10) → Node 2 (Data: 20) → Node 3 (Data: 30) → NULL
Operations:
- Insertion at head: Update
headpointer (O(1)). - Deletion at tail: Traverse to last node (O(n)).
Example: Khalti transaction history (newest first):
Node 1 (Txn: 5000) → Node 2 (Txn: 3000) → Node 3 (Txn: 2000) → NULL
Comparison Table: Array vs. Linked List
| Feature | Array | Linked List |
|---|---|---|
| Memory | Contiguous | Non-contiguous (dynamic) |
| Insertion | O(n) (shift elements) | O(1) at head |
| Deletion | O(n) (shift elements) | O(n) (traverse to node) |
| Access | O(1) (random access) | O(n) (sequential access) |
| Use Case | Fixed-size data (e.g., marks) | Dynamic data (e.g., playlist) |
In the Real World
eSewa (Nepal)
- Data Structure Used: Queue
- How? When you book an appointment, your request is added to a FIFO queue. The system processes older requests first (e.g., first-come-first-served for bill payments).
- Visual:
[Request 1 (User A)] → [Request 2 (User B)] → [Request 3 (User C)] → NULL
Daraz (Nepal)
- Data Structure Used: Array + Hash Table
- How? Product IDs are stored in an array for quick access, while user reviews use a hash table (key = product ID, value = list of reviews) for O(1) lookup.
- Example: Searching for a product by ID (
productID = 1005) uses the hash table to fetch details instantly.
NTC Traffic Routes (Nepal)
- Data Structure Used: Graph
- How? Roads are nodes, and connections are edges. The NTC uses Dijkstra’s algorithm (a graph algorithm) to find the fastest route between two cities, avoiding traffic jams.
- Visual:
```
Kathmandu --(60km)--> Bhaktapur --(40km)--> Dhulikhel
| |
30km 50km
| |
Dharan --(120km)--> Pokhara
```
Worked Example: Storing NEPSE Stock Prices
Problem: Store daily closing prices of NEPSE shares for the last 7 days. Which data structure is better: array or linked list?
Solution:
Array Choice:
- Fixed size (7 days), random access needed (e.g., "What was the price on Day 3?").
- Implementation:
float prices[7] = {1200.5, 1202.3, 1198.7, 1210.1, 1205.0, 1199.8, 1203.2}; - Access Day 3:
printf("Day 3 price: %.2f", prices[2]); // Output: 1198.70
Linked List Choice:
- If prices arrive dynamically (e.g., real-time updates), a linked list allows easy insertion at the end (newest price).
- Implementation:
struct Node { float price; struct Node* next; }; - Insertion:
```
Before: NULL ← [1200.5] ← [1202.3] ← NULL
After: NULL ← [1200.5] ← [1202.3] ← [1198.7] ← NULL
```
Trace for Array Insertion (Day 8):
| Step | Action | Array State (prices) |
|---|---|---|
| 1 | Check if full | [1200.5, 1202.3, ..., 1203.2] (7/7) |
| 2 | Resize (if needed) | Allocate new array of size 14. |
| 3 | Copy old data | [1200.5, 1202.3, ..., 1203.2, 0, 0, 0, 0] |
| 4 | Insert new price | [1200.5, ..., 1203.2, 1207.8, 0, ...] |
Advantages and Disadvantages
| Data Structure | Advantages | Disadvantages |
|---|---|---|
| Array | Fast access, cache-friendly | Fixed size, slow insertions/deletions |
| Linked List | Dynamic size, easy insertions/deletions | Slow access, extra memory for pointers |
| Stack | LIFO order, fast operations | Limited to one end |
| Queue | FIFO order, fair processing | Same as linked list for dynamic data |
Exam Tip
- Definitions Matter: Always define data structures clearly (e.g., "A stack is a LIFO structure with push/pop operations").
- Visuals = Marks: Draw diagrams for:
- Array/linked list operations (insertion/deletion).
- Stack/queue states after each operation.
- Graphs for real-world examples (e.g., NTC routes).
- Time Complexity: Memorize O(1), O(n), O(n²) for operations (e.g., array access is O(1), linked list search is O(n)).
- Real-World Links: Examiners love connections to apps (e.g., "How would you implement a browser’s back button using a stack?").
- Pseudocode: Write short traces (like the NEPSE example) to show understanding of step-by-step execution.
Key Formula to Remember:
For an array of size n:
- Access time:
- Insertion at end: (if space exists), else (for resizing).
- Deletion at start: (shifting elements).
Based on the PU BE Computer (PU) syllabus for Data Structure and Algorithms (CMP160), unit 1.
Discussion
Loading…