
Lecture 10: Data Structures
In this lecture, we will explore data structures, the fundamental building blocks of computer science. A data structure is a way of organizing and storing data so that it can be accessed and modified efficiently. Choosing the right data structure is crucial for solving problems effectively and optimizing both time and memory.
1. Introduction to Data Structures
Data structures define how data is arranged in memory and how operations such as insertion, deletion, searching, and traversal are performed. They are categorized into two broad types:
- Primitive Data Structures: Basic data types like integers, floats, characters, and booleans.
- Non-Primitive Data Structures: Complex structures like arrays, linked lists, stacks, queues, trees, and graphs.
2. Arrays
An array is a collection of elements of the same type stored at contiguous memory locations.
- Advantages: Fast access using indices.
- Disadvantages: Fixed size, costly insertion/deletion in the middle.
int numbers[5] = {1, 2, 3, 4, 5};
3. Linked Lists
A linked list is a sequence of nodes where each node contains data and a pointer to the next node.
- Singly Linked List: Each node points to the next node only.
- Doubly Linked List: Each node points to both previous and next nodes.
- Circular Linked List: Last node points back to the first node.
Advantage: Dynamic memory allocation.
Disadvantage: Sequential access is slower than arrays.
4. Stacks
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle.
- Push: Insert an element at the top.
- Pop: Remove the top element.
- Peek: View the top element without removing it.
Applications: Expression evaluation, undo operations, function call management.
5. Queues
A queue is a linear data structure that follows the FIFO (First In, First Out) principle.
- Enqueue: Insert at the rear.
- Dequeue: Remove from the front.
Types: Simple Queue, Circular Queue, Priority Queue, Double-Ended Queue (Deque).
6. Trees
A tree is a hierarchical data structure consisting of nodes connected by edges. The top node is called the root.
- Binary Tree: Each node has at most two children (left and right).
- Binary Search Tree (BST): Left child is smaller, right child is larger.
- Balanced Trees: AVL, Red-Black Trees ensure height balance.
- Heaps: Complete binary trees used in priority queues.
Applications: Searching, sorting, compilers, databases.
7. Graphs
A graph is a collection of nodes (vertices) and edges connecting them. It can be directed or undirected, weighted or unweighted.
- Adjacency Matrix: 2D array representation.
- Adjacency List: Linked list representation.
Applications: Social networks, routing, recommendation systems, web crawlers.
8. Hashing
Hashing maps data to a fixed-size table using a hash function. It provides fast search, insert, and delete operations.
Applications: Databases, caching, password storage.
9. Comparison of Data Structures
| Data Structure | Access Time | Insertion/Deletion | Applications |
|---|---|---|---|
| Array | O(1) | O(n) | Static data storage |
| Linked List | O(n) | O(1) (if pointer known) | Dynamic memory |
| Stack | O(n) | O(1) | Undo, recursion |
| Queue | O(n) | O(1) | Scheduling |
| Tree | O(log n) | O(log n) | Searching, indexing |
| Graph | Depends on algorithm | Depends on structure | Networks, routing |
| Hash Table | O(1) (average) | O(1) (average) | Databases, caches |
10. Summary
- Data structures define how data is stored and accessed.
- Linear structures: arrays, linked lists, stacks, queues.
- Non-linear structures: trees, graphs.
- Hashing enables efficient data retrieval.
- Choosing the right data structure improves performance.
Next Lecture (11): Operating Systems