Introduction to Data Structures
Data Structures: A Simple Guide
1. **Arrays**:
- An array is a collection of elements stored in a specific order.
- Best used when you know the size of the data beforehand.
- Example in C++:
int marks[5] = {80, 90, 85, 95, 75};
2. **Stacks**:
- A stack is like a pile of plates: Last In, First Out (LIFO).
- Useful in scenarios like undo operations.
- Example in C++:
stack<int> s;
s.push(10);
s.push(20);
s.pop(); // Removes 20
3. **Queues**:
- A queue is like a line at a coffee shop: First In, First Out (FIFO).
- Best for tasks or requests processed in order.
- Example in C++:
queue<int> q;
q.push(10);
q.push(20);
q.pop(); // Removes 10
4. **Linked Lists**:
- A sequence of elements where each points to the next.
- Good for dynamic-sized data where frequent insertions/removals are needed.
- Example in C++:
struct Node {
int data;
Node* next;
};
5. **Trees**:
- A tree structure with a root and branches (child nodes).
- Used in hierarchical data, e.g., file systems.
- Example in C++: A binary tree has left and right children.
6. **Hash Tables**:
- Stores data in key-value pairs.
- Very fast for lookups using a unique key.
- Example in C++:
unordered_map<string, int> age;
age["John"] = 25;
age["Mary"] = 30;