Assignment SEM2 1
Assignment SEM2 1
Problems Page. No
1. Write a program to search an element from a list. Give 1
12. WAP to display Fibonacci series (i) using recursion, (ii) using iteration 85
vice-versa.
16. WAP to reverse the order of the elements in the stack 121
17. WAP to reverse the order of the elements in the stack 124
one-dimensional array.
one-dimensional array.
Problem Statement: Write a program to search an element from a list. Give user the option to perform
Linear or Binary search.
Program Listing:
#include <iostream>
int main() {
int arr[100];
1
switch (choice) {
case 1:
linear_search(arr, length);
break;
case 2:
binary_search(arr, length);
break;
default:
return 0;
int i, key;
if (arr[i] == key) {
return;
2
void binary_search(int arr[], int length) {
sort(arr, length);
if (arr[mid] == key) {
return;
beg = mid + 1;
} else {
end = mid - 1;
3
array[j] = array[j + 1];
array[j + 1] = temp;
Output 1:
12
134
19
314
123
1.Linear Search
2.Binary Search
Output 2:
4
123
12
34
45345
123
1.Linear Search
2.Binary Search
5
Assignment No: 2
Problem Statement: WAP to sort a list of elements. Give user the option to perform sorting using
Insertion sort, Bubble sort or Selection sort.
Program Listing:
#include <iostream>
int main() {
int n, choice;
cin >> n;
int arr[n];
6
cin >> choice;
switch (choice) {
case 1:
bubbleSort(arr, n);
break;
case 2:
selectionSort(arr, n);
break;
case 3:
insertionSort(arr, n);
7
break;
default:
return 0;
arr[j + 1] = temp;
int minIndex = i;
8
if (arr[j] < arr[minIndex]) {
minIndex = j;
arr[minIndex] = arr[i];
arr[i] = temp;
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
Output:
6364
9
1
231
352
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
10
Assignment No: 3
Problem Statement: Implement Linked List. Include functions for insertion, deletion and search of a
number, reverse the list.
Program Listing:
#include <iostream>
struct Node {
int data;
Node *next;
};
class list {
Node *head;
public:
void insert();
void remove();
void search();
void reverse();
void display();
};
void list::insert() {
11
temp = new Node;
if (head == NULL) {
head = temp;
temp->next = NULL;
} else {
current = head;
current = current->next;
current->next = temp;
temp->next = NULL;
void list::remove() {
if (head == NULL) {
return;
int i;
12
cin >> i;
prev = current;
current = current->next;
if (current == NULL) {
return;
if (prev == NULL) {
head = current->next;
} else {
prev->next = current->next; }
delete current;
void list::display() {
if (head == NULL) {
return;
13
}
Node *temp;
temp = head;
temp = temp->next;
void list::search() {
if (head == NULL) {
return;
Node *temp;
temp = head;
int item;
if (temp->data == item) {
return;
temp = temp->next;
14
}
void list::reverse() {
if (head == NULL) {
return;
current = head;
next = current->next;
prev = NULL;
current->next = prev;
prev = current;
current = next;
next = current->next;
head = prev;
int main() {
list l;
int choice;
15
while (1) {
switch (choice) {
case 1:
l.insert();
break;
case 2:
l.remove();
break;
case 3:
l.display();
break;
case 4:
l.search();
break;
case 5:
l.reverse();
break;
case 6:
exit(0);
default:
exit(0);
16
}
return 0;
Output:
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
List is empty
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
List is empty
1.Insert
17
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
Enter data: 12
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
18
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
12 134 1513
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
1513 134 12
19
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
1513 deleted
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
20
6.Exit
134 12
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
21
Assignment No: 4
Problem Statement: Implement Doubly Linked List. Include functions for insertion, deletion and search
of a number, reverse the list.
Program Listing:
#include <iostream>
struct NODE {
int data;
NODE *prev;
NODE *next;
};
class dlist {
NODE *head;
public:
void insert();
void remove();
void display();
void search();
void reverse();
};
void dlist::insert() {
22
NODE *temp, *current;
if (head == NULL) {
head = temp;
temp->prev = NULL;
temp->next = NULL;
} else {
current = head;
current = current->next;
current->next = temp;
temp->prev = current;
temp->next = NULL;
void dlist::remove() {
if (head == NULL) {
return;
23
current = head;
int item;
head = NULL;
delete current;
return;
current = current->next;
if (current == NULL) {
return;
if(current == head)
head = current->next;
head->prev = NULL;
else
24
temp = current->prev;
temp->next = current->next;
if(current->next != NULL)
current->next->prev = temp;
delete current;
void dlist::display() {
if (head == NULL) {
return;
NODE *temp;
temp = head;
temp = temp->next;
25
void dlist::search() {
if (head == NULL) {
return;
NODE *temp;
temp = head;
int item;
if (temp->data == item) {
return;
temp = temp->next;
void dlist::reverse() {
if (head == NULL) {
return;
26
NODE *current, *prevnode, *nextnode;
current = head;
nextnode = current->next;
prevnode = NULL;
current->next = prevnode;
prevnode = current;
prevnode->prev = nextnode;
current = nextnode;
nextnode = current->next;
head = prevnode;
int main() {
dlist l;
int choice;
while (1) {
switch (choice) {
case 1:
l.insert();
break;
27
case 2:
l.remove();
break;
case 3:
l.display();
break;
case 4:
l.search();
break;
case 5:
l.reverse();
break;
case 6:
exit(0);
default:
exit(0);
return 0;
Output:
1.Insert
2.Delete
28
3.Display
4.Search
5.Reverse
6.Exit
List is empty
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
Enter data: 11
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
Enter data: 12
29
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
Enter data: 13
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
11 12 13
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
30
Item found
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
12Deleted
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
1.Insert
2.Delete
3.Display
4.Search
31
5.Reverse
6.Exit
11 13
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
32
Assignment No: 5
Problem Statement: Implement Circular Linked List. Include functions for insertion, deletion and search
of a number, reverse the list.
Program Listing:
#include <iostream>
struct NODE {
int data;
NODE *next;
};
class clist {
NODE *head;
public:
void insert();
void remove();
void search();
void reverse();
void display();
};
void clist::insert() {
33
NODE *temp, *current;
if (head == NULL) {
head = temp;
temp->next = temp;
} else {
current = head;
current = current->next;
current->next = temp;
temp->next = head;
void clist::remove() {
if (head == NULL) {
return;
temp = head;
if (temp == temp->next) {
34
cout << temp->next << " has been deleted\n";
head = NULL;
delete temp;
} else {
temp = temp->next;
current = head;
current = current->next;
current->next = temp->next;
delete temp;
void clist::display() {
NODE *temp;
if (head == NULL) {
return;
temp = head;
do {
35
cout << temp->data << " ";
temp = temp->next;
void clist::search() {
if (head == NULL) {
return;
NODE *temp;
temp = head;
int item;
do {
if (temp->data == item) {
return;
temp = temp->next;
36
void clist::reverse() {
return;
current = head;
while (1) {
next = current->next;
current->next = prev;
prev = current;
current = next;
if (current == head) {
current->next = prev;
head = prev;
break;
int main() {
37
clist l;
int choice;
while (1) {
switch (choice) {
case 1:
l.insert();
break;
case 2:
l.remove();
break;
case 3:
l.display();
break;
case 4:
l.search();
break;
case 5:
l.reverse();
break;
case 6:
exit(0);
default:
38
cout << "Wrong Input\nExitting program\n";
exit(0);
return 0;
Output:
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
List is empty
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
39
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
40
11 12 13
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
List reversed
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
13 12 11
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
41
6.Exit
Item found
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
1.Insert
2.Delete
3.Display
4.Search
5.Reverse
6.Exit
Wrong Input
Exitting program
42
Assignment No: 6
Program Listing:
#include<iostream>
struct NODE
int data;
NODE *next;
};
class stack_ll
NODE *top;
public:
stack_ll()
top = NULL;
void pop();
void display();
43
void stack_ll::push(int x)
NODE *temp;
temp->data = x;
temp->next = top;
top = temp;
};
void stack_ll::pop()
if(top == NULL)
cout<<"Stack is empty\n";
return;
NODE *temp;
temp = top;
top = top->next;
delete temp;
void stack_ll::display()
44
{
if(top == NULL)
cout<<"Stack is empty\n";
return;
NODE *temp;
temp = top;
while(temp != NULL)
cout<<temp->data<<" ";
temp = temp->next;
int main()
stack_ll s;
int choice, x;
while(1)
cout<<"\n1.Push\n2.Pop\n3.Display\n4.Exit\n";
cin>>choice;
switch(choice)
45
{
case 1:
cin>>x;
s.push(x);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
exit(0);
default:
return 0;
Output:
1.Push
2.Pop
3.Display
46
4.Exit
Stack is empty
1.Push
2.Pop
3.Display
4.Exit
1.Push
2.Pop
3.Display
4.Exit
12
1.Push
2.Pop
3.Display
4.Exit
1.Push
47
2.Pop
3.Display
4.Exit
13 12
1.Push
2.Pop
3.Display
4.Exit
1.Push
2.Pop
3.Display
4.Exit
12
1.Push
2.Pop
3.Display
4.Exit
48
1.Push
2.Pop
3.Display
4.Exit
Stack is empty
49
Assignment No: 7
Program Listing:
#include<iostream>
class stack_arr
int stck[size];
public:
void pop();
void display();
};
void stack_arr::push(int x)
if(top == size-1)
cout<<"Stack is full\n";
return;
top++;
50
stck[top] = x;
void stack_arr::pop()
if(top == -1)
cout<<"Stack is empty\n";
return;
top--;
void stack_arr::display()
if(top == -1)
cout<<"Stack is empty\n";
return;
cout<<stck[i]<<" ";
51
}
int main()
stack_arr s;
int choice, x;
while(1)
cout<<"\n1.Push\n2.Pop\n3.Display\n4.Exit\n";
cin>>choice;
switch(choice)
case 1:
cin>>x;
s.push(x);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
52
exit(0);
default:
return 0;
Output:
1.Push
2.Pop
3.Display
4.Exit
Stack is empty
1.Push
2.Pop
3.Display
4.Exit
1.Push
2.Pop
53
3.Display
4.Exit
1.Push
2.Pop
3.Display
4.Exit
1343 34134
1.Push
2.Pop
3.Display
4.Exit
1.Push
2.Pop
3.Display
4.Exit
54
1.Push
2.Pop
3.Display
4.Exit
Stack is empty
1.Push
2.Pop
3.Display
4.Exit
Stack is empty
1.Push
2.Pop
3.Display
4.Exit
55
Assignment No: 8
Program Listing:
#include <iostream>
class Queue {
private:
public:
bool isFull() {
return true;
return false;
bool isEmpty() {
if (front == -1) {
return true;
return false;
56
}
if (isFull()) {
} else {
if (front == -1) {
front = 0;
items[rear] = value;
int dequeue() {
int item;
if (isEmpty()) {
item = -1;
} else {
item = items[front];
if (front == rear) {
} else {
57
cout << item << " dequeued from queue\n";
return item;
void display() {
if (isEmpty()) {
} else {
};
int main() {
Queue q;
while (1) {
switch (choice) {
58
case 1:
q.enqueue(data);
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
exit(0);
default:
exit(0);
return 0;
return 0;
Output:
1.Enqueue
2.Dequeue
59
3.Display
4.Exit
Queue is empty
1.Enqueue
2.Dequeue
3.Display
4.Exit
11 enqueued to queue
1.Enqueue
2.Dequeue
3.Display
4.Exit
12 enqueued to queue
1.Enqueue
2.Dequeue
3.Display
4.Exit
60
Enter your choice: 1
13 enqueued to queue
1.Enqueue
2.Dequeue
3.Display
4.Exit
11 12 13
1.Enqueue
2.Dequeue
3.Display
4.Exit
1.Enqueue
2.Dequeue
3.Display
4.Exit
61
12 13
1.Enqueue
2.Dequeue
3.Display
4.Exit
62
Assignment No: 9
Problem Statement: Create and perform different operations on Double-ended Queues using Linked List
implementation.
Program Listing:
#include <iostream>
struct node {
int data;
node *next;
};
class deque {
public:
void insertFront();
void insertRear();
void deleteFront();
void deleteRear();
void display();
};
void deque::insertFront() {
int x;
63
cout << "Enter data to insert at front: ";
cin >> x;
temp->data = x;
temp->next = front;
front = temp;
if (front == NULL) {
rear = temp;
void deque::insertRear() {
int x;
cin >> x;
temp->data = x;
temp->next = NULL;
if (rear == NULL) {
front = temp;
rear = temp;
} else {
rear->next = temp;
rear = temp;
64
}
void deque::deleteRear() {
if (rear == NULL) {
return;
if (front == rear) {
return;
temp = temp->next;
temp->next = NULL;
rear = temp;
void deque::deleteFront() {
if (front == NULL) {
return;
65
cout << front->data << " has been deleted\n";
if (front == rear) {
return;
front = front->next;
void deque::display() {
if (front == NULL) {
return;
temp = temp->next;
int main() {
deque obj;
while (1) {
66
cout << "\n1. Insert at front\n2. Insert at rear\n3. Delete from front\n4. "
int ch = 0;
switch (ch) {
case 1:
obj.insertFront();
break;
case 2:
obj.insertRear();
break;
case 3:
obj.deleteFront();
break;
case 4:
obj.deleteRear();
break;
case 5:
obj.display();
break;
case 6:
exit(0);
break;
default:
67
cout << "Invalid choice";
exit(0);
Output:
1. Insert at front
2. Insert at rear
5. Display
6. Exit
1. Insert at front
2. Insert at rear
5. Display
6. Exit
12
68
1. Insert at front
2. Insert at rear
5. Display
6. Exit
1. Insert at front
2. Insert at rear
5. Display
6. Exit
12 14
1. Insert at front
2. Insert at rear
5. Display
6. Exit
69
Enter data to insert at rear: 155
1. Insert at front
2. Insert at rear
5. Display
6. Exit
12 14 155
1. Insert at front
2. Insert at rear
5. Display
6. Exit
1. Insert at front
2. Insert at rear
5. Display
70
6. Exit
11 12 14 155
1. Insert at front
2. Insert at rear
5. Display
6. Exit
1. Insert at front
2. Insert at rear
5. Display
6. Exit
10 11 12 14 155
1. Insert at front
2. Insert at rear
71
4. Delete from rear
5. Display
6. Exit
1. Insert at front
2. Insert at rear
5. Display
6. Exit
11 12 14 155
1. Insert at front
2. Insert at rear
5. Display
6. Exit
1. Insert at front
72
2. Insert at rear
5. Display
6. Exit
11 12 14
1. Insert at front
2. Insert at rear
5. Display
6. Exit
73
Assignment No: 10
Problem Statement: WAP to scan a polynomial using linked list and add two polynomials.
Program Listing:
#include <iostream>
struct node {
int coeff;
int power;
node *next;
};
class Polynomial {
node *head;
public:
void read();
void display();
};
74
int coef;
int pow;
ptr1 = poli1.head;
ptr2 = poli2.head;
coef = ptr1->coeff;
pow = ptr1->power;
ptr1 = ptr1->next;
coef = ptr2->coeff;
pow = ptr2->power;
ptr2 = ptr2->next;
} else {
pow = ptr1->power;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
if (coef != 0) {
add_node_end(coef, pow);
if (ptr1 == NULL) {
add_node_end(ptr2->coeff, ptr2->power);
75
ptr2 = ptr2->next;
if (ptr2 == NULL) {
add_node_end(ptr1->coeff, ptr1->power);
ptr1 = ptr1->next;
temp->coeff = coef;
temp->power = powe;
temp->next = NULL;
if (head == NULL) {
head = temp;
} else {
current = head;
while(current->next != NULL)
current = current->next;
76
}
current->next = temp;
void Polynomial::read()
while(1)
cin>>coef;
cin>>pow;
add_node_end(coef, pow);
cin>>ch;
if(ch != 1)
break;
77
void Polynomial::display()
if(head != NULL)
if(temp->coeff != 0)
if(temp->coeff > 0)
cout<<" + ";
if(temp->coeff < 0)
temp->coeff = -temp->coeff;
if(temp->power == 0)
cout<< temp->coeff;
78
else if(temp->power == 1)
else
int main()
p1.read();
p2.read();
p3.add(p1, p2);
p1.display();
cout<<endl;
79
p2.display();
cout<<endl;
p3.display();
return 0;
Output:
80
Enter the power: 1
81
Assignment No: 11
Problem Statement: WAP to calculate factorial of a given no. (i) using recursion, (ii) using iteration
Program Listing:
#include <iostream>
class fact {
public:
int itterative(int);
int recursive(int);
};
int f = 1;
if (num < 0) {
return -1;
} else if (num == 0) {
return 1;
} else {
f = f * i;
return f;
82
}
if (num < 0) {
return -1;
} else if (num == 0) {
return 1;
} else {
int main() {
fact f;
switch (choice) {
case 1:
factorial = f.itterative(num);
if (factorial > 0)
83
break;
case 2:
factorial = f.recursive(num);
if (factorial > 0)
break;
default:
Output:
Enter a number: 6
1. ITERATIVE
2. RECURSIVE
6! = 720
84
Assignment No: 12
Problem Statement: WAP to display Fibonacci series (i) using recursion, (ii) using iteration.
Program Listing:
#include <iostream>
class FIBO {
public:
void fibo_itteration(int);
int fibo_recursion(int);
};
void FIBO::fibo_itteration(int n) {
int n1 = 0, n2 = 1, n3;
if (i == 1) {
continue;
if (i == 2) {
continue;
85
}
n3 = n1 + n2;
n1 = n2;
n2 = n3;
int FIBO::fibo_recursion(int n) {
if ((n == 1) || (n == 0)) {
return (n);
} else {
int main() {
FIBO obj;
86
switch (choice) {
case 1:
obj.fibo_itteration(num);
break;
case 2:
Output:
1. Itteration
2. Recursion
1, 1, 2, 3, 5,
87
Assignment No: 13
Problem Statement: WAP to calculate GCD of two numbers (i) with recursion (ii) without recursion.
Program Listing:
// GCD
#include <iostream>
class GCD {
public:
};
int x;
x = num1 % num2;
while (x != 0) {
num1 = num2;
num2 = x;
x = num1 % num2;
return num2;
88
int GCD::gcd_recursive(int num1, int num2) {
if (num2 != 0) {
} else {
return num1;
int main() {
GCD obj;
int choice;
switch (choice) {
case 1:
break;
case 2:
89
break;
default:
Output:
1. Using Itteration
2. Using Recursion
GCD is: 8
90
Assignment No: 14
Problem Statement:
WAP to create a Binary Search Tree and include following operations in tree:
Program Listing:
#include <iostream>
#include <stack>
struct Node {
int data;
Node* left;
Node* right;
91
};
class BST {
public:
Node* root;
BST() : root(nullptr) {}
// Insertion
// Deletion
// Search
// Traversals (Recursive)
// Traversals (Iterative)
92
void preorderIterative();
void inorderIterative();
void postorderIterative();
// Level-by-Level Traversal
void levelOrderTraversal();
// Counting Nodes
int countNonLeafNodes();
int countLeafNodes();
// Height
// Mirror Image
void createMirrorImage();
// Equality Check
private:
93
// Helper functions for level order traversal
};
if (node == nullptr) {
94
}
return node;
if (root == nullptr) {
return;
parent = current;
current = current->left;
} else {
current = current->right;
95
parent->left = new Node(value);
} else {
if (node == nullptr) {
return nullptr;
delete node;
node = nullptr;
96
delete node;
node = temp;
delete node;
node = temp;
node->data = temp->data;
return node;
node = node->left;
return node;
97
root = deleteNode(root, value);
node = node->right;
return node;
if (value == current->data) {
return true;
current = current->left;
} else {
current = current->right;
return false;
98
void BST::preorderRecursive(Node* node) {
if (node != nullptr) {
preorderRecursive(node->left);
preorderRecursive(node->right);
if (node != nullptr) {
inorderRecursive(node->left);
inorderRecursive(node->right);
if (node != nullptr) {
postorderRecursive(node->left);
postorderRecursive(node->right);
void BST::preorderIterative() {
99
if (root == nullptr) {
return;
stack<Node*> s;
s.push(current);
current = current->left;
if (!s.empty()) {
current = s.top();
s.pop();
current = current->right;
void BST::inorderIterative() {
if (root == nullptr) {
return;
100
}
stack<Node*> s;
s.push(current);
current = current->left;
if (!s.empty()) {
current = s.top();
s.pop();
current = current->right;
void BST::postorderIterative() {
if (root == nullptr) {
return;
101
Node* current = root;
stack<Node*> s;
s.push(current);
current = current->left;
if (!s.empty()) {
current = s.top();
current = current->right;
} else {
s.pop();
previous = current;
102
void BST::levelOrderTraversal() {
if (root == nullptr) {
return;
printLevel(root, i);
if (node == nullptr) {
return;
if (level == 1) {
int BST::countNonLeafNodes() {
103
return countNonLeafNodes(root);
return 0;
} else {
int BST::countLeafNodes() {
return countLeafNodes(root);
if (node == nullptr) {
return 0;
return 1;
} else {
104
int BST::height(Node* node) {
if (node == nullptr) {
return 0;
} else {
void BST::createMirrorImage() {
root = createMirrorImage(root);
if (node == nullptr) {
return nullptr;
node->left = createMirrorImage(node->right);
node->right = createMirrorImage(temp);
return node;
105
bool BST::isEqual(BST& other) {
return true;
return false;
return false;
} else {
int main() {
BST tree;
while (true) {
106
cout << "3. Delete by Copying\n";
switch (choice) {
case 1:
tree.insertRecursive(value);
break;
107
case 2:
tree.insertIterative(value);
break;
case 3:
tree.deleteByCopying(value);
break;
case 4:
tree.deleteByMerging(value);
break;
case 5:
if (tree.search(value)) {
} else {
break;
case 6:
108
cout << "Preorder (Recursive): ";
tree.preorderRecursive(tree.root);
break;
case 7:
tree.inorderRecursive(tree.root);
break;
case 8:
tree.postorderRecursive(tree.root);
break;
case 9:
tree.preorderIterative();
break;
case 10:
tree.inorderIterative();
break;
case 11:
109
cout << "Postorder (Iterative): ";
tree.postorderIterative();
break;
case 12:
tree.levelOrderTraversal();
break;
case 13:
break;
case 14:
break;
case 15:
break;
case 16:
tree.createMirrorImage();
break;
case 17: {
BST tree2;
110
int numElements;
cout << "Enter the number of elements for the second BST: ";
int val;
tree2.insertRecursive(val);
cout << "Trees are equal: " << (tree.isEqual(tree2) ? "Yes" : "No") << endl;
break;
case 18:
return 0;
default:
111
Assignment No: 15
Problem Statement: WAP to convert the Sparse Matrix into non-zero form and vice-versa.
Program Listing:
#include<iostream>
class sparse
int sparse_matrix[10][10];
int compact_matrix[3][100];
public:
void sparse_input();
void calculate_nonzero();
void calculate_compact_matrix();
void display_compact_matrix();
};
void sparse::sparse_input()
cin>>s_row;
cin>>s_col;
112
cout<<"Enter the matrix: \n";
cin>>sparse_matrix[i][j];
void sparse::calculate_nonzero()
for(int i=0;i<s_row;i++)
for(int j=0;j<s_col;j++)
if(sparse_matrix[i][j]!=0)
size_nonzero++;
void sparse::calculate_compact_matrix()
113
{
calculate_nonzero();
int k=0;
for(int i=0;i<s_row;i++)
for(int j=0;j<s_col;j++)
if(sparse_matrix[i][j]!=0)
compact_matrix[0][k]=i;
compact_matrix[1][k]=j;
compact_matrix[2][k]=sparse_matrix[i][j];
k++;
void sparse::display_compact_matrix()
cout<<"Row : ";
if(sparse_matrix[i][j]!=0)
114
cout<<i<<" ";
cout<<endl;
cout<<"Coloumn: ";
if(sparse_matrix[i][j]!=0)
cout<<j<<" ";
cout<<endl;
cout<<"Value : ";
if(sparse_matrix[i][j]!=0)
cout<<sparse_matrix[i][j]<<" ";
cout<<endl;
class compact
int compact_matrix[3][100];
int sparse_matrix[10][10];
public:
void compact_input();
void calculate_sparse_matrix();
115
void sparse_display();
};
void compact::compact_input()
cin>>size_nonzero;
cin>>compact_matrix[0][i];
cin>>compact_matrix[1][i];
cin>>compact_matrix[2][i];
void compact::calculate_sparse_matrix() {
int maxRow = 0;
int maxCol = 0;
maxRow = compact_matrix[0][i];
116
}
maxCol = compact_matrix[1][i];
sparse_matrix[i][j] = 0;
sparse_matrix[compact_matrix[0][i]][compact_matrix[1][i]] = compact_matrix[2][i];
void compact::sparse_display() {
117
cout << sparse_matrix[i][j] << " ";
int main()
int choice;
cin>>choice;
if(choice==1)
sparse s;
s.sparse_input();
s.calculate_compact_matrix();
s.display_compact_matrix();
else if(choice==2)
compact c;
c.compact_input();
c.calculate_sparse_matrix();
118
c.sparse_display();
else
cout<<"Wrong Input";
Output:
010
100
011
Row : 0 1 2 2
Coloumn: 1 0 1 2
Value : 1 1 1 1
119
Enter your choice: 2
Enter value: 1
Enter value: 1
Enter value: 1
Enter value: 1
010
100
011
120
Assignment No: 16
Problem Statement: WAP to reverse the order of the elements in the stack using additional stack.
Program Listing:
#include <iostream>
class stack {
public:
if (top == 99) {
return;
top++;
stack[top] = item;
int pop() {
int item;
if (top == -1) {
item = 0;
return item;
121
item = stack[top];
top--;
return item;
};
stack rev;
int item;
item = s.pop();
rev.push(item);
return rev;
int main() {
stack s;
int n, item;
cin >> n;
s.push(item);
122
}
s = reverse_stack(s, n);
Output:
123
Assignment No: 17
Problem Statement: WAP to reverse the order of the elements in the stack using additional Queue.
Program Listing:
#include <iostream>
class stack {
public:
if (top == 99) {
return;
top++;
stack[top] = item;
int pop() {
int item;
if (top == -1) {
item = 0;
return item;
124
item = stack[top];
top--;
return item;
};
class queue {
public:
if (rear == 99) {
return;
if (front == -1)
front = 0;
rear++;
queue[rear] = item;
int dequeue() {
int item;
125
item = 0;
return item;
item = queue[front];
front++;
return item;
};
queue rev;
int item;
item = s.pop();
rev.enqueue(item);
item = rev.dequeue();
s.push(item);
return s;
int main() {
stack s;
126
int n, item;
cin >> n;
s.push(item);
} // The original stack has the first input value at bottom and last inserted
// value at top
s = reverse_stack(s, n); // the reversed stack has the first inserted value at
Output:
127
Enter elements for the stack:
128
Assignment No: 18
Program Listing:
#include <iostream>
class matrix {
int *arr_1d;
int size;
public:
void input() {
void display_matrix() {
if (i == j) {
129
cout << arr_1d[i] << " ";
} else {
};
int main() {
matrix m;
m.input();
m.display_matrix();
Output:
130
10000
02000
00300
00040
00005
131
Assignment No: 19
Problem Statement: WAP to implement Lower Triangular Matrix using one-dimensional array.
Program Listing:
#include <iostream>
class lower_triangular {
int *arr_1d;
int size;
int total_size;
public:
void input_size() {
void total_input() {
total_size = 0;
total_size += i;
void array_input() {
total_input();
132
cout << "Enter the elements of the array in row major order: \n";
void display_matrix() {
int k = 0;
if (i >= j) {
} else {
};
int main() {
lower_triangular obj;
obj.input_size();
obj.array_input();
133
obj.display_matrix();
Output:
100
230
456
134
Assignment No: 20
Problem Statement: WAP to implement Upper Triangular Matrix using one-dimensional array.
Program Listing:
#include <iostream>
class upper_triangular {
int *arr_1d;
int size;
int total_size;
public:
void input_size() {
void total_input() {
total_size = 0;
total_size += i;
void array_input() {
total_input();
135
cout << "Enter the elements of the array in row major order: \n";
void display_matrix() {
int k = 0;
if (i <= j) {
} else {
};
int main() {
upper_triangular obj;
obj.input_size();
obj.array_input();
136
obj.display_matrix();
Output:
123
045
006
137
Assignment No: 21
#include <iostream>
class SymmetricMatrix {
private:
int *data;
int size;
swap(row, col);
public:
} else {
138
cerr << "Index out of bounds.\n";
} else {
return 0;
};
int main() {
int n;
139
cout << "Enter the size of the symmetric matrix: ";
cin >> n;
SymmetricMatrix matrix(n);
int value;
cout << "Enter value for (" << i << ", " << j << "): ";
matrix.set(i, j, value);
matrix.print();
return 0;
Output:
140
Enter value for (0, 1): 2
Symmetric Matrix:
123
245
356
141