Lab Manual DSA
Lab Manual DSA
#include <iostream>
using namespace std;
int main() {
int a[10], item, flag = 0;
if(flag == 0)
cout << "Element Not Found" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a[3][3], item, flag = 0;
if(flag == 0)
cout << "Element Not Found" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a[10], b[10], c[20], i, j, k = 0, n, m;
i = j = 0;
while(i < n && j < m) {
if(a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
while(i < n)
c[k++] = a[i++];
while(j < m)
c[k++] = b[j++];
#include <iostream>
using namespace std;
// Addition
cout << "Addition of two Matrix A and B is:" << endl;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
c[i][j] = a[i][j] + b[i][j];
cout << c[i][j] << "\t";
}
cout << endl;
}
// Subtraction
cout << "Subtraction of two Matrix A and B is:" << endl;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
c[i][j] = a[i][j] - b[i][j];
cout << c[i][j] << "\t";
}
cout << endl;
}
// Multiplication
cout << "Multiplication of Matrix A and B is:" << endl;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
c[i][j] = 0;
for(int k = 0; k < 3; k++) {
c[i][j] += a[i][k] * b[k][j];
}
cout << c[i][j] << "\t";
}
cout << endl;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter the two numbers: ";
cin >> a >> b;
swapByValue(a, b);
swapByRef(a, b);
cout << "\nNumber after swapping by Reference:\n";
cout << "a = " << a << "\nb = " << b << endl;
return 0;
}
int main() {
char a[100], b[100], c[100];
strcat(a, b);
cout << "\nConcatenation of String a and b is: " << a;
int l = strlen(a);
cout << "\nLength of String is: " << l;
strcpy(c, a);
return 0;
}
int main() {
int a[100], n, mid, beg, end, item, loc = -1;
beg = 0;
end = n - 1;
if(loc == -1)
cout << "Element Not Present";
else
cout << "Element found at index: " << loc;
return 0;
}
int main() {
int a[100], n, item;
if(loc == -1)
cout << "Element not Found";
else
cout << "Element Found at position: " << loc;
return 0;
}
int main() {
int a[100], n;
bubble(a, n);
return 0;
}
int main() {
int a[100], n;
return 0;
}
int main() {
int a[100], n;
insertionSort(a, n);
return 0;
}
int main() {
int a[20], i, n;
cout << "Enter the size of array: ";
cin >> n;
quicksort(a, 0, n - 1);
int main() {
int a[20], i, n;
cout << "Enter the number of elements: ";
cin >> n;
mergesort(a, 0, n - 1);
void push();
void pop();
void display();
int main() {
int choice;
char ch;
top = -1;
do {
cout << "\n\t1. PUSH";
cout << "\n\t2. POP";
cout << "\n\t3. DISPLAY";
cout << "\n\t4. EXIT";
cout << "\nEnter your choice: ";
cin >> choice;
switch(choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout << "\nBAD CHOICE";
}
return 0;
}
void push() {
int item;
if(top == 4) {
cout << "STACK IS FULL";
} else {
cout << "Enter the item to be inserted: ";
cin >> item;
top = top + 1;
a[top] = item;
}
}
void pop() {
if(top == -1) {
cout << "STACK IS EMPTY";
} else {
int item = a[top];
top = top - 1;
cout << item << " is deleted";
}
void display() {
if(top == -1) {
cout << "STACK IS EMPTY";
} else {
cout << "\nStack elements are:\n";
for(int i = top; i >= 0; i--) {
cout << a[i] << "\n";
}
}
}
void insert();
void delet();
void display();
int main() {
int choice;
char ch;
front = -1;
rear = -1;
do {
cout << "\n\t1. INSERT";
cout << "\n\t2. DELETE";
cout << "\n\t3. DISPLAY";
cout << "\n\t4. EXIT";
cout << "\nEnter your choice: ";
cin >> choice;
switch(choice) {
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout << "\nBAD CHOICE";
}
return 0;
}
void insert() {
int item;
if (((front == 0 && rear == 4) || (front == rear + 1))) {
cout << "QUEUE IS FULL\n";
} else {
cout << "Enter the element: ";
cin >> item;
if (front == -1) {
front = 0;
rear = 0;
} else if (rear == 4) {
rear = 0;
} else {
rear++;
}
q[rear] = item;
}
}
void delet() {
if (front == -1) {
cout << "QUEUE IS EMPTY\n";
} else {
int item = q[front];
if (front == rear) {
front = -1;
rear = -1;
} else if (front == 4) {
front = 0;
} else {
front++;
}
void display() {
if (front == -1) {
cout << "QUEUE IS EMPTY\n";
} else {
cout << "Queue elements:\n";
int i = front;
if (front <= rear) {
while (i <= rear) {
cout << q[i] << "\n";
i++;
}
} else {
while (i < 5) {
cout << q[i] << "\n";
struct Node {
int info;
Node* next;
};
// Function Prototypes
void insert();
void insertAtBeginning();
void insertAtMiddle();
void insertAtEnd();
void remove();
void deleteAtBeginning();
void deleteAtMiddle();
void deleteAtEnd();
void display();
int countNodes();
int main() {
int ch, cnt;
while (true) {
cout << "\n********** MENU **********";
cout << "\n1. Insert";
cout << "\n2. Delete";
cout << "\n3. Display";
cout << "\n4. Count";
cout << "\n5. Exit";
cout << "\nEnter your choice: ";
cin >> ch;
switch (ch) {
case 1: insert(); break;
case 2: remove(); break;
case 3: display(); break;
case 4:
cnt = countNodes();
cout << "\nThe number of nodes: " << cnt << endl;
break;
case 5: exit(0);
default: cout << "\nInvalid Choice!\n";
return 0;
}
void insert() {
int ch1;
cout << "\nInsert Options:";
cout << "\n1. Insert at the beginning";
cout << "\n2. Insert in the middle";
cout << "\n3. Insert at the end";
cout << "\nEnter your choice: ";
cin >> ch1;
switch (ch1) {
case 1: insertAtBeginning(); break;
case 2: insertAtMiddle(); break;
case 3: insertAtEnd(); break;
default: cout << "\nInvalid Choice!\n";
}
}
void insertAtBeginning() {
int data;
Node* newNode = new Node;
newNode->info = data;
newNode->next = start;
start = newNode;
}
void insertAtMiddle() {
int pos, data;
cout << "\nEnter the position after which new node is to be inserted: ";
cin >> pos;
if (temp == nullptr) {
cout << "\nPosition out of bounds!";
return;
}
newNode->info = data;
newNode->next = temp->next;
temp->next = newNode;
}
void insertAtEnd() {
newNode->info = data;
newNode->next = nullptr;
if (start == nullptr) {
start = newNode;
} else {
Node* temp = start;
while (temp->next != nullptr)
temp = temp->next;
temp->next = newNode;
}
}
void remove() {
int ch2;
cout << "\nDelete Options:";
cout << "\n1. Delete at the beginning";
cout << "\n2. Delete in the middle";
cout << "\n3. Delete at the end";
cout << "\nEnter your choice: ";
cin >> ch2;
switch (ch2) {
case 1: deleteAtBeginning(); break;
case 2: deleteAtMiddle(); break;
case 3: deleteAtEnd(); break;
default: cout << "\nInvalid Choice!\n";
}
}
void deleteAtBeginning() {
if (start == nullptr) {
cout << "\nList is empty!";
return;
}
void deleteAtMiddle() {
if (start == nullptr) {
cout << "\nList is empty!";
return;
}
int value;
cout << "\nEnter the value to be deleted: ";
cin >> value;
if (current == nullptr) {
cout << "\nValue not found!";
return;
}
if (previous == nullptr) {
// First node match
start = start->next;
} else {
previous->next = current->next;
}
delete current;
cout << "\nNode deleted.";
}
void deleteAtEnd() {
if (start == nullptr) {
cout << "\nList is empty!";
return;
}
if (previous == nullptr) {
// Only one node
delete start;
start = nullptr;
} else {
previous->next = nullptr;
delete current;
}
void display() {
if (start == nullptr) {
cout << "\nList is empty!";
return;
}
int countNodes() {
int count = 0;
Node* temp = start;
while (temp != nullptr) {
++count;
temp = temp->next;
}
return count;
}