Lab File
Lab File
Practical File
Date
S.no Title
Output:
Q2: Write a function, strmdel, that accepts a string and two integers, start and length.
Return a new string that is equivalent to the original string, except that length
characters beginning at start have been removed.
Code:
#include <iostream>
#include <string>
using namespace std;
void strmdel(string &str, int start, int length) {
str.erase(start, length);}
int main() {
string str;
int start, length;
cout << "Enter a string: ";
getline(cin, str);
cout << "Enter start index and length: ";
cin >> start >> length;
strmdel(str, start, length);
cout << "Updated string: " << str << endl;
return 0;
}
Output:
Q3: Write a function, strdel, that accepts a string and a character. The function
returns the string with the first occurrence of the character removed.
Code :
#include <iostream>
#include <string>
using namespace std;
void strdel(string &str, char ch) {
size_t pos = str.find(ch);
if (pos != string::npos) {
str.erase(pos, 1);}}
int main() {
string str;
char ch;
cout << "Enter a string: ";
getline(cin, str);
cout << "Enter a character to remove: ";
cin >> ch;
strdel(str, ch);
cout << "Updated string: " << str << endl;
return 0;}
Output:
Q4: Write a program (WAP) with a function to swap two numbers using call by
reference.
Code:
#include <iostream>
using namespace std;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;}
int main() {
int x, y;
cout << "Enter two numbers: ";
cin >> x >> y;
swap(x, y);
cout << "After swapping: x = " << x << ", y = " << y << endl;
return 0;}
Output:
Q5: Write a program (WAP) with a function to swap two integer arrays of the same
size using call by reference.
Code:
#include <iostream>
using namespace std;
void swapArrays(int arr1[], int arr2[], int size) {
for (int i = 0; i < size; i++) {
int temp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = temp;
}}
int main() {
int n;
cout << "Enter the size of arrays: ";
cin >> n;
int arr1[n], arr2[n];
cout << "Enter elements of first array:\n";
for (int i = 0; i < n; i++) {
cin >> arr1[i];
}
cout << "Enter elements of second array:\n";
for (int i = 0; i < n; i++) {
cin >> arr2[i];
}
swapArrays(arr1, arr2, n);
cout << "After swapping:\nFirst array: ";
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
cout << "\nSecond array: ";
for (int i = 0; i < n; i++) {
cout << arr2[i] << " ";
}
return 0;
}
Output:
Q6: Write a program (WAP) to reverse an array by swapping (without using
additional memory).
Code:
#include <iostream>
using namespace std;
Q7: Write a program (WAP) with a function to swap two strings using pointers.
Code:
#include <iostream>
#include <string>
using namespace std;
void swapStrings(string *str1, string *str2) {
string temp = *str1;
*str1 = *str2;
*str2 = temp;
}
int main() {
string str1, str2;
cout << "Enter first string: ";
getline(cin, str1);
cout << "Enter second string: ";
getline(cin, str2);
swapStrings(&str1, &str2);
cout << "After swapping:\nFirst string: " << str1 << "\nSecond string: " << str2 <<
endl;
return 0;
}
Output:
Q8: Write a program (WAP) to find the number of non-repeated elements in an array.
Code:
#include <iostream>
using namespace std;
void findNonRepeated(int arr[], int n) {
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j]) {
count++;}}
if (count == 1) {
cout << arr[i] << " ";}}
cout << endl;}
int main() {
int n;
cout << "Enter size of array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];}
cout << "Non-repeated elements: ";
findNonRepeated(arr, n);
return 0;}
Output:
Q9: Write a program (WAP) to identify the missing numbers in a given array within
the range [1…N].
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array:\n";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
findMissingNumbers(arr, n);
return 0;
}
Output:
Experiment 2
Q1: Write a program (WAP) to find the median of the elements after merging two
sorted arrays of the same size.
Code:
#include <iostream>
#include <algorithm>
using namespace std;
cout << "Median of merged array: " << median << endl;
}
int main() {
int n;
cout << "Enter the size of both arrays: ";
cin >> n;
struct Student {
string name;
int roll_no;
float total_marks;
};
int main() {
int count = 0;
int capacity = 2; // Initial capacity
Student* students = new Student[capacity];
while (true) {
cout << "1. Insert student data\n2. Display student data\n3. Exit\nChoose an
option: ";
int choice;
cin >> choice;
if (choice == 1) {
if (count == capacity) {
capacity *= 2;
Student* newStudents = new Student[capacity];
for (int i = 0; i < count; i++) {
newStudents[i] = students[i];
}
delete[] students;
students = newStudents;
}
delete[] students;
return 0;
}
Output:
Q3: Implement the above program for Employee structures (contains
employee_name, emp_no, emp_salary).
Code:
#include <iostream>
#include <string>
using namespace std;
struct Employee {
string name;
int emp_no;
float emp_salary;
};
void displayEmployees(Employee* employees, int count) {
cout << "Employee Information:\n";
for (int i = 0; i < count; i++) {
cout << "Name: " << employees[i].name
<< ", Employee No: " << employees[i].emp_no
<< ", Salary: " << employees[i].emp_salary << endl;
}
}
int main() {
int count = 0;
int capacity = 2;
Employee* employees = new Employee[capacity];
while (true) {
cout << "1. Insert employee data\n2. Display employee data\n3. Exit\nChoose
an option: ";
int choice;
cin >> choice;
if (choice == 1) {
if (count == capacity) {
capacity *= 2;
Employee* newEmployees = new Employee[capacity];
for (int i = 0; i < count; i++) {
newEmployees[i] = employees[i];
}
delete[] employees;
employees = newEmployees;
}
cout << "Enter employee name: ";
cin.ignore();
getline(cin, employees[count].name);
cout << "Enter employee number: ";
cin >> employees[count].emp_no;
cout << "Enter employee salary: ";
cin >> employees[count].emp_salary;
count++;
}
else if (choice == 2) {
displayEmployees(employees, count);
}
else if (choice == 3) {
break;
}
}
delete[] employees;
return 0;
}
Output:
Q4: Implement the above program for Faculty structures (contains faculty_name,
faculty_ID, subject_codes, class_names).
Code:
#include <iostream>
#include <string>
using namespace std;
struct Faculty {
string name;
int faculty_ID;
string subject_codes;
string class_names;
};
int main() {
int count = 0;
int capacity = 2;
Faculty* faculties = new Faculty[capacity];
while (true) {
cout << "1. Insert faculty data\n2. Display faculty data\n3. Exit\nChoose an
option: ";
int choice;
cin >> choice;
if (choice == 1) {
if (count == capacity) {
capacity *= 2;
Faculty* newFaculties = new Faculty[capacity];
for (int i = 0; i < count; i++) {
newFaculties[i] = faculties[i];
}
delete[] faculties;
faculties = newFaculties;
}
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string filename = "example.txt"; // Filename to work with
return 0;
}
Output:
Experiment 3
Q1: Write a program (WAP) to implement Linear Search and Binary Search on a 1D
array of integers.
Code:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, key;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
return 0;
}
Output:
Q2: Write a program (WAP) to implement Insertion Sort and Selection Sort on a 1D
array of strings.
Code:
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
cout << "Enter the number of strings: ";
cin >> n;
string arr[n];
// Insertion Sort
insertionSort(arr, n);
cout << "Array after Insertion Sort: ";
printArray(arr, n);
// Selection Sort
selectionSort(arr, n);
cout << "Array after Selection Sort: ";
printArray(arr, n);
return 0;
}
Output:
Code:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of characters: ";
cin >> n;
char arr[n];
quickSort(arr, 0, n - 1);
return 0;
}
Output:
Q4:
Write a program (WAP) to implement Merge Sort on a 1D array of Student structures
(contains student_name, student_roll_no, total_marks) with the key as
student_roll_no.
Code:
#include <iostream>
#include <string>
using namespace std;
// Student structure
struct Student {
string name;
int roll_no;
float total_marks;
};
int i = 0, j = 0, k = left;
int main() {
int n;
cout << "Enter the number of students: ";
cin >> n;
Student arr[n];
mergeSort(arr, 0, n - 1);
return 0;
}
Output:
Q5: Write a program (WAP) to implement Bubble Sort and Radix Sort on a 1D array
of Faculty structures (contains faculty_name, faculty_ID, subject_codes,
class_names) with the key as faculty_ID.
Code:
#include <iostream>
#include <string>
using namespace std;
// Faculty structure
struct Faculty {
string name;
int faculty_ID;
string subject_codes;
string class_names;
};
int main() {
int n;
cout << "Enter the number of faculty members: ";
cin >> n;
Faculty arr[n];
bubbleSort(arr, n);
return 0;
}
Output:
Experiment 4
Q1: WAP to store and display a Lower-Right triangular matrix in RMO and CMO
fashion.
Code:
#include <iostream>
using namespace std;
int main() {
int n = 4;
int matrix[10][10] = {
{1, 2, 3, 4},
{0, 5, 6, 7},
{0, 0, 8, 9},
{0, 0, 0, 10}
};
displayRMO(storeRMO, 10);
displayCMO(storeCMO, 10);
return 0;
}
Output:
Q2: WAP to store and display an Upper-Right triangular matrix in RMO and CMO
fashion.
Code:
#include <iostream>
using namespace std;
int main() {
int n = 3; // Size of matrix
int matrix[10][10] = {
{1, 2, 3},
{0, 4, 5},
{0, 0, 6}
};
// Store in RMO
storeUpperRightRMO(n, matrix, storeRMO);
cout << "Upper-Right Triangular Matrix in RMO:" << endl;
displayUpperRightRMO(n, storeRMO);
// Store in CMO
storeUpperRightCMO(n, matrix, storeCMO);
cout << "Upper-Right Triangular Matrix in CMO:" << endl;
displayUpperRightCMO(n, storeCMO);
return 0;
}
Output:
Q3: WAP to store and display a Tri-Diagonal matrix in RMO and CMO fashion.
Code:
#include <iostream>
using namespace std;
int main() {
int n = 3; // Size of matrix
int matrix[10][10] = {
{1, 2, 0},
{3, 4, 5},
{0, 6, 7}
};
int storeRMO[9];
// Store in RMO
storeTriDiagonalRMO(n, matrix, storeRMO);
cout << "Tri-Diagonal Matrix in RMO:" << endl;
displayTriDiagonalRMO(n, storeRMO);
return 0;
}
Output:
Q4: WAP to store and display a Lower-Left triangular matrix in RMO and CMO
fashion.
Code:
#include <iostream>
using namespace std;
int main() {
int n = 3; // Size of matrix
int matrix[10][10] = {
{1, 0, 0},
{2, 3, 0},
{4, 5, 6}
};
int storeRMO[6];
// Store in RMO
storeLowerLeftRMO(n, matrix, storeRMO);
cout << "Lower-Left Triangular Matrix in RMO:" << endl;
displayLowerLeftRMO(n, storeRMO);
return 0;
}
Output:
Q5: WAP to store and display an Upper-Left triangular matrix in RMO and CMO
fashion.
Code:
#include <iostream>
using namespace std;
int main() {
int n = 3; // Size of matrix
int matrix[10][10] = {
{1, 0, 0},
{2, 3, 0},
{4, 5, 6}
};
int storeRMO[6];
// Store in RMO
storeUpperLeftRMO(n, matrix, storeRMO);
cout << "Upper-Left Triangular Matrix in RMO:" << endl;
displayUpperLeftRMO(n, storeRMO);
return 0;
}
Output:
Q6: WAP to store and display a C matrix in RMO and CMO fashion. (C matrix
contains non-zero elements in the first row, last row, and first column only).
Code:
#include <iostream>
using namespace std;
int main() {
int n = 4;
int matrix[10][10] = {
{1, 2, 3, 4},
{5, 0, 0, 0},
{6, 0, 0, 0},
{7, 8, 9, 10}
};
return 0;
}
Q7: WAP to store and display a Z matrix in RMO and CMO fashion. (Z matrix
contains non-zero elements in the first row, last row, and right diagonal only).
Code:
#include <iostream>
using namespace std;
// First row
for (int j = 0; j < n; j++) {
store[index++] = matrix[0][j];
}
// Last row
for (int j = 0; j < n; j++) {
store[index++] = matrix[n-1][j];
}
}
// First row
for (int j = 0; j < n; j++) {
store[index++] = matrix[0][j];
}
// Last row
for (int j = 0; j < n; j++) {
store[index++] = matrix[n-1][j];
}
}
// First row
for (int j = 0; j < n; j++) {
cout << store[index++] << " ";
}
cout << endl;
// Last row
for (int j = 0; j < n; j++) {
cout << store[index++] << " ";
}
cout << endl;
}
// First row
for (int j = 0; j < n; j++) {
cout << store[index++] << " ";
}
cout << endl;
// Last row
for (int j = 0; j < n; j++) {
cout << store[index++] << " ";
}
cout << endl;
}
int main() {
int n = 4; // Size of matrix
int matrix[10][10] = {
{1, 2, 3, 4},
{0, 0, 5, 0},
{0, 6, 0, 0},
{7, 8, 9, 10}
};
// Store in RMO
storeZMatrixRMO(n, matrix, storeRMO);
cout << "Z Matrix in RMO:" << endl;
displayZMatrixRMO(n, storeRMO);
// Store in CMO
storeZMatrixCMO(n, matrix, storeCMO);
cout << "Z Matrix in CMO:" << endl;
displayZMatrixCMO(n, storeCMO);
return 0;
}
Output:
Q8: WAP using switch statement to perform the following functions: 1. Transpose of
a matrix. 2. Computing determinant of a matrix. 3. Addition of two matrices. 4.
Multiplication of two matrices. The program should handle wrong input exceptions
such as size mismatch, etc.
Code:
#include <iostream>
using namespace std;
int submatrix[10][10];
for (int x = 0; x < n; x++) {
// Forming the submatrix
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x) continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
// Recursive call for determinant calculation
det += (x % 2 == 0 ? 1 : -1) * matrix[0][x] * determinantMatrix(n - 1, submatrix);
}
return det;
}
int main() {
int choice;
int n = 2; // You can change this to test larger matrices
int matrix1[10][10] = {{1, 2}, {3, 4}};
int matrix2[10][10] = {{5, 6}, {7, 8}};
int result[10][10];
switch (choice) {
case 1:
transposeMatrix(n, matrix1, result);
cout << "Transpose Matrix:\n";
printMatrix(n, result);
break;
case 2:
cout << "Determinant: " << determinantMatrix(n, matrix1) << endl;
break;
case 3:
addMatrices(n, matrix1, matrix2, result);
cout << "Matrix Addition Result:\n";
printMatrix(n, result);
break;
case 4:
multiplyMatrices(n, matrix1, matrix2, result);
cout << "Matrix Multiplication Result:\n";
printMatrix(n, result);
break;
default:
cout << "Invalid choice!";
}
return 0;
}
Output:
Q9: WAP for addition and multiplication of two sparse matrices. The matrices can be
of any of the following types: - a. Lower-Right triangular matrix. - b. Upper-Right
triangular matrix. - c. Tri-Diagonal matrix. - d. Lower-Left triangular matrix. - e.
Upper-Left triangular matrix. Firstly, the program should ask the user about the
type(s) of input matrices, store the given matrices either in RMO or CMO fashion in a
1D array, and then perform the respective operation and display the result.
Code:
#include <iostream>
using namespace std;
int main() {
int n = 3; // Size of the matrix
int matrix2[10][10] = {
{7, 8, 9},
{0, 10, 11},
{0, 0, 12}
};
// Perform addition
addLowerRightRMO(n, store1, store2, result);
cout << "Addition of Lower-Right Matrices in RMO:" << endl;
displayMatrix(n, result);
// Perform multiplication
multiplyLowerRightRMO(n, store1, store2, result);
cout << "Multiplication of Lower-Right Matrices in RMO:" << endl;
displayMatrix(n, result);
return 0;
}
Output:
Experiment 5
Q1a: Write a program (WAP) to implement Stack Abstract Data Type (ADT) using
arrays, which has basic operations as: Create(), IsEmpty(), Push(), Pop(), IsFull()
Code:
#include<iostream>
using namespace std;
#define MAX 100
class Stack {
int top;
int arr[MAX];
public:
Stack() { top = -1; }
bool IsFull() {
return top >= MAX - 1;
}
bool IsEmpty() {
return top == -1;
}
void Push(int x) {
if (IsFull()) {
cout << "Stack Overflow\n";
} else {
arr[++top] = x;
cout << x << " pushed into stack\n";
}
}
int Pop() {
if (IsEmpty()) {
cout << "Stack Underflow\n";
return -1;
} else {
int val = arr[top--];
cout << val << " popped from stack\n";
return val;
}
}
};
int main() {
Stack s;
s.Push(10);
s.Push(20);
s.Pop();
s.Pop();
return 0;
}
Output:
Code:
#include<iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
cyclicPermute(arr, n);
Code:
#include<iostream>
#include<stack>
using namespace std;
int main() {
string str = "madam";
if (isPalindrome(str)) {
cout << str << " is a palindrome\n";
} else {
cout << str << " is not a palindrome\n";
}
return 0;
}
Output:
Q3a: WAP to check if the given expression is correctly parenthesized.
Code:
#include<iostream>
#include<stack>
using namespace std;
int main() {
string expr = "{[()]}";
if (isBalanced(expr)) {
cout << "Balanced\n";
} else {
cout << "Not Balanced\n";
}
return 0;
}
Output:
Q3b: WAP to implement two overlapping stacks, facing in opposite directions, using
an array and check for overflow and underflow conditions.
Code:
#include<iostream>
using namespace std;
#define MAX 100
class TwoStacks {
int arr[MAX];
int top1, top2;
public:
TwoStacks() {
top1 = -1;
top2 = MAX;
}
void Push1(int x) {
if (top1 < top2 - 1) {
arr[++top1] = x;
} else {
cout << "Stack Overflow\n";
}
}
void Push2(int x) {
if (top1 < top2 - 1) {
arr[--top2] = x;
} else {
cout << "Stack Overflow\n";
}
}
int Pop1() {
if (top1 >= 0) {
return arr[top1--];
} else {
cout << "Stack Underflow\n";
return -1;
}
}
int Pop2() {
if (top2 < MAX) {
return arr[top2++];
} else {
cout << "Stack Underflow\n";
return -1;
}
}
};
int main() {
TwoStacks ts;
ts.Push1(5);
ts.Push2(10);
ts.Push1(15);
ts.Push2(20);
cout << "Popped from Stack1: " << ts.Pop1() << endl;
cout << "Popped from Stack2: " << ts.Pop2() << endl;
return 0;
}
Output:
Q4: WAP to implement 3-stacks of size m in an array of size n, with all basic
operations: IsEmpty(), Push(), Pop(), IsFull()
Code:
#include<iostream>
using namespace std;
#define MAX 100
class ThreeStacks {
int arr[MAX];
int top1, top2, top3, n;
public:
ThreeStacks(int n) {
this->n = n;
top1 = -1;
top2 = n / 3 - 1;
top3 = 2 * n / 3 - 1;
}
void Push1(int x) {
if (top1 < n / 3 - 1) {
arr[++top1] = x;
} else {
cout << "Stack 1 Overflow\n";
}
}
void Push2(int x) {
if (top2 < 2 * n / 3 - 1) {
arr[++top2] = x;
} else {
cout << "Stack 2 Overflow\n";
}
}
void Push3(int x) {
if (top3 < n - 1) {
arr[++top3] = x;
} else {
cout << "Stack 3 Overflow\n";
}
}
int Pop1() {
if (top1 >= 0) {
return arr[top1--];
} else {
cout << "Stack 1 Underflow\n";
return -1;
}
}
int Pop2() {
if (top2 >= n / 3) {
return arr[top2--];
} else {
cout << "Stack 2 Underflow\n";
return -1;
}
}
int Pop3() {
if (top3 >= 2 * n / 3) {
return arr[top3--];
} else {
cout << "Stack 3 Underflow\n";
return -1;
}
}
};
int main() {
ThreeStacks ts(9);
ts.Push1(5);
ts.Push2(10);
ts.Push3(15);
cout << "Popped from Stack 1: " << ts.Pop1() << endl;
cout << "Popped from Stack 2: " << ts.Pop2() << endl;
cout << "Popped from Stack 3: " << ts.Pop3() << endl;
return 0;
}
Output:
Code:
#include<iostream>
#include<stack>
using namespace std;
while (!s.empty()) {
result += s.top();
s.pop();
}
return result;
}
int main() {
string exp = "A+B*C";
cout << "Postfix Expression: " << infixToPostfix(exp) << endl;
return 0;
}
Output:
Q5b: WAP to transform infix expression into equivalent prefix expression using
stack, similar to above.
Code:
#include<iostream>
#include<stack>
#include<algorithm>
using namespace std;
while (!s.empty()) {
result += s.top();
s.pop();
}
return result;
}
return postfix;
}
int main() {
string exp = "(A-B/C)*(A/K-L)";
cout << "Prefix Expression: " << infixToPrefix(exp) << endl;
return 0;
}
Output:
Q6a: WAP that gives the solution to the Tower of Hanoi problem for n disks. Test the
program using: - n = 3, n = 4.
Code:
#include<iostream>
using namespace std;
int main() {
int n = 4; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B, C are names of rods
return 0;
}
Output:
Code:
#include<iostream>
using namespace std;
#define N 4
if (!solveMazeUtil(maze, 0, 0, sol)) {
cout << "No solution exists" << endl;
return false;
}
if (isSafe(maze, x, y)) {
if (sol[x][y] == 1) return false;
sol[x][y] = 1;
sol[x][y] = 0;
return false;
}
return false;
}
int main() {
int maze[N][N] = { {1, 0, 0, 0},
{1, 1, 0, 1},
{0, 1, 0, 0},
{1, 1, 1, 1} };
solveMaze(maze);
return 0;
}
Output:
Q7a: WAP to implement Queue ADT using arrays with the basic functions of:
Create(), IsEmpty(), Insert(), Delete(), IsFull()
Code:
#include<iostream>
using namespace std;
#define MAX 5
class Queue {
int front, rear;
int arr[MAX];
public:
Queue() {
front = -1;
rear = -1;
}
bool IsFull() {
return (rear + 1) % MAX == front;
}
bool IsEmpty() {
return front == -1;
}
void Insert(int x) {
if (IsFull()) {
cout << "Queue is Full\n";
return;
}
if (IsEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX;
}
arr[rear] = x;
cout << x << " inserted into queue\n";
}
void Delete() {
if (IsEmpty()) {
cout << "Queue is Empty\n";
return;
}
cout << arr[front] << " deleted from queue\n";
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
void Display() {
if (IsEmpty()) {
cout << "Queue is Empty\n";
return;
}
int i = front;
while (i != rear) {
cout << arr[i] << " ";
i = (i + 1) % MAX;
}
cout << arr[rear] << endl;
}
};
int main() {
Queue q;
q.Insert(10);
q.Insert(20);
q.Insert(30);
q.Display();
q.Delete();
q.Display();
return 0;
}
Output:
class QueueUsingStacks {
stack<int> s1, s2;
public:
void Insert(int x) {
s1.push(x);
cout << x << " inserted into queue\n";
}
void Delete() {
if (s2.empty()) {
if (s1.empty()) {
cout << "Queue is Empty\n";
return;
}
while (!s1.empty()) {
s2.push(s1.top());
s1.pop();
}
}
cout << s2.top() << " deleted from queue\n";
s2.pop();
}
};
int main() {
QueueUsingStacks q;
q.Insert(10);
q.Insert(20);
q.Delete();
q.Insert(30);
q.Delete();
return 0;
}
Output:
Q8a : WAP to implement 2 overlapping queues in an array of size n. There are two
queues facing in opposite directions with basic operations: IsEmpty(), Insert(),
Delete(), IsFull().
Code:
#include<iostream>
using namespace std;
#define MAX 10
class TwoQueues {
int arr[MAX];
int front1, rear1, front2, rear2;
public:
TwoQueues() {
front1 = rear1 = -1;
front2 = rear2 = MAX;
}
void Insert1(int x) {
if (rear1 < rear2 - 1) {
if (front1 == -1) front1 = 0;
arr[++rear1] = x;
cout << x << " inserted into Queue 1\n";
} else {
cout << "Queue 1 Overflow\n";
}
}
void Insert2(int x) {
if (rear1 < rear2 - 1) {
if (front2 == MAX) front2 = MAX - 1;
arr[--rear2] = x;
cout << x << " inserted into Queue 2\n";
} else {
cout << "Queue 2 Overflow\n";
}
}
void Delete1() {
if (front1 == -1) {
cout << "Queue 1 Underflow\n";
} else {
cout << arr[front1] << " deleted from Queue 1\n";
if (front1 == rear1) {
front1 = rear1 = -1;
} else {
front1++;
}
}
}
void Delete2() {
if (front2 == MAX) {
cout << "Queue 2 Underflow\n";
} else {
cout << arr[front2] << " deleted from Queue 2\n";
if (front2 == rear2) {
front2 = rear2 = MAX;
} else {
front2--;
}
}
}
};
int main() {
TwoQueues q;
q.Insert1(10);
q.Insert2(20);
q.Delete1();
q.Insert1(30);
q.Delete2();
return 0;
}
Output:
Q8b: WAP to implement dequeue using arrays with all the basic operations.
Code:
#include<iostream>
using namespace std;
#define MAX 5
class Deque {
int arr[MAX];
int front, rear, size;
public:
Deque() {
front = -1;
rear = 0;
size = 0;
}
bool IsFull() {
return (size == MAX);
}
bool IsEmpty() {
return (size == 0);
}
void InsertFront(int x) {
if (IsFull()) {
cout << "Deque Overflow\n";
return;
}
if (front == -1) {
front = rear = 0;
} else if (front == 0) {
front = MAX - 1;
} else {
front = front - 1;
}
arr[front] = x;
size++;
}
void InsertRear(int x) {
if (IsFull()) {
cout << "Deque Overflow\n";
return;
}
if (rear == MAX - 1) {
rear = 0;
} else {
rear = rear + 1;
}
arr[rear] = x;
size++;
}
void DeleteFront() {
if (IsEmpty()) {
cout << "Deque Underflow\n";
return;
}
if (front == rear) {
front = rear = -1;
} else if (front == MAX - 1) {
front = 0;
} else {
front++;
}
size--;
}
void DeleteRear() {
if (IsEmpty()) {
cout << "Deque Underflow\n";
return;
}
if (rear == front) {
rear = front = -1;
} else if (rear == 0) {
rear = MAX - 1;
} else {
rear--;
}
size--;
}
void Display() {
if (IsEmpty()) {
cout << "Deque is empty\n";
return;
}
int i = front;
while (i != rear) {
cout << arr[i] << " ";
i = (i + 1) % MAX;
}
cout << arr[rear] << endl;
}
};
int main() {
Deque dq;
dq.InsertRear(5);
dq.InsertRear(10);
dq.InsertFront(15);
dq.Display();
dq.DeleteRear();
dq.Display();
return 0;
}
Output:
Experiment 6
Q1:
WAP to construct simple linear linked list using dynamic memory allocation for the
given elements with the following functions, (a) Inserting a new node, (b) Accessing
a node (finding the position wrt header), (c) Removing a node with particular key
value, (d) Complete deletion of a linked list, and (e) Displaying the current list. (f)
Copy the linked list and return the pointer of the new list.
Code:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class LinkedList {
Node* head;
public:
LinkedList() {
head = nullptr;
}
prev->next = temp->next;
delete temp;
cout << "Removed: " << key << endl;
}
// (f) Copy the linked list and return the pointer of the new list
LinkedList* Copy() {
LinkedList* newList = new LinkedList();
Node* current = head;
Node** lastPtrRef = &(newList->head);
int main() {
LinkedList list;
list.Insert(10);
list.Insert(20);
list.Insert(30);
list.Display();
Node* node = list.Access(1);
if (node) cout << "Node at position 1: " << node->data << endl;
list.Remove(20);
list.Display();
list.DeleteList();
list.Display();
return 0;
}
Output:
Q2: (a) WAP to reverse a singly linked list using one auxillary pointer. And try without
using any auxillary pointer. (b) WAP to implement Stack ADT using Linked list with
the basic operations as Create(), IsEmpty(), Push(), Pop(), IsFull() with appropriate
prototype to a functions.
Code a:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class LinkedList {
Node* head;
public:
LinkedList() {
head = nullptr;
}
void Display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
int main() {
LinkedList list;
list.Insert(10);
list.Insert(20);
list.Insert(30);
list.Reverse();
return 0;
}
Output A:
Code b:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Stack {
Node* top;
public:
Stack() {
top = nullptr;
}
bool IsEmpty() {
return top == nullptr;
}
void Pop() {
if (IsEmpty()) {
cout << "Stack is Empty" << endl;
return;
}
Node* temp = top;
cout << "Popped: " << top->data << endl;
top = top->next;
delete temp;
}
void Display() {
Node* temp = top;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};
int main() {
Stack stack;
stack.Push(10);
stack.Push(20);
stack.Push(30);
stack.Display();
stack.Pop();
stack.Display();
return 0;
}
Output B;
Q3: WAP to implement Queue ADT using Linked list with the basic functions of
Create(), IsEmpty(), Insert(), Delete() and IsFull() with suitable prototype to a
functions.
Code:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class Queue {
Node *front, *rear;
public:
Queue() {
front = rear = nullptr;
}
bool IsEmpty() {
return front == nullptr;
}
rear->next = newNode;
rear = newNode;
cout << "Inserted: " << newData << endl;
}
void Delete() {
if (IsEmpty()) {
cout << "Queue is Empty" << endl;
return;
}
if (front == nullptr) {
rear = nullptr;
}
void Display() {
Node* temp = front;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};
int main() {
Queue q;
q.Insert(10);
q.Insert(20);
q.Insert(30);
q.Display();
q.Delete();
q.Display();
return 0;
}
Output:
Q4: WAP for polynomial addition. Represent polynomial in linked list form with
suitable data structure.
Code:
#include<iostream>
using namespace std;
class Node {
public:
int coeff;
int power;
Node* next;
};
class Polynomial {
Node* head;
public:
Polynomial() {
head = nullptr;
}
// Polynomial addition
Polynomial* Add(Polynomial* poly2) {
Polynomial* result = new Polynomial();
Node* p1 = head;
Node* p2 = poly2->head;
return result;
}
};
int main() {
Polynomial poly1, poly2;
poly1.AddTerm(3, 2);
poly1.AddTerm(5, 1);
poly1.AddTerm(6, 0);
poly2.AddTerm(2, 2);
poly2.AddTerm(3, 1);
poly2.AddTerm(4, 0);
delete result;
return 0;
}
Output:
Q5: WAP to swap elements in pairs in the given linked list. (e.g.: 1->2->3->4->5-
>null, then result should be 2->1->4->3->5->null.
Code:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class LinkedList {
Node* head;
public:
LinkedList() {
head = nullptr;
}
void Display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};
int main() {
LinkedList list;
list.Insert(5);
list.Insert(4);
list.Insert(3);
list.Insert(2);
list.Insert(1);
list.SwapInPairs();
return 0;
}
Output:
Q6: (a) WAP to find second last node of the given linked list. (b) WAP to
concatenate two singly linked list in sorted order either ascending or descending.
Code A:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class LinkedList {
Node* head;
public:
LinkedList() {
head = nullptr;
}
void Display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};
int main() {
LinkedList list;
list.Insert(10);
list.Insert(20);
list.Insert(30);
list.Display();
return 0;
}
Output A:
Code B:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class LinkedList {
Node* head;
public:
LinkedList() {
head = nullptr;
}
void Display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
return resultList;
}
};
int main() {
LinkedList list1, list2;
list1.Insert(10);
list1.Insert(20);
list1.Insert(30);
list2.Insert(15);
list2.Insert(25);
list2.Insert(35);
return 0;
}
Output B
Experiment 7
Q1: WAP to create a Circular Singly Linked List for the given elements with the
following functions:
(a) Inserting a node, before the node with key givenkey,
(b) Inserting a node, after the node with key givenkey,
(c) Accessing a node (finding the position wrt header),
(d) Removing a node with particular key value,
(e) Complete deletion of a list,
(f) Displaying the current list, and
(g) Sorting the list.
Code:
#include <iostream>
struct Node {
int data;
Node* next;
};
class CircularSinglyLinkedList {
private:
Node* head;
public:
CircularSinglyLinkedList() : head(nullptr) {}
do {
index = current->next;
while (index != head) {
if (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
} while (current != head);
}
do {
nextNode = current->next;
delete current;
current = nextNode;
} while (current != head);
head = nullptr;
}
~CircularSinglyLinkedList() {
deleteList(); // Clean up on destruction
}
};
int main() {
CircularSinglyLinkedList list;
return 0;
}
Output:
Q2: WAP to implement doubly linked list with the following operations.
Code:
#include <iostream>
#include <string>
struct Student {
std::string student_name;
int student_roll_no;
float total_marks;
};
struct DNode {
Student data;
DNode* next;
DNode* prev;
};
class DoublyLinkedList {
private:
DNode* head;
public:
DoublyLinkedList() : head(nullptr) {}
std::cout << "Key " << key << " not found!" << std::endl;
delete newNode; // Clean up
}
std::cout << "Key " << key << " not found!" << std::endl;
delete newNode; // Clean up
}
do {
if (pos == index - 1) {
newNode->next = current->next;
newNode->prev = current;
current->next->prev = newNode;
current->next = newNode;
return;
}
current = current->next;
pos++;
} while (current != head);
std::cout << "Index " << index << " out of bounds!" << std::endl;
delete newNode; // Clean up
}
do {
if (current->data.student_roll_no == key) return position;
current = current->next;
position++;
} while (current != head);
do {
if (current->data.student_roll_no == key) {
if (current == head && current->next == head) { // Only one node
delete head;
head = nullptr;
} else {
current->prev->next = current->next;
current->next->prev = current->prev;
if (current == head) head = current->next; // Update head if necessary
delete current;
}
return;
}
current = current->next;
} while (current != head);
std::cout << "Key " << key << " not found!" << std::endl;
}
do {
nextNode = current->next;
delete current;
current = nextNode;
} while (current != head);
head = nullptr;
}
do {
index = current->next;
while (index != head) {
if (current->data.student_roll_no > index->data.student_roll_no) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
} while (current != head);
}
~DoublyLinkedList() {
deleteList(); // Clean up on destruction
}
};
int main() {
DoublyLinkedList list;
// Create students
Student s1 = {"Alice", 1, 85.0};
Student s2 = {"Bob", 2, 90.0};
Student s3 = {"Charlie", 3, 95.0};
Student s4 = {"David", 4, 88.0};
Student s5 = {"Eve", 5, 92.0};
// Insert students directly into the list
list.insertAt(0, s1); // Insert Alice at index 0
list.insertAt(1, s2); // Insert Bob at index 1
list.insertAt(2, s3); // Insert Charlie at index 2
list.insertAt(3, s4); // Insert David at index 3
list.insertAt(4, s5); // Insert Eve at index 4
// Accessing a node
int position = list.accessNode(4); // Access position of David
std::cout << "\nPosition of roll number 4 (David): " << position << std::endl;
// Removing a node
list.removeNode(2); // Remove Bob
std::cout << "\nList after removing roll number 2 (Bob):" << std::endl;
list.displayClockwise(); // Display the current list
return 0;
}
Output: