Name
Name
Roll-No :- 207B073
Asingnment No :- 1
#include <iostream>
struct Student {
int roll_no;
string name;
float sgpa;
};
Student students[] = {
};
cout << "Roll No: " << students[i].roll_no << ", Name: "
<< students[i].name << ", SGPA: " << students[i].sgpa << endl;
}}
int j = i - 1;
students[j + 1] = students[j];
j = j - 1; }
students[j + 1] = key; }}
int i = low - 1;
i++;
swap(students[i], students[j]); } }
return i + 1;}
quickSortBySGPA(students, pi + 1, high); }}
quickSortBySGPA(students, 0, n - 1);
cout << "Roll No: " << students[i].roll_no << ", Name: "
<< students[i].name << ", SGPA: " << students[i].sgpa << endl; }}
if (students[i].sgpa == sgpa) {
cout << "Roll No: " << students[i].roll_no << ", Name: "
<< students[i].name << ", SGPA: " << students[i].sgpa << endl;
found = true; }}
if (!found) cout << "No students found with SGPA " << sgpa << endl;}
if (students[mid].name == name) {
cout << "Roll No: " << students[mid].roll_no << ", Name: "
<< students[mid].name << ", SGPA: " << students[mid].sgpa << endl;
found = true;
break;
left = mid + 1;
} else {
right = mid - 1; } }
if (!found) cout << "Student with name " << name << " not found." << endl;
int main() {
bubbleSortByRollNo(students, n);
printStudents(students, n);
// b) Sort by Name
printStudents(students, n);
printTop10Students(students, n);
// d) Search by SGPA
float sgpaToSearch;
searchBySGPA(students, n,
sgpaToSearch);
string nameToSearch;
binarySearchByName(students,
n, nameToSearch);
return 0;
}
Name :- Mhaske Suraj Machhindra
Roll-No :- 207B073
Asingnment No :- 2
#include <iostream>
struct Node {
char data;
Node* next;
};
class Stack {
private:
Node* top;
public:
newNode->data = value;
newNode->next = top;
top = newNode; }
char pop() {
if (top == nullptr) {
return -1; }
top = top->next;
delete temp;
return value; }
char peek() {
if (top != nullptr)
return top->data;
else
return -1; }
bool isEmpty() {
bool isOperator(char c) {
return 0;}
Stack stack;
if (isalnum(ch)) {
postfix += ch;
stack.push(ch);
postfix += stack.pop();}
} else if (isOperator(ch)) {
postfix += stack.pop();
}
stack.push(ch); } }
while (!stack.isEmpty()) {
postfix += stack.pop();}
return postfix;}
reverse(infix.begin(), infix.end());
reverse(postfix.begin(), postfix.end());
return postfix;}
stack<int> stack;
if (isdigit(ch)) {
stack.push(ch - '0');
} else if (isOperator(ch)) {
switch (ch) {
return stack.top();}
stack<int> stack;
reverse(prefix.begin(), prefix.end());
for (char ch : prefix) {
if (isdigit(ch)) {
stack.push(ch - '0');
} else if (isOperator(ch)) {
switch (ch) {
return stack.top();}
int main() {
string infix;
return 0;
}
Name :- Mhaske Suraj Machhindra
Roll-No :- 207B073
Asingnment No :- 3
#include <iostream>
return;
front = 0;
rear = 0;
} else {
if (rear == n - 1)
else
rear = rear + 1;
cqueue[rear] = val;
void deleteCQ() {
if (front == -1) {
return;
}
cout << "Element deleted from queue is : " << cqueue[front] << endl;
front = -1;
rear = -1;
} else {
if (front == n - 1)
else
front = front + 1;
void displayCQ_forward() {
if (front == -1) {
return;
} else {
}}
void displayCQ_reverse() {
if (front == -1) {
return;
} else {
}}
int main() {
do {
switch (ch) {
case 1:
cout << "Input for insertion: ";
insertCQ(val);
break;
case 2:
deleteCQ();
break;
case 3:
displayCQ_forward();
break;
case 4:
displayCQ_reverse();
break;
case 5:
break;
default:
return 0;
}
Name :- Mhaske Suraj Machhindra
Roll-No :- 207B073
Asingnment No :- 4
#include<iostream>
char data;
}node;
node* data;
}stacknode;
class stack
stacknode *top;
public:
stack()
top=NULL;}
node* topp()
return (top->data);}
int isempty()
if(top==NULL)
return 1;
return 0;
}
stacknode *p;
p=new stacknode();
p->data=a;
p->next=top;
top=p;}
node* x;
x=top->data;
p=top;
top=top->next;
return x;}};
{node *p;
stack s;
for(int i=0;postfix[i]!='\0';i++)
char token=postfix[i];
if(isalnum(token))
p=new node();
p->data=token;
p->left=NULL;
p->right=NULL;
s.push(p); }
else{
p=new node();
p->data=token;
p->right=s.pop();
p->left=s.pop();
s.push(p); }}
return s.pop();}
{node *p;
stack s;
int i;
for(i=0;prefix[i]!='\0';i++)
{}
i=i-1;
for(;i>=0;i--){
char token=prefix[i];
if(isalnum(token)){
p=new node();
p->data=token;
p->left=NULL;
p->right=NULL;
s.push(p);
} else{
p=new node();
p->data=token;
p->left=s.pop();
p->right=s.pop();
s.push(p); }}
return s.pop();}
int main(){
node *r=NULL,*r1;
char postfix[10],prefix[10];
int x;
int ch,choice;
do
cin>>ch;
switch(ch)
cin>>choice;
if(choice==1){
cin>>postfix;
r=create_post(postfix);}
else{
cin>>prefix;
r=create_pre(prefix);}
break;
inorder(r);
inorder_non_recursive(r);
break;
preorder_non_recursive(r);
break;
postorder(r);
postorder_non_recursion(r);
break;}
}while(ch!=5);
return 0;}
if(p!=NULL){
inorder(p->left);
cout<<p->data;
inorder(p->right); }}
if(p!=NULL){
cout<<p->data;
preorder(p->left);
preorder(p->right); }}
if(p!=NULL){
postorder(p->left);
postorder(p->right);
cout<<p->data; }}
stack s;
while(t!=NULL){
s.push(t);
t=t->left;}
while(s.isempty()!=1){
t=s.pop();
cout<<t->data;
t=t->right;
while(t!=NULL){
s.push(t);
t=t->left;}}}
stack s;
while(t!=NULL){
cout<<t->data;
s.push(t);
t=t->left;}
while(s.isempty()!=1){
t=s.pop();
t=t->right;
while(t!=NULL){
cout<<t->data;
s.push(t);
t=t->left;}}}
{stack s,s1;
node *t1;
while(t!=NULL){
s.push(t);
s1.push(NULL);
t=t->left; }
while(s.isempty()!=1){
t=s.pop();
t1=s1.pop();
if(t1==NULL){
s.push(t);
s1.push((node *)1);
t=t->right;
while(t!=NULL){
s.push(t);
s1.push(NULL);
t=t->left; }}
else{
cout<<t->data; }}
Name :- Mhaske Suraj Machhindra
Roll-No :- 207B073
Asingnment No :- 5
#include<iostream>
int data;
struct tnode*left;
struct tnode*right;
}tnode;
struct tnode*x;
}node;
class queue {
node *front,*rear;
public:
queue() {
front=NULL;
rear=NULL; }
int isempty() {
if(front==NULL)
return 1;
return 0; }
node *p;
p=new node();
p->x=i;
p->next=NULL;
if(front==NULL) {
front=p;
rear=p; }
else {
rear->next=p;
rear=rear->next; } }
tnode *deque() {
node *p;
tnode *temp;
p=front;
temp=front->x;
if(front==rear) {
front=NULL;
rear=NULL; }
else {
front=front->next; }
delete p;
return temp;
} };
class tree{
tnode *t;
public:
tree(){
t=NULL; }
tnode *insert(int x) {
tnode *p,*q,*r;
p=new tnode();
p->data=x;
p->left=NULL;
p->right=NULL;
if(t==NULL)
return p;
q=t;
r=t;
while(r!=NULL) {
q=r;
if(x<r->data)
r=r->left;
else
r=r->right; }
if(x<q->data)
q->left=p;
else
q->right=p;
return t; }
tnode *create() {
int n,i,key;
cin>>n;
for(i=0;i<n;i++) {
cin>>key;
t=insert(key); }
return t; }
if(t!=NULL) {
inorder(t->left);
cout<<"\t"<<t->data;
inorder(t->right); } }
tnode *s=t;
while(s!=NULL) {
if(s->data==key)
return t;
else if(s->data<key)
s=s->right;
else
s=s->left; }
return NULL; }
while(r->left!=NULL) {
r=r->left; }
return r; }
tnode *temp;
if(t==NULL) {
return NULL; }
if(key<t->data) {
t->left=del(t->left,key);
return t; }
if(key>t->data) {
t->right=del(t->right,key);
return t; }
//element found
//no child
if(t->left==NULL&t->right==NULL) {
temp=t;
delete temp;
return NULL; }
//one child
if(t->left!=NULL&&t->right==NULL) {
temp=t;
t=t->left;
delete temp;
return t; }
if(t->left==NULL&&t->right!=NULL) {
temp=t;
t=t->right;
delete temp;
return t; }
temp=find_min(t->right);
t->data=temp->data;
t->right=del(t->right,temp->data);
return t; }
tnode *temp;
if(t==NULL) {
return NULL; }
temp=t->left;
t->left=mirror(t->right);
t->right=mirror(temp);
return t; }
tnode *P;
P=NULL;
if(T!=NULL) {
P=new tnode();
P->data=T->data;
P->left=copy(T->left);
P->right=copy(T->right); }
return P; }
int hl,hr;
if(T==NULL)
return 0;
return 0;
hl=height(T->left);
hr=height(T->right);
if(hl>hr)
return 1+hl;
else
return 1+hr; }
if(T==NULL)
return ;
{ cout<<"\t"<<T->data;
leaf(T->left);
leaf(T->right); }
if(T==NULL)
return ;
cout<<"\t"<<T->data;
cout<<"\t"<<T->left->data;
cout<<"\n"; }
cout<<"\t"<<T->data;
cout<<"\t"<<T->right->data;
cout<<"\n"; }
cout<<"\t"<<T->data;
cout<<"\t"<<T->left->data<<"\t"<<T->right->data;
cout<<"\n"; }
parent(T->left);
parent(T->right); }
void level_wise() {
tnode *t1;
queue q1;
if(t==NULL)
return;
q1.enque(t);
cout<<"\n"<<t->data;
while(q1.isempty()!=1) {
cout<<"\n";
queue q2;
while(q1.isempty()!=1) {
t1=q1.deque();
if(t1->left!=NULL) {
q2.enque(t1->left);
cout<<" "<<t1->left->data; }
if(t1->right!=NULL) {
q2.enque(t1->right);
cout<<" "<<t1->right->data; } }
q1=q2; }}};
int main(){
tree t;
do {
cin>>choice;
switch(choice) {
cin>>key;
root=t.insert(key); break;
t.inorder(root); break;
cin>>key;
result=t.search(key);
if(result==NULL) {
else{
break;
cin>>key;
result=t.del(root,key);
root=result;
case 6:root=t.mirror(root);
t.inorder(root);
break;
break;
rt=t.copy(root);
t.inorder(rt); break;
case 8:cnt=t.height(root);
break;
case 9:result=t.find_min(root);
break;
t.level_wise();
break;
t.leaf(root);
break;
t.parent(root);
break;
case 13:return 0;
}while(choice!=13); }
Name :- Mhaske Suraj Machhindra
Roll-No :- 207B073
Asingnment No :- 6
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
node->data = data;
node->isThreaded = false;
return node; }
while (current) {
parent = current;
if (current->left)
current = current->left;
else
break;
} else {
if (!current->isThreaded)
current = current->right;
else
break; } }
parent->left = node;
node->right = parent;
node->isThreaded = true;
} else {
node->right = parent->right;
node->isThreaded = true;
parent->right = node;
parent->isThreaded = false;
return root; }
current = current->left; }
while (current) {
if (current->isThreaded) {
current = current->right;
} else {
current = current->right;
current = current->left; } } }}
while (current) {
if (current->left) {
current = current->left;
} else {
current = current->right;
if (current) {
current = current->right; } } }}
int main() {
inorder(root);
cout << endl;
preorder(root);
return 0;
}
Name :- Mhaske Suraj Machhindra
Roll-No :- 207B073
Asingnment No :- 7
#include <iostream>
struct Edge {
class Graph {
public:
Graph(int V) : V(V) {
adjList.resize(V);
adjList[u].push_back({v, weight});
adjList[v].push_back({u, weight});
edges.push_back({u, v, weight});
void kruskalMST() {
vector<int> parent(V);
for (int i = 0; i < V; ++i) parent[i] = i;
if (v == parent[v]) return v;
};
if (rootU != rootV) {
mst.push_back(edge);
unionSets(edge.src, edge.dest);
int totalWeight = 0;
cout << e.src << " - " << e.dest << " : " << e.weight << endl;
totalWeight += e.weight;
cout << "Total Weight of MST: " << totalWeight << endl;
}
// Function to perform Prim's Algorithm for MST
void primMST() {
vector<int> key(V, INT_MAX); // Key values used to pick minimum weight edge
while (!pq.empty()) {
pq.pop();
key[v] = weight;
pq.push({key[v], v});
parent[v] = u; }} }
int totalWeight = 0;
if (parent[i] != -1) {
cout << parent[i] << " - " << i << " : " << key[i] << endl;
totalWeight += key[i]; } }
cout << "Total Weight of MST: " << totalWeight << endl; }};
int main() {
Graph g(V);
g.addEdge(0, 1, 10);
g.addEdge(0, 2, 15);
g.addEdge(1, 2, 5);
g.addEdge(1, 3, 20);
g.addEdge(2, 4, 25);
g.addEdge(3, 4, 30);
g.addEdge(3, 5, 35);
g.addEdge(4, 5, 10);
cout << "Minimum Spanning Tree using Kruskal's Algorithm:" << endl;
g.kruskalMST();
cout << "Minimum Spanning Tree using Prim's Algorithm:" << endl;
g.primMST();
return 0;
/* ************Output**************************
1-2:5
0 - 1 : 10
4 - 5 : 10
2 - 4 : 25
3 - 4 : 30
0 - 1 : 10
1-2:5
2 - 4 : 25
4 - 5 : 10
3 - 4 : 30
******************************************** */
Name :- Mhaske Suraj Machhindra
Roll-No :- 207B073
Asingnment No :- 8
#include <iostream>
class Graph {
vector<vector<pair<int, int>>> adjList; // Adjacency list where each pair contains (destination,
weight)
public:
Graph(int V) : V(V) {
adjList.resize(V);
adjList[u].push_back({v, weight});
dist[src] = 0;
// Min-heap priority queue to store vertices and their distances from the source
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
// If the popped vertex distance is greater than the current known distance, continue
pq.push({dist[v], v});
} }}
cout << "Shortest distances from source " << src << " to all other landmarks:" << endl;
cout << "Landmark " << src << " to Landmark " << i << " : ";
if (dist[i] == INT_MAX) {
} else {
}} }};
int main() {
Graph g(V);
g.addEdge(0, 1, 10);
g.addEdge(0, 4, 3);
g.addEdge(1, 2, 2);
g.addEdge(1, 4, 4);
g.addEdge(2, 3, 9);
g.addEdge(3, 4, 7);
g.addEdge(4, 2, 1);
g.dijkstra(source);
return 0;
}
/* ********************************Output***********************
Landmark 0 to Landmark 0 : 0
Landmark 0 to Landmark 1 : 7
Landmark 0 to Landmark 2 : 6
Landmark 0 to Landmark 3 : 15
Landmark 0 to Landmark 4 : 3
*************************************************** */
Name :- Mhaske Suraj Machhindra
Roll-No :- 207B073
Asingnment No :- 9
#include <iostream>
largest = left;
largest = right;
if (largest != i) {
int n = arr.size();
heapify(arr, n, i);
}}
int main() {
printArray(arr);
heapSort(arr);
printArray(arr);
return 0;
}
Name :- Mhaske Suraj Machhindra
Roll-No :- 207B073
Asingnment No :- 10
#include <iostream>
struct Student {
int rollNumber;
string name;
string division;
string address;
};
void addStudent() {
Student student;
getline(cin, student.name);
getline(cin, student.division);
getline(cin, student.address);
if (file.is_open()) {
file << student.rollNumber << " " << student.name << " " << student.division << " " <<
student.address << endl;
file.close();
} else {
cout << "Unable to open file to add student record.\n"; } }
void displayStudent() {
int rollNumber;
ifstream file("students.txt");
if (file.is_open()) {
Student student;
getline(file, student.address);
if (student.rollNumber == rollNumber) {
found = true;
break; }}
file.close();
if (!found) {
cout << "Student with Roll Number " << rollNumber << " not found.\n"; }
} else {
void deleteStudent() {
int rollNumber;
cout << "Enter Roll Number to delete: ";
ifstream file("students.txt");
ofstream tempFile("temp.txt");
Student student;
file.ignore();
getline(file, student.address);
if (student.rollNumber == rollNumber) {
found = true;
cout << "Student with Roll Number " << rollNumber << " deleted.\n";
} else {
tempFile << student.rollNumber << " " << student.name << " " << student.division << " "
<< student.address << endl; } }
file.close();
tempFile.close();
remove("students.txt");
rename("temp.txt", "students.txt");
if (!found) {
cout << "Student with Roll Number " << rollNumber << " not found.\n"; }
} else {
void displayAllStudents() {
ifstream file("students.txt");
if (file.is_open()) {
Student student;
file.ignore();
getline(file, student.address);
file.close();
} else {
int main() {
int choice;
do {
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayStudent();
break;
case 3:
deleteStudent();
break;
case 4:
displayAllStudents();
break;
case 5:
break;
default:
return 0;