Data Structures and Algorithms
Data Structures and Algorithms
Name: Ajay Kumar Sharma | Roll Number: 252432001 | Subject: DSA Lab | Branch: Mathematics And Computing
List of Experiments
1. 1-Dimensional Arrays and its various operations like insertion, deletion, searching, sorting etc.
2. 2-Dimensional Arrays and its various operations like inserting or deleting a particular row or column.
3. Finding solutions of a given equation using Gauss Jordan Method.
4. Finding solutions of a given equation using Gauss Elimination Method.
5. Singly Linked List and its various operations like insertion, deletion and reversing.
6. Doubly Linked List and its various operations like insertion, deletion and reversing.
7. Circular Linked List and its various operations like insertion, deletion and reversing.
8. Doubly Circular Linked list and its various operations like insertion, deletion and reversing.
9. Implementation of Stacks and its various operations using arrays (Push, Pop and Peek).
10. Implementation of Stacks and its various operations using linked list (Push, Pop and Peek).
11. Implementation of Queues using Arrays and perform operations like enqueue, dequeue etc.
12. Implementation of Queues using Linked List and perform operations like enqueue, dequeue etc.
13. Implementation of Circular Queues using arrays and perform operations like enqueue, dequeue etc.
14. Implementation of Circular Queues using Linked List and perform operations like enqueue, dequeue etc.
15. Implementation of Binary Trees and perform operations for traversals and to find the height of the tree.
16. Implementation of Binary Search Trees and perform operations for traversals and to find the height of the tree.
17. Implementation of AVL Trees and perform operations of searching, insertion, deletion and to get the height of the
tree.
18. Implementation of Various Sorting Algorithms on Arrays
a) Bubble Sort
b) Optimized Bubble Sort
c) Insertion Sort
d) Selection Sort
e) Quick Sort
f) Merge Sort
EXPERIMENT 1
1.1_D.Array
[ ]: 1.1_D.Array
#include <iostream>
using namespace std;
int main() {
int arr[100], n = 0, choice;
while (true) {
cout << "\n--- ARRAY OPERATIONS MENU ---\n";
cout << "1. Insert element\n";
cout << "2. Delete element\n";
cout << "3. Search element\n";
cout << "4. Sort array\n";
cout << "5. Display array\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
// Insertion
int pos, value;
cout << "Enter position (0 to " << n << "): ";
cin >> pos;
cout << "Enter value: ";
cin >> value;
else if (choice == 2) {
// Deletion
int pos;
cout << "Enter position to delete (0 to " << n - 1 << "): ";
cin >> pos;
else if (choice == 3) {
// Searching (Linear Search)
int key, found = 0;
cout << "Enter element to search: ";
cin >> key;
else if (choice == 4) {
// Sorting (Bubble Sort)
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "Array sorted unsuccessfully.\n";
}
else if (choice == 5) {
// Display
cout << "Array elements: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
}
else if (choice == 6) {
cout << "Exiting program...\n";
break;
}
else {
cout << "Invalid choice! Try again.\n";
}
}
return 0;
}
#1 Output
3. Search element
4. Sort array
5. Display array
6. Exit
Enter your choice: 3
Enter element to search: 25
Element found at index 0
2.2_D Array
[ ]: #2.2_D Array
#include <iostream>
using namespace std;
int main() {
int a[20][20];
int r, c;
int choice;
cout << "\n1. Insert Row\n2. Delete Row\n3. Insert Column\n4. Delete
Column\n";
cout << "Enter choice: ";
cin >> choice;
if (choice == 1) {
// Insert Row
int pos;
cout << "Enter row position to insert (0 to " << r << "): ";
cin >> pos;
else if (choice == 2) {
// Delete Row
int pos;
cout << "Enter row position to delete (0 to " << r - 1 << "): ";
cin >> pos;
else if (choice == 3) {
// Insert Column
int pos;
cout << "Enter column position to insert (0 to " << c << "): ";
cin >> pos;
else if (choice == 4) {
// Delete Column
int pos;
cout << "Enter column position to delete (0 to " << c - 1 << "): ";
cin >> pos;
return 0;
}
#20Output
1. Insert Row
2. Delete Row
3. Insert Column
4. Delete Column
Enter choice: 1
Enter row position to insert (0 to 3): 3
Enter elements of new row:
5
54
Updated Matrix:
011
202
330
554
1. Insert Row
2. Delete Row
3. Insert Column
4. Delete Column
Enter choice: 2
Enter row position to delete (0 to 2): 1
Updated Matrix:
011
330
1. Insert Row
2. Delete Row
3. Insert Column
4. Delete Column
Enter choice: 4
Enter column position to delete (0 to 2): 2
Updated Matrix:
01
20
33
EXPERIMENTS 3 & 4
[ ]: #[Link] Jordan
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n;
float a[20][21];
cout << "Enter augmented matrix (row-wise) for Gauss jordan method:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++) {
cin >> a[i][j];
}
}
// Gauss-Jordan Elimination
for (int i = 0; i < n; i++) {
// Make diagonal element = 1
float pivot = a[i][i];
for (int j = 0; j <= n; j++) {
a[i][j] /= pivot;
}
return 0;
}
#[Link]
Enter number of variables: 3
Enter augmented matrix (row-wise) for Gauss jordan method:
-33 16 0 0.25
16 -33 16 0.5
0 16 -33 0.75
Solution:
x1 = -0.03
x2 = -0.06
x3 = -0.05
int main() {
int n;
float a[20][21], x[20];
// Forward Elimination
for (int i = 0; i < n - 1; i++) {
for (int k = i + 1; k < n; k++) {
float factor = a[k][i] / a[i][i];
for (int j = i; j <= n; j++) {
a[k][j] -= factor * a[i][j];
}
}
}
}
}
}
// Back Substitution
for (int i = n - 1; i >= 0; i--) {
x[i] = a[i][n];
for (int j = i + 1; j < n; j++) {
x[i] -= a[i][j] * x[j];
}
x[i] /= a[i][i];
}
return 0;
}
#[Link]
Enter number of variables: 3
Enter augmented matrix (row-wise)for Gauss Elimination method:
-33 16 0 0.25
16 -33 16 0.5
0 16 -33 0.75
Solution:
x1 = -0.03
x2 = -0.06
x3 = -0.05
EXPERIMENT 5
struct Node {
int data;
Node* next;
};
temp->next = NULL;
return temp;
}
if (head == NULL) {
head = temp;
return;
}
curr->next = temp;
}
if (curr == NULL) {
cout << "Invalid Position\n";
return;
}
void deleteBeg() {
if (head == NULL) {
void deleteEnd() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
if (head->next == NULL) {
delete head;
head = NULL;
return;
}
delete curr->next;
curr->next = NULL;
}
if (pos == 1) {
deleteBeg();
return;
}
void display() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
int countNodes() {
int count = 0;
Node* curr = head;
while (curr != NULL) {
count++;
curr = curr->next;
}
return count;
}
int main() {
int choice, val, pos;
do
{
cout << "\n---- SINGLY LINKED LIST MENU ----\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Delete from Beginning\n";
cout << "5. Delete from End\n";
cout << "6. Delete from Position\n";
cout << "7. Display List\n";
cout << "8. Count Nodes\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
}
case 2:
cout << "Enter value: ";
cin >> val;
insertEnd(val);
break;
case 3:
cout << "Enter value and position: ";
cin >> pos >> val;
insertPos(val, pos);
break;
case 4:
deleteBeg();
break;
case 5:
deleteEnd();
break;
case 6:
cout << "Enter position: ";
cin >> pos;
deletePos(pos);
break;
case 7:
display();
break;
case 8:
cout << "Total Nodes: " << countNodes() << endl;
break;
case 0:
cout << "Exiting...\n";
break;
}
} while (choice != 0);
return 0;
}
#5. Output
0. Exit
Enter choice: 3
Enter value and position: 32 2
Invalid Position
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 10 -> 50 -> 55 -> NULL
struct Node {
int data;
Node* prev;
Node* next;
};
if (head == NULL) {
head = temp;
return;
}
curr->next = temp;
temp->prev = curr;
}
if (curr == NULL) {
if (curr->next != NULL)
curr->next->prev = temp;
curr->next = temp;
}
void deleteBeg() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
if (head != NULL)
head->prev = NULL;
delete temp;
}
void deleteEnd() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
if (head->next == NULL) {
delete head;
head = NULL;
return;
}
curr->prev->next = NULL;
delete curr;
}
if (pos == 1) {
deleteBeg();
return;
}
if (curr == NULL) {
cout << "Invalid Position\n";
return;
}
if (curr->next != NULL)
curr->next->prev = curr->prev;
if (curr->prev != NULL)
curr->prev->next = curr->next;
delete curr;
}
void display() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
int countNodes() {
int count = 0;
Node* curr = head;
while (curr != NULL) {
count++;
curr = curr->next;
}
return count;
}
int main() {
int choice, val, pos;
do {
cout << "\n---- DOUBLY LINKED LIST MENU ----\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Delete from Beginning\n";
cout << "5. Delete from End\n";
cout << "6. Delete from Position\n";
cout << "7. Display List\n";
cout << "8. Count Nodes\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value: ";
cin >> val;
insertBeg(val);
break;
case 2:
cout << "Enter value: ";
cin >> val;
insertEnd(val);
break;
case 3:
cout << "Enter value and position: ";
cin >> pos >> val;
insertPos(val, pos);
break;
case 4:
deleteBeg();
break;
case 5:
deleteEnd();
break;
case 6:
cout << "Enter position: ";
cin >> pos;
deletePos(pos);
break;
case 7:
display();
break;
case 8:
cout << "Total Nodes: " << countNodes() << endl;
break;
case 0:
cout << "Exiting...\n";
break;
}
return 0;
}
#6. Output
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 1 15 1 20 2 25 2 30
Enter value:
----- DOUBLY LINKED LIST MENU -----
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: Enter value:
----- DOUBLY LINKED LIST MENU -----
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: Enter value:
----- DOUBLY LINKED LIST MENU -----
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: Enter value:
----- DOUBLY LINKED LIST MENU -----
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 20 <-> 15 <-> 10 <-> 25 <-> 30 <-> NULL
Total Nodes: 4
struct Node {
int data;
Node* next;
};
if (head == NULL) {
head = temp;
temp->next = head;
return;
}
curr->next = temp;
temp->next = head;
head = temp;
}
if (head == NULL) {
head = temp;
temp->next = head;
return;
}
curr->next = temp;
temp->next = head;
}
void deleteBeg() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
}
if (head->next == head) {
delete head;
head = NULL;
return;
}
void deleteEnd() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
if (head->next == head) {
delete head;
head = NULL;
return;
}
delete curr->next;
curr->next = head;
}
if (pos == 1) {
deleteBeg();
return;
}
}
if (curr->next == head) {
cout << "Invalid Position\n";
return;
}
void display() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
int countNodes() {
if (head == NULL) return 0;
int count = 0;
Node* curr = head;
do {
count++;
curr = curr->next;
} while (curr != head);
return count;
}
int main() {
int choice, val, pos;
do {
cout << "\n--- CIRCULAR SINGLY LINKED LIST ---\n";
}
switch (choice) {
case 1:
cin >> val;
insertBeg(val);
break;
case 2:
cin >> val;
insertEnd(val);
break;
case 3:
cout << "Enter position then value: ";
cin >> pos >> val;
insertPos(pos, val);
break;
case 4:
deleteBeg();
break;
case 5:
deleteEnd();
break;
case 6:
cin >> pos;
deletePos(pos);
break;
case 7:
display();
break;
case 8:
cout << "Total Nodes: " << countNodes() << endl;
break;
}
} while (choice != 0);
return 0;
}
#[Link]
Enter choice:
--- CIRCULAR SINGLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR SINGLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR SINGLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice: 7
List: 25 -> 20 -> 15 -> 10 -> 30 -> 32 -> (HEAD)
8. Count Nodes
0. Exit
Enter choice: 6
1
struct Node {
int data;
Node* prev;
Node* next;
};
if (head == NULL) {
head = temp;
return;
}
if (head == NULL) {
head = temp;
return;
}
if (curr->next == head) {
cout << "Invalid Position\n";
return;
}
void deleteBeg() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
if (head->next == head) {
delete head;
head = NULL;
return;
}
void deleteEnd() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
}
if (head->next == head) {
delete head;
head = NULL;
return;
}
if (pos == 1) {
deleteBeg();
return;
}
if (curr == head) {
cout << "Invalid Position\n";
return;
}
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
delete curr;
}
void display() {
if (head == NULL) {
cout << "List Empty\n";
return;
}
int countNodes() {
if (head == NULL) return 0;
int count = 0;
Node* curr = head;
do {
count++;
curr = curr->next;
} while (curr != head);
return count;
}
int main() {
int choice, val, pos;
do {
cout << "\n--- CIRCULAR DOUBLY LINKED LIST ---\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Insert at Position\n";
cout << "4. Delete from Beginning\n";
cout << "5. Delete from End\n";
cout << "6. Delete from Position\n";
cout << "7. Display List\n";
cout << "8. Count Nodes\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cin >> val;
insertBeg(val);
break;
case 2:
cin >> val;
insertEnd(val);
break;
case 3:
cout << "Enter position then value: ";
return 0;
}
#[Link]
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR DOUBLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR DOUBLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR DOUBLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
--- CIRCULAR DOUBLY LINKED LIST ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display List
8. Count Nodes
0. Exit
Enter choice:
8. Count Nodes
0. Exit
Enter choice: 4
int stackArr[MAX];
int top = -1;
void pop() {
if (top == -1) {
cout << "Stack Underflow\n";
return;
}
cout << "Popped: " << stackArr[top--] << endl;
}
void peek() {
if (top == -1) {
cout << "Stack is Empty\n";
return;
}
cout << "Top Element: " << stackArr[top] << endl;
}
int main() {
int choice, val;
do {
cout << "\n--- STACK USING ARRAY ---\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value: ";
cin >> val;
push(val);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 0:
cout << "Exiting...\n";
break;
}
return 0;
}
#9. Output
struct Node {
int data;
Node* next;
};
};
void pop() {
if (top == NULL) {
cout << "Stack Underflow\n";
return;
}
void peek() {
if (top == NULL) {
cout << "Stack is Empty\n";
return;
}
int main() {
int choice, val;
do {
cout << "\n--- STACK USING LINKED LIST ---\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek\n";
cout << "0. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value: ";
case 2:
pop();
break;
case 3:
peek();
break;
case 0:
cout << "Exiting...\n";
break;
return 0;
}
#[Link]
[ ]: #11.Queue_Array
#include <iostream>
using namespace std;
#define SIZE 5
class Queue {
int arr[SIZE];
int front, rear;
public:
Queue() {
front = -1;
rear = -1;
}
arr[++rear] = value;
cout << value << " inserted\n";
}
void dequeue() {
if (front == -1 || front > rear) {
cout << "Queue Underflow\n";
return;
}
cout << arr[front++] << " deleted\n";
}
void display() {
if (front == -1 || front > rear) {
cout << "Queue is empty\n";
return;
}
cout << "Queue: ";
for (int i = front; i <= rear; i++)
cout << arr[i] << " ";
cout << endl;
}
};
int main() {
Queue q;
int choice, value;
do {
cout << "\n--- Queue using Array ---\n";
cout << "1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value: ";
cin >> value;
[Link](value);
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice\n";
}
} while (choice != 4);
return 0;
}
#11. Output
4. Exit
Enter choice: Enter value: 20 inserted
20 deleted
#12.Queue_Linked list
[ ]: #12.Queue_Linked list
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class Queue {
Node *front, *rear;
public:
Queue() {
front = rear = NULL;
}
if (rear == NULL) {
front = rear = temp;
} else {
rear->next = temp;
rear = temp;
}
}
}
void dequeue() {
if (front == NULL) {
cout << "Queue Underflow\n";
return;
}
front = front->next;
if (front == NULL) rear = NULL;
delete temp;
}
void display() {
if (front == NULL) {
cout << "Queue is empty\n";
return;
}
int main() {
Queue q;
int choice, value;
do {
cout << "\n--- Queue using Linked List ---\n";
cout << "1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value: ";
return 0;
}
#12. Output
3. Display
4. Exit
Enter choice: Enter value: 30 inserted
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 3
Queue: 10 15 5 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 2
10 deleted
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 3
Queue: 15 5 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 2
15 deleted
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 3
Queue: 5 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter choice: 4
Exiting...