Name 5
Name 5
#include<iostream>
#include<string>
#include <iomanip>
using namespace std;
int const MaxStudent = 15;
struct student{
int roll_no;
string name;
float sgpa; };
void input(struct student s[]){
for(int i=0;i<MaxStudent;i++){
cout<<"\nEnter the Roll number of the student : ";
cin>>s[i].roll_no;
cout<<"\nEnter the Name of Roll No. "<< s[i].roll_no <<" : ";
cin>>s[i].name;
cout<<"\nEnter the SGPA of Roll No. "<< s[i].roll_no <<" : "; cin>>s[i].sgpa;
}}
void display(struct student list[MaxStudent ]){
cout<<"\n"; cout<<"---------------------------------------------------\n";
cout <<left<<setw(10)<<"Sr.No."<<left<<setw(10)<<"Roll No."<<left <<setw(25)<< "Student Name"<<left<<setw(15)<<"SGPA"
<<endl;
cout<<"---------------------------------------------------";
cout<<"\n"; for(int i=0; i<MaxStudent; i++){
cout <<left<<setw(10)<< i+1 <<left<<setw(10)<< list[i].roll_no <<left <<setw(25)<< list[i].name<<left<<setw(15)<<list[i].sgpa
<<endl;
}}
void bubble_sort (struct student s[]){
for(int k = 0; k < MaxStudent; k++){
for (int j = 0; j < (MaxStudent - 1); j++){
if (s[j].roll_no > s[j+1].roll_no){
struct student temp = s[j];
s[j] = s[j+1];
s[j+1] = temp; }}}
cout << "\n\t\t* Bubble sort : Roll No. *" << endl; display(s);
}
void insertion_sort(struct student s[MaxStudent]){
for (int a = 1; a < MaxStudent ; a++){
int key = s[a].roll_no;
string key1 = s[a].name; float key2 = s[a].sgpa;
int b = a - 1;
while((b>=0) && (s[b].name > key1)){
s[b+1].roll_no = s[b].roll_no;
s[b+1].name = s[b].name;
s[b+1].sgpa = s[b].sgpa;
b = b - 1; }
s[b+1].roll_no = key;
s[b+1].name = key1;
s[b+1].sgpa = key2;
}
cout << "\n\t\t* Insertion sort : Name *" << endl; display(s);
}
void quick_sort(struct student s[], int MaxStudent , int first, int last){
int p, i, j; struct student temp; p = i = first; j = last;
if(first < last) {
while(i < j){
while(s[i].sgpa>=s[p].sgpa && i < last) i++;
while(s[j].sgpa < s[p].sgpa) j--;
if(i < j){
temp = s[i];
s[i] = s[j];
s[j] = temp; } }
temp = s[j];
s[j] = s[p];
s[p] = temp;
quick_sort(s,MaxStudent ,first,j-1);
quick_sort(s,MaxStudent, j+1, last);
}}
void search_sgpa(struct student list[MaxStudent ]){
float sgpa; int z = 0;
cout << "\nEnter SGPA : " ; cin >> sgpa;
cout<<"\n"; cout<<"--------------------------------------------------------\n";
cout <<left<<setw(10)<<"Sr.No."<<left<<setw(10)<<"Roll No."<<left <<setw(25)<< "Student Name"<<left<<setw(15)<<"SGPA"
<<endl; cout<<"--------------------------------------------------------"; cout<<"\n"; for (int i = 0; i < MaxStudent ;i++)
{
if ((i<MaxStudent) && (list[i].sgpa == sgpa))
{
cout <<left<<setw(10)<< i+1 <<left<<setw(10)<< list[i].roll_no <<left <<setw(25)<< list[i].name<<left<<setw(15)<<list[i].sgpa <<endl;
continue;
} } }
int binary_search(struct student s[])
{
insertion_sort(s); string key; cout << "\nEnter the Name you want to search : " ; cin >> key; int low = 0; int high = MaxStudent - 1;
while(low <= high){ int mid = (low + high)/2;
cout<<"\n##### MENU #####"<<endl; cout<<"\n1. Bubble sort (To sort Roll Number)\n2. Insertion Sort (To sort Student
Name)\n3. Quick Sort (To find Top 10 rank)\n4. Linear Search (To find Students with Same SGPA)\n5. Binary Search (To find Student
Name form database)"<<endl;
cout<<"\nSelect the Operation : "; cin >> choice;
switch(choice)
{
case 1:
bubble_sort(s); break;
case 2:
insertion_sort(s); break;
case 3: cout << "\n\t\t* Quick sort : SGPA *" << endl;
quick_sort(s,MaxStudent,0,MaxStudent -1);
view(s,10); break;
case 4:
search_sgpa(s); break;
case 5: {
int result = binary_search(s);
if (result == -1){
cout << "Data not found!!\n";
}
else{
cout << "Name is found at Sr. no. : " << result+1 << endl;
} }
break;
default:
cout<<"Invalid Input !!"<<endl;
}
cout << "\nEnter 0 to EXIT or Enter 1 to RETURN MENU : " ;
cin >> cont ; if(cont!=1){ cout<<"Thank you !!"; break;
}
}while(cont<=5);
}
Practical No : 2
#include <iostream>
#include <cctype>
#include <cstring>
char data;
} node;
class stack {
node *top;
public:
stack() {
top = NULL;
bool isempty() {
void push(char x) {
p->data = x;
p->next = top;
top = p;
char pop() {
if (isempty()) {
return -1;
node *p = top;
char x = p->data;
top = top->next;
delete p;
return x;
char topdata() {
if (!isempty())
return top->data;
else
return -1;
};
int main() {
int ch;
do {
case 1:
infix_postfix(infix, postfix);
break;
case 2:
infix_prefix(infix, prefix);
break;
case 3:
evaluate_postfix(postfix);
break;
case 4:
evaluate_prefix(prefix);
break;
case 5:
break;
default:
stack s;
int i, j = 0;
char token, x;
token = infix[i];
if (isalnum(token)) {
postfix[j++] = token;
s.push(token);
postfix[j++] = x;
} else {
postfix[j++] = s.pop();
s.push(token);
while (!s.isempty()) {
postfix[j++] = s.pop();
postfix[j] = '\0';
int i, j = 0;
b[j++] = ')';
b[j++] = '(';
else
b[j++] = a[i];
b[j] = '\0';
reverse(infix, infix1);
infix_postfix(infix1, prefix1);
reverse(prefix1, prefix);
int precedence(char x) {
if (x == '(')
return 0;
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/')
return 2;
return 3;
switch (op) {
stack s;
char token;
token = postfix[i];
if (isalnum(token)) {
int value;
cout << "Enter value for " << token << ": ";
s.push(value);
} else {
op2 = s.pop();
op1 = s.pop();
s.push(result);
result = s.pop();
stack s;
char token;
token = prefix[i];
if (isalnum(token)) {
int value;
cout << "Enter value for " << token << ": ";
s.push(value);
} else {
op1 = s.pop();
op2 = s.pop();
s.push(result);
result = s.pop();
}
Practical No : 3
#include <iostream>
using namespace std;
int cqueue[5];
int front = -1, rear = -1, n = 5;
void deleteCQ() {
if (front == -1) {
cout << "Queue Underflow\n";
return;
}
cout << "Element deleted from queue is: " << cqueue[front] << endl;
if (front == rear) {
front = -1;
rear = -1;
} else if (front == n - 1) {
front = 0;
} else {
front = front + 1;
}
}
void displayCQ_forward() {
if (front == -1) {
cout << "Queue is empty\n";
return;
}
cout << "Queue elements are: ";
if (front <= rear) {
for (int i = front; i <= rear; i++) {
cout << cqueue[i] << " ";
}
} else {
for (int i = front; i < n; i++) {
cout << cqueue[i] << " ";
}
for (int i = 0; i <= rear; i++) {
cout << cqueue[i] << " ";
}
}
cout << endl;
}
void displayCQ_reverse() {
if (front == -1) {
cout << "Queue is empty\n";
return;
}
cout << "Queue elements in reverse are: ";
if (front <= rear) {
for (int i = rear; i >= front; i--) {
cout << cqueue[i] << " ";
}
} else {
for (int i = rear; i >= 0; i--) {
cout << cqueue[i] << " ";
}
for (int i = n - 1; i >= front; i--) {
cout << cqueue[i] << " ";
}
}
cout << endl;
}
int main() {
int ch, val;
cout << "1) Insert\n";
cout << "2) Delete\n";
cout << "3) Display forward\n";
cout << "4) Display reverse\n";
cout << "5) Exit\n";
do {
cout << "Enter choice: ";
cin >> ch;
switch (ch) {
case 1:
cout << "Input for insertion: ";
cin >> val;
insertCQ(val);
break;
case 2:
deleteCQ();
break;
case 3:
displayCQ_forward();
break;
case 4:
displayCQ_reverse();
break;
case 5:
cout << "Exited\n";
break;
default:
cout << "Incorrect Choice, select from above only!\n";
}
} while (ch != 5);
return 0;
}
Practical No : 4
#include <iostream>
#include <cstring> // for handling C strings
using namespace std;
struct Node {
char data;
Node *left, *right;
};
struct StackNode {
Node* data;
StackNode *next;
};
class Stack {
StackNode *top;
public:
Stack() : top(nullptr) {}
Node* topElement() {
return top ? top->data : nullptr;
}
bool isEmpty() {
return top == nullptr;
}
Node* pop() {
if (isEmpty())
return nullptr;
StackNode *temp = top;
Node* node = top->data;
top = top->next;
delete temp;
return node;
}
};
if (isalnum(prefix[i])) {
s.push(newNode);
} else {
newNode->left = s.pop();
newNode->right = s.pop();
s.push(newNode);
}
}
return s.pop();
}
int main() {
Node *root = nullptr;
char postfix[1000], prefix[1000];
int choice, ch;
do {
cout << "\n1. Construct tree from postfix/prefix expression\n"
<< "2. Inorder traversal\n"
<< "3. Preorder traversal\n"
<< "4. Postorder traversal\n"
<< "5. Exit\nEnter your choice: ";
cin >> ch;
switch (ch) {
case 1:
cout << "1. Postfix expression\n2. Prefix expression\nEnter choice: ";
cin >> choice;
if (choice == 1) {
cout << "Enter postfix expression: ";
cin >> postfix;
root = createTreeFromPostfix(postfix);
} else {
cout << "Enter prefix expression: ";
cin >> prefix;
root = createTreeFromPrefix(prefix);
}
cout << "Tree created successfully.\n";
break;
case 2:
cout << "Inorder traversal: ";
inorder(root);
cout << "\nInorder traversal (non-recursive): ";
inorderNonRecursive(root);
cout << endl;
break;
case 3:
cout << "Preorder traversal: ";
preorder(root);
cout << "\nPreorder traversal (non-recursive): ";
preorderNonRecursive(root);
cout << endl;
break;
case 4:
cout << "Postorder traversal: ";
postorder(root);
cout << "\nPostorder traversal (non-recursive): ";
postorderNonRecursive(root);
cout << endl;
break;
case 5:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice.\n";
}
} while (ch != 5);
return 0;
}
Practical No : 5
#include <iostream>
struct bstnode {
int data;
};
temp->data = value;
return temp;
if (root != nullptr) {
inorder(root->left);
inorder(root->right);
if (node == nullptr)
return newNode(key);
else
return node;
}
return root;
current = current->left;
return current;
if (root == nullptr)
return root;
else {
if (root->left == nullptr) {
delete root;
return temp;
return temp;
root->data = temp->data;
return root;
if (node == nullptr)
return;
mirror(node->left);
mirror(node->right);
node->left = node->right;
node->right = temp;
if (root == nullptr)
return nullptr;
newRoot->left = copy(root->left);
newRoot->right = copy(root->right);
return newRoot;
if (root == nullptr)
return 0;
int main() {
while (true) {
switch (choice) {
case 1:
cin >> n;
break;
case 2:
break;
case 3:
else
break;
case 4:
inorder(root);
break;
case 5:
depth = maxDepth(root);
cout << "\nThe depth of the BST is: " << depth << endl;
break;
case 6:
rootCopy = copy(root);
inorder(rootCopy);
break;
case 7:
mirror(root);
inorder(root);
break;
case 8:
return 0;
default:
return 0;
}
Practical No : 6
#include <iostream>
using namespace std;
class Node {
public:
int data;
int lth, rth; // Left and right thread flags
Node *left, *right;
};
class ThreadedBinaryTree {
private:
Node *dummy;
Node *root;
public:
ThreadedBinaryTree();
void create();
void insert(Node *root, Node *newNode);
void display();
};
ThreadedBinaryTree::ThreadedBinaryTree() {
root = nullptr;
dummy = new Node;
dummy->data = -999;
dummy->left = dummy->right = dummy;
dummy->lth = dummy->rth = 1;
}
void ThreadedBinaryTree::create() {
Node *newNode = new Node;
newNode->left = nullptr;
newNode->right = nullptr;
newNode->lth = 0;
newNode->rth = 0;
if (root == nullptr) {
root = newNode;
dummy->left = root;
root->left = dummy;
root->right = dummy;
} else {
insert(root, newNode);
}
}
void ThreadedBinaryTree::display() {
if (root == nullptr) {
cout << "Tree is not created" << endl;
} else {
cout << "\nInorder Traversal: ";
inorderTraversal(root, dummy);
cout << "\nPreorder Traversal: ";
preorderTraversal(root, dummy);
}
}
int main() {
int choice;
char ans = 'N';
ThreadedBinaryTree tree;
do {
cout << "\n\tProgram for Threaded Binary Tree";
cout << "\n1. Create\n2. Display\n";
cin >> choice;
switch (choice) {
case 1:
do {
tree.create();
cout << "\nDo you want to enter more elements? (y/n): ";
cin >> ans;
} while (ans == 'y');
break;
case 2:
tree.display();
break;
default:
cout << "\nInvalid choice!";
}
return 0;
}
Practical No : 8
#include <iostream>
using namespace std;
class MST {
int a[20][20], n, k;
struct gr {
int v1;
int v2;
int wt;
} g[20];
public:
void accept();
void extract_edges();
void prims();
};
void MST::accept() {
int i, j;
cout << "\nEnter the number of vertices: ";
cin >> n;
cout << "Enter adjacency matrix:\n";
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> a[i][j];
}
void MST::extract_edges() {
int i, j;
for (i = 0, k = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[i][j] != 0) {
g[k].v1 = i;
g[k].v2 = j;
g[k++].wt = a[i][j];
}
}
}
void MST::prims() {
int i, j, min_edge, visited[20] = {0}, sum = 0, min, flag;
visited[0] = 1; // Start from vertex 0
cout << "\nTotal cost of MST: " << sum << "\n";
}
int main() {
MST m;
m.accept();
m.extract_edges();
m.prims();
return 0;
}
Practical No : 8
#include <iostream>
#define max 20
class graph {
public:
graph(int m) {
n = m;
visit_dist = 0;
client_dist = 0;
min_dist = 0;
g[i][j] = 0;
void accept_v();
void accept_e();
void display();
void dj_init(int);
void dj_init();
};
void graph::accept_v() {
int i = 0;
while (i < n) {
cout << "\nEnter Name of City : [" << i + 1 << "] :: ";
i++;
void graph::accept_e() {
int i, j, cst;
if (i == j) {
g[i][j] = 0;
continue;
} else {
cout << "\n\tRent between cities [" << v[i] << "][" << v[j] << "] : ";
void graph::display() {
int i, j;
void graph::dj_init() {
int i, j;
c[i] = 9999;
ch[i] = 0;
str[i][j] = '-';
void graph::dj_init(int i) {
str[i][j] = '-';
dj_init();
int i, j, l, k, flag, min = 999, cst = 0;
i = 0;
c[i] = 0;
k = 0;
str[i][k] = v[i];
do {
ch[i] = 1;
min = 999;
flag = 0;
k = 0;
c[j] = cst;
dj_init(j);
while (flag == 0) {
str[j][k] = str[i][k];
k++;
str[j][k] = v[j];
min = c[l];
i = l;
}
j = 0;
if (f == 0) min_dist = c[i];
if (f == 0) {
cout << "\nMinimum Distance : " << c[i] << "\nShortest Path: ";
j++;
min_path[j] = '\0';
j = 1;
client_path[j - 1] = str[i][j];
j++;
client_path[j - 1] = '\0';
int main() {
int n, ch;
cin >> n;
graph g(n);
do {
cout << "\nEnter Your Choice: \n1. Accept City Name\n2. Accept Routes\n3. Display\n4. Shortest Distance\n5. Exit\nInput--> ";
switch (ch) {
case 1:
g.accept_v();
break;
case 2:
g.accept_e();
break;
case 3:
g.display();
break;
case 4:
break;
return 0;
}
Practical No : 9
#include <iostream>
int left = 2 * i;
int right = 2 * i + 1;
int largest = i;
largest = left;
largest = right;
if (largest != i) {
swap(a[i], a[largest]);
MaxHeapify(a, i, n);
swap(a[1], a[i]);
MaxHeapify(a, 1, i - 1);
MaxHeapify(a, i, n);
}}
int main() {
int n;
cin >> n;
HeapSort(arr, n);
return 0;
}
Practical No : 10
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
class Student {
int rollno;
char div;
int year;
public:
Student() {
strcpy(name, "");
strcpy(city, "");
Student(int rollno, char name[MAX], int year, char div, char city[MAX]) {
this->rollno = rollno;
strcpy(this->name, name);
strcpy(this->city, city);
this->year = year;
this->div = div;
}
int getRollNo() {
return rollno;
void displayRecord() {
cout << endl << setw(5) << rollno << setw(20) << name << setw(5) << year << setw(5) << div << setw(10) << city;
};
class FileOperations {
fstream file;
public:
if (!file) {
exit(1);
void insertRecord(int rollno, char name[MAX], int year, char div, char city[MAX]) {
file.seekp(0, ios::end);
file.clear();
void displayAll() {
Student s1;
file.seekg(0, ios::beg);
s1.displayRecord();
file.clear();
Student s1;
file.seekg(0, ios::beg);
if (s1.getRollNo() == rollNo) {
s1.displayRecord();
flag = true;
break;
if (!flag) {
cout << "\nRecord of Roll No " << rollNo << " is not present." << endl;
file.clear();
file.seekg(0, ios::beg);
bool flag = false;
Student s1;
if (s1.getRollNo() == rollno) {
flag = true;
continue;
if (!flag) {
cout << "\nRecord of Roll No " << rollno << " is not present." << endl;
file.close();
outFile.close();
remove("student.dat");
rename("new.dat", "student.dat");
~FileOperations() {
file.close();
};
int main() {
FileOperations file("student.dat");
char div;
while (choice != 5) {
switch (choice) {
case 1:
case 2:
cout << endl << setw(5) << "ROLL" << left << setw(20) << " NAME" << setw(5) << "YEAR" << setw(5) << "DIV" <<
setw(10) << "CITY" << endl;
file.displayAll();
break;
case 3:
file.displayRecord(rollNo);
break;
case 4:
file.deleteRecord(rollNo);
break;
case 5:
break;
default:
return 0;