DS
DS
DS
1. Linked Lists
Definition:
A linked list is a linear data structure where elements (nodes) are stored in memory, connected
by pointers.
Traversal is one-directional.
struct Node {
int data;
Node* next;
};
2.
Doubly Linked List:
struct Node {
int data;
Node* prev;
Node* next;
};
3.
Circular Linked List:
Common Operations:
1. Insertion
:
At the beginning
At the end
At a specific position
2. Deletion
:
From the beginning
From the end
A specific node
3. Traversal
:
Iterate through the list to access elements.
Advantages:
Dynamic memory allocation (no fixed size).
Efficient insertion and deletion (compared to arrays).
Disadvantages:
Extra memory required for pointers.
Sequential access only (no random access).
2. Stack
Definition:
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. The last
element added is the first to be removed.
Basic Operations:
1. Push: Add an element to the top of the stack.
2. Pop: Remove the top element from the stack.
3. Peek/Top: View the top element without removing it.
4. IsEmpty: Check if the stack is empty.
Implementation:
1. Using Arrays: #define MAX 100
int stack[MAX], top = -1;
void push(int x) {
if (top == MAX - 1) return; // Overflow
stack[++top] = x;
}
int pop() {
if (top == -1) return -1; // Underflow
return stack[top--];
}
Applications:
Function call management in recursion.
Undo operations in text editors.
Expression evaluation and conversion (Infix to Postfix).
3. Queues
Definition:
A queue is a linear data structure that follows the FIFO (First In, First Out) principle. The first
element added is the first to be removed.
Types of Queues:
1.
Simple Queue:
2.
Circular Queue:
3.
Priority Queue:
4.
Double-Ended Queue (Deque):
Basic Operations:
1. Enqueue: Add an element to the rear.
2. Dequeue: Remove an element from the front.
3. Peek/Front: View the front element.
4. IsEmpty: Check if the queue is empty.
Implementation:
1. Using Arrays: #define MAX 100
int queue[MAX], front = 0, rear = -1;
void enqueue(int x) {
if (rear == MAX - 1) return; // Overflow
queue[++rear] = x;
}
int dequeue() {
if (front > rear) return -1; // Underflow
return queue[front++];
}
struct Queue {
Node* front;
Node* rear;
};
int dequeue(Queue& q) {
if (!q.front) return -1;
int val = q.front->data;
Node* temp = q.front;
q.front = q.front->next;
if (!q.front) q.rear = nullptr;
delete temp;
return val;
}
Definition:
Operations:
1.
Insertion:
struct Node {
int data;
Node* left;
Node* right;
};
2.
Search:
3.
Deletion:
4.
Traversal:
5. Hashing
Definition:
Hashing is a technique to map data to a fixed-size table (hash table) using a hash function.
Hash Function:
Collision Handling:
1.
Chaining:
Use a linked list at each index to handle multiple elements mapping to the same index.
2.
Open Addressing:
Applications:
Symbol tables in compilers.
Caching (e.g., LRU Cache).
Databases and file systems.
Quick Tips for Preparation:
1. Focus on understanding basic operations (insert, delete, search) for all data structures.
2. Write down the differences between stacks, queues, and linked lists.
3. Practice small code snippets for traversal and insertion.
4. Revise hash table collision resolution techniques.
5. Don't forget the tree traversals (Inorder, Preorder, Postorder).