0% found this document useful (0 votes)
22 views41 pages

Name 5

...

Uploaded by

Suraj Mhaske
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views41 pages

Name 5

...

Uploaded by

Suraj Mhaske
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Practical No : 1

#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;

if (key == s[mid].name){ return mid;


}
else if (key < s[mid].name){ high = mid - 1;
}
else {
low = mid + 1; }
} return -1;
}
void view (struct student list[MaxStudent],
int 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 j=0; j<MaxStudent; j++){


cout <<left<<setw(10)<< j+1 <<left<<setw(10)<< list[j].roll_no <<left <<setw(25)<< list[j].name<<left<<setw(15)<<list[j].sgpa
<<endl;
} }
int main() { struct student s[MaxStudent ]; input(s);
display(s);
int choice,cont=0; do{

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>

using namespace std;

typedef struct node {

char data;

struct node *next;

} node;

class stack {

node *top;

public:

stack() {

top = NULL;

bool isempty() {

return top == NULL;

void push(char x) {

node *p = new node();

p->data = x;

p->next = top;

top = p;

char pop() {

if (isempty()) {

cout << "Stack underflow" << endl;

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;

};

void infix_postfix(char infix[20], char postfix[20]);

void reverse(char a[20], char b[20]);

void infix_prefix(char infix[20], char prefix[20]);

int evaluate(int op1, int op2, char op);

void evaluate_postfix(char postfix[20]);

int precedence(char x);

void evaluate_prefix(char prefix[20]);

int main() {

char infix[20], postfix[20], prefix[20];

int ch;

do {

cout << "\n1. Infix to Postfix Conversion";

cout << "\n2. Infix to Prefix Conversion";

cout << "\n3. Evaluate Postfix Expression";

cout << "\n4. Evaluate Prefix Expression";

cout << "\n5. Exit";

cout << "\nEnter your choice: ";

cin >> ch;


switch (ch) {

case 1:

cout << "\nEnter Infix Expression: ";

cin >> infix;

infix_postfix(infix, postfix);

cout << "\nPostfix Expression: " << postfix;

break;

case 2:

cout << "\nEnter Infix Expression: ";

cin >> infix;

infix_prefix(infix, prefix);

cout << "\nPrefix Expression: " << prefix;

break;

case 3:

cout << "\nEnter Postfix Expression: ";

cin >> postfix;

evaluate_postfix(postfix);

break;

case 4:

cout << "\nEnter Prefix Expression: ";

cin >> prefix;

evaluate_prefix(prefix);

break;

case 5:

cout << "Exiting...";

break;

default:

cout << "Invalid Choice!";

} while (ch != 5);


return 0;

void infix_postfix(char infix[20], char postfix[20]) {

stack s;

int i, j = 0;

char token, x;

for (i = 0; infix[i] != '\0'; i++) {

token = infix[i];

if (isalnum(token)) {

postfix[j++] = token;

} else if (token == '(') {

s.push(token);

} else if (token == ')') {

while ((x = s.pop()) != '(') {

postfix[j++] = x;

} else {

while (!s.isempty() && precedence(token) <= precedence(s.topdata())) {

postfix[j++] = s.pop();

s.push(token);

while (!s.isempty()) {

postfix[j++] = s.pop();

postfix[j] = '\0';

void reverse(char a[20], char b[20]) {

int i, j = 0;

for (i = strlen(a) - 1; i >= 0; i--) {


if (a[i] == '(')

b[j++] = ')';

else if (a[i] == ')')

b[j++] = '(';

else

b[j++] = a[i];

b[j] = '\0';

void infix_prefix(char infix[20], char prefix[20]) {

char infix1[20], prefix1[20];

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;

int evaluate(int op1, int op2, char op) {

switch (op) {

case '+': return op1 + op2;

case '-': return op1 - op2;

case '*': return op1 * op2;

case '/': return op1 / op2;

case '%': return op1 % op2;


default: return 0;

void evaluate_postfix(char postfix[20]) {

stack s;

int i, op1, op2, result;

char token;

for (i = 0; postfix[i] != '\0'; i++) {

token = postfix[i];

if (isalnum(token)) {

int value;

cout << "Enter value for " << token << ": ";

cin >> value;

s.push(value);

} else {

op2 = s.pop();

op1 = s.pop();

result = evaluate(op1, op2, token);

s.push(result);

result = s.pop();

cout << "Result: " << result << endl;

void evaluate_prefix(char prefix[20]) {

stack s;

int i, op1, op2, result;

char token;

for (i = strlen(prefix) - 1; i >= 0; i--) {

token = prefix[i];

if (isalnum(token)) {
int value;

cout << "Enter value for " << token << ": ";

cin >> value;

s.push(value);

} else {

op1 = s.pop();

op2 = s.pop();

result = evaluate(op1, op2, token);

s.push(result);

result = s.pop();

cout << "Result: " << result << endl;

}
Practical No : 3

#include <iostream>
using namespace std;

int cqueue[5];
int front = -1, rear = -1, n = 5;

void insertCQ(int val) {


if ((front == 0 && rear == n - 1) || (front == rear + 1)) {
cout << "Queue Overflow\n";
return;
}
if (front == -1) {
front = 0;
rear = 0;
} else if (rear == n - 1) {
rear = 0;
} else {
rear = rear + 1;
}
cqueue[rear] = val;
}

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;
}

void push(Node* node) {


StackNode *newNode = new StackNode();
newNode->data = node;
newNode->next = top;
top = newNode;
}

Node* pop() {
if (isEmpty())
return nullptr;
StackNode *temp = top;
Node* node = top->data;
top = top->next;
delete temp;
return node;
}
};

Node* createTreeFromPostfix(const char postfix[]) {


Stack s;
for (int i = 0; postfix[i] != '\0'; i++) {
Node* newNode = new Node();
newNode->data = postfix[i];
newNode->left = newNode->right = nullptr;
if (isalnum(postfix[i])) {
s.push(newNode);
} else {
newNode->right = s.pop();
newNode->left = s.pop();
s.push(newNode);
}
}
return s.pop();
}

Node* createTreeFromPrefix(const char prefix[]) {


Stack s;
int length = strlen(prefix);
for (int i = length - 1; i >= 0; i--) {
Node* newNode = new Node();
newNode->data = prefix[i];
newNode->left = newNode->right = nullptr;

if (isalnum(prefix[i])) {
s.push(newNode);
} else {
newNode->left = s.pop();
newNode->right = s.pop();
s.push(newNode);
}
}
return s.pop();
}

void inorder(Node* root) {


if (root) {
inorder(root->left);
cout << root->data;
inorder(root->right);
}
}

void preorder(Node* root) {


if (root) {
cout << root->data;
preorder(root->left);
preorder(root->right);
}
}

void postorder(Node* root) {


if (root) {
postorder(root->left);
postorder(root->right);
cout << root->data;
}
}

void inorderNonRecursive(Node* root) {


Stack s;
while (root || !s.isEmpty()) {
while (root) {
s.push(root);
root = root->left;
}
root = s.pop();
cout << root->data;
root = root->right;
}
}

void preorderNonRecursive(Node* root) {


Stack s;
while (root || !s.isEmpty()) {
while (root) {
cout << root->data;
s.push(root);
root = root->left;
}
root = s.pop();
root = root->right;
}
}

void postorderNonRecursive(Node* root) {


Stack s1, s2;
s1.push(root);
while (!s1.isEmpty()) {
root = s1.pop();
s2.push(root);
if (root->left) {
s1.push(root->left);
}
if (root->right) {
s1.push(root->right);
}
}
while (!s2.isEmpty()) {
cout << s2.pop()->data;
}
}

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>

using namespace std;

struct bstnode {

int data;

struct bstnode *left, *right;

};

struct bstnode* newNode(int value) {

bstnode* temp = new bstnode();

temp->data = value;

temp->left = temp->right = nullptr;

return temp;

void inorder(struct bstnode* root) {

if (root != nullptr) {

inorder(root->left);

cout << root->data << " ";

inorder(root->right);

struct bstnode* insert(struct bstnode* node, int key) {

if (node == nullptr)

return newNode(key);

if (key < node->data)

node->left = insert(node->left, key);

else

node->right = insert(node->right, key);

return node;
}

struct bstnode* search(struct bstnode* root, int key) {

if (root == nullptr || root->data == key)

return root;

if (key < root->data)

return search(root->left, key);

return search(root->right, key);

struct bstnode* minValueNode(struct bstnode* node) {

struct bstnode* current = node;

while (current && current->left != nullptr)

current = current->left;

return current;

struct bstnode* deleteNode(struct bstnode* root, int key) {

if (root == nullptr)

return root;

if (key < root->data)

root->left = deleteNode(root->left, key);

else if (key > root->data)

root->right = deleteNode(root->right, key);

else {

if (root->left == nullptr) {

struct bstnode* temp = root->right;

delete root;

return temp;

} else if (root->right == nullptr) {

struct bstnode* temp = root->left;


delete root;

return temp;

struct bstnode* temp = minValueNode(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

return root;

void mirror(struct bstnode* node) {

if (node == nullptr)

return;

mirror(node->left);

mirror(node->right);

struct bstnode* temp = node->left;

node->left = node->right;

node->right = temp;

struct bstnode* copy(struct bstnode* root) {

if (root == nullptr)

return nullptr;

struct bstnode* newRoot = newNode(root->data);

newRoot->left = copy(root->left);

newRoot->right = copy(root->right);

return newRoot;

int maxDepth(struct bstnode* root) {

if (root == nullptr)
return 0;

int leftDepth = maxDepth(root->left);

int rightDepth = maxDepth(root->right);

return max(leftDepth, rightDepth) + 1;

int main() {

struct bstnode* root = nullptr;

struct bstnode* rootCopy = nullptr;

int choice, n, value, depth;

while (true) {

cout << "\n************** MENU **************\n";

cout << "1. Insert\n";

cout << "2. Delete\n";

cout << "3. Search\n";

cout << "4. Traversal\n";

cout << "5. Depth of BST\n";

cout << "6. Copy of BST\n";

cout << "7. Mirror Image\n";

cout << "8. Exit\n";

cout << "***********************************\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << "\nEnter number of values to insert: ";

cin >> n;

cout << "Enter the values: ";

for (int i = 0; i < n; ++i) {


cin >> value;

root = insert(root, value);

break;

case 2:

cout << "\nEnter the value to delete: ";

cin >> value;

root = deleteNode(root, value);

break;

case 3:

cout << "\nEnter the value to search: ";

cin >> value;

if (search(root, value) != nullptr)

cout << "Value found in BST.\n";

else

cout << "Value not found in BST.\n";

break;

case 4:

cout << "\nInorder traversal: ";

inorder(root);

cout << endl;

break;

case 5:

depth = maxDepth(root);

cout << "\nThe depth of the BST is: " << depth << endl;

break;

case 6:
rootCopy = copy(root);

cout << "\nInorder traversal of the copied BST: ";

inorder(rootCopy);

cout << endl;

break;

case 7:

mirror(root);

cout << "\nInorder traversal of the mirrored BST: ";

inorder(root);

cout << endl;

break;

case 8:

return 0;

default:

cout << "\nInvalid option. Try again.\n";

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;

void inorderTraversal(Node *temp, Node *dummy);


void preorderTraversal(Node *temp, Node *dummy);

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;

cout << "\nEnter the element: ";


cin >> newNode->data;

if (root == nullptr) {
root = newNode;
dummy->left = root;
root->left = dummy;
root->right = dummy;
} else {
insert(root, newNode);
}
}

void ThreadedBinaryTree::insert(Node *root, Node *newNode) {


if (newNode->data < root->data) {
if (root->lth == 0) {
newNode->left = root->left;
newNode->right = root;
root->left = newNode;
root->lth = 1;
} else {
insert(root->left, newNode);
}
} else if (newNode->data > root->data) {
if (root->rth == 0) {
newNode->right = root->right;
newNode->left = root;
root->right = newNode;
root->rth = 1;
} else {
insert(root->right, 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);
}
}

void ThreadedBinaryTree::inorderTraversal(Node *temp, Node *dummy) {


while (temp != dummy) {
while (temp->lth == 1)
temp = temp->left;
cout << temp->data << " ";
while (temp->rth == 0) {
temp = temp->right;
if (temp == dummy)
return;
cout << temp->data << " ";
}
temp = temp->right;
}
}

void ThreadedBinaryTree::preorderTraversal(Node *temp, Node *dummy) {


while (temp != dummy) {
cout << temp->data << " ";
if (temp->lth == 1) {
temp = temp->left;
} else {
while (temp->rth == 0 && temp != dummy)
temp = temp->right;
temp = temp->right;
}
}
}

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!";
}

cout << "\n\nWant to see the main menu? (y/n): ";


cin >> ans;
} while (ans == 'y');

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];
}
}
}

cout << "Edges in the graph are:\n\tSource\tSink\tWeight\n";


for (i = 0; i < k; i++) {
cout << "\t" << char(g[i].v1 + 65) << "\t" << char(g[i].v2 + 65) << "\t" << g[i].wt << "\n";
}
}

void MST::prims() {
int i, j, min_edge, visited[20] = {0}, sum = 0, min, flag;
visited[0] = 1; // Start from vertex 0

for (i = 0; i < n - 1; i++) { // We need n-1 edges for a spanning tree


min = 1000;
flag = 0;

for (j = 0; j < k; j++) {


// Check if one vertex is visited and the other is not
if ((visited[g[j].v1] == 0 && visited[g[j].v2] == 1) || (visited[g[j].v1] == 1 && visited[g[j].v2] == 0)) {
if (g[j].wt < min) {
min = g[j].wt;
min_edge = j;
flag = 1;
}
}
}

// If a minimum edge is found, include it in MST


if (flag) {
cout << "\nEdge included: " << char(g[min_edge].v1 + 65) << " -- " << char(g[min_edge].v2 + 65)
<< " with weight " << g[min_edge].wt;
visited[g[min_edge].v1] = 1;
visited[g[min_edge].v2] = 1;
sum += g[min_edge].wt;
}
}

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

using namespace std;

class graph {

int g[max][max], n, c[max], ch[max], min_dist, client_dist, visit_dist;

char v[max], str[max][max], min_path[max], client_path[max];

public:

graph(int m) {

n = m;

visit_dist = 0;

client_dist = 0;

min_dist = 0;

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

g[i][j] = 0;

void accept_v();

void accept_e();

void display();

void dj_init(int);

void dj_init();

void dj(char, char, int);

void dj_client(char, char);

};

void graph::accept_v() {
int i = 0;

cout << "\nName of Cities...\n";

while (i < n) {

cout << "\nEnter Name of City : [" << i + 1 << "] :: ";

cin >> v[i];

i++;

void graph::accept_e() {

char v1, v2;

int i, j, cst;

for (i = 0; i < n; i++) {

for (j = i + 1; j < n; j++) {

if (i == j) {

g[i][j] = 0;

continue;

} else {

cout << "\n\tRent between cities [" << v[i] << "][" << v[j] << "] : ";

cin >> cst;

g[i][j] = g[j][i] = cst;

void graph::display() {

int i, j;

cout << "\nCities: ";

for (i = 0; i < n; i++) {


cout << "\t" << v[i];

cout << "\nAdjacency Matrix:\n";

for (i = 0; i < n; i++) {

cout << v[i];

for (j = 0; j < n; j++) {

cout << "\t" << g[i][j];

cout << endl;

void graph::dj_init() {

int i, j;

for (i = 0; i < n; i++) {

c[i] = 9999;

ch[i] = 0;

for (j = 0; j < n; j++) {

str[i][j] = '-';

void graph::dj_init(int i) {

for (int j = 0; j < n; j++) {

str[i][j] = '-';

void graph::dj(char s, char d, int f) {

dj_init();
int i, j, l, k, flag, min = 999, cst = 0;

i = 0;

while (v[i] != s) i++;

c[i] = 0;

k = 0;

str[i][k] = v[i];

do {

ch[i] = 1;

min = 999;

for (j = 0; j < n; j++) {

flag = 0;

cst = c[i] + g[i][j];

if (g[i][j] != 0 && i != j && cst < c[j]) {

k = 0;

c[j] = cst;

dj_init(j);

while (flag == 0) {

str[j][k] = str[i][k];

k++;

if (str[i][k] == '-') flag = 1;

str[j][k] = v[j];

for (l = 0; l < n; l++) {

if (c[l] <= min && ch[l] == 0) {

min = c[l];

i = l;
}

} while (v[i] != d);

j = 0;

if (f == 0) min_dist = c[i];

else client_dist = c[i];

if (f == 0) {

cout << "\nMinimum Distance : " << c[i] << "\nShortest Path: ";

while (str[i][j] != '-') {

cout << " " << str[i][j];

j++;

min_path[j] = '\0';

cout << "\n\n";

j = 1;

while (str[i][j] != '-' && f == 1) {

client_path[j - 1] = str[i][j];

j++;

client_path[j - 1] = '\0';

cout << "\nShortest Distance: " << client_dist << "\n";

int main() {

int n, ch;

char v1, v2;

cout << "\nEnter Number of Cities: ";

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--> ";

cin >> ch;

switch (ch) {

case 1:

g.accept_v();

break;

case 2:

g.accept_e();

break;

case 3:

g.display();

break;

case 4:

cout << "\nEnter Start City: ";

cin >> v1;

cout << "\nEnter Destination City: ";

cin >> v2;

g.dj(v1, v2, 0);

break;

} while (ch != 5);

return 0;

}
Practical No : 9
#include <iostream>

using namespace std;

void MaxHeapify(int a[], int i, int n) {

int left = 2 * i;

int right = 2 * i + 1;

int largest = i;

if (left <= n && a[left] > a[largest]) {

largest = left;

if (right <= n && a[right] > a[largest]) {

largest = right;

if (largest != i) {

swap(a[i], a[largest]);

MaxHeapify(a, largest, n); // Recursively heapify the affected subtree

void HeapSort(int a[], int n) {

for (int i = n / 2; i >= 1; i--) {

MaxHeapify(a, i, n);

for (int i = n; i >= 2; i--) {

swap(a[1], a[i]);

MaxHeapify(a, 1, i - 1);

void Build_MaxHeap(int a[], int n) {

for (int i = n / 2; i >= 1; i--) {

MaxHeapify(a, i, n);

}}
int main() {

int n;

cout << "\nEnter the number of data elements to be sorted: ";

cin >> n;

int arr[n + 1]; // Array with 1-based indexing

for (int i = 1; i <= n; i++) {

cout << "Enter element " << i << ": ";

cin >> arr[i];

HeapSort(arr, n);

cout << "\nSorted Data: ";

for (int i = 1; i <= n; i++) {

cout << arr[i] << " ";

cout << endl;

return 0;

}
Practical No : 10
#include <iostream>

#include <fstream>

#include <cstring>

#include <iomanip>

using namespace std;

const int MAX = 20;

class Student {

int rollno;

char name[MAX], city[MAX];

char div;

int year;

public:

Student() {

strcpy(name, "");

strcpy(city, "");

rollno = year = div = 0;

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:

FileOperations(const char *filename) {

file.open(filename, ios::in | ios::out | ios::ate | ios::binary);

if (!file) {

cout << "File could not be opened!" << endl;

exit(1);

void insertRecord(int rollno, char name[MAX], int year, char div, char city[MAX]) {

Student s1(rollno, name, year, div, city);

file.seekp(0, ios::end);

file.write((char *)&s1, sizeof(Student));

file.clear();

void displayAll() {

Student s1;
file.seekg(0, ios::beg);

while (file.read((char *)&s1, sizeof(Student))) {

s1.displayRecord();

file.clear();

void displayRecord(int rollNo) {

Student s1;

file.seekg(0, ios::beg);

bool flag = false;

while (file.read((char *)&s1, sizeof(Student))) {

if (s1.getRollNo() == rollNo) {

s1.displayRecord();

flag = true;

break;

if (!flag) {

cout << "\nRecord of Roll No " << rollNo << " is not present." << endl;

file.clear();

void deleteRecord(int rollno) {

ofstream outFile("new.dat", ios::binary);

file.seekg(0, ios::beg);
bool flag = false;

Student s1;

while (file.read((char *)&s1, sizeof(Student))) {

if (s1.getRollNo() == rollno) {

flag = true;

continue;

outFile.write((char *)&s1, sizeof(Student));

if (!flag) {

cout << "\nRecord of Roll No " << rollno << " is not present." << endl;

file.close();

outFile.close();

remove("student.dat");

rename("new.dat", "student.dat");

file.open("student.dat", ios::in | ios::out | ios::ate | ios::binary);

~FileOperations() {

file.close();

cout << "\nFile Closed." << endl;

};

int main() {

ofstream newFile("student.dat", ios::app | ios::binary);


newFile.close();

FileOperations file("student.dat");

int rollNo, year, choice = 0;

char div;

char name[MAX], city[MAX];

while (choice != 5) {

cout << "\n**Student Database**" << endl;

cout << "1) Add New Record" << endl;

cout << "2) Display All Records" << endl;

cout << "3) Display by RollNo" << endl;

cout << "4) Delete a Record" << endl;

cout << "5) Exit" << endl;

cout << "Choose your choice: ";

cin >> choice;

switch (choice) {

case 1:

cout << endl << "Enter RollNo and name: ";

cin >> rollNo >> name;

cout << "Enter Year and Division: ";

cin >> year >> div;

cout << "Enter City: ";

cin >> city;

file.insertRecord(rollNo, name, year, div, city);

cout << "\nRecord Inserted." << endl;


break;

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:

cout << "Enter Roll Number: ";

cin >> rollNo;

file.displayRecord(rollNo);

break;

case 4:

cout << "Enter Roll No: ";

cin >> rollNo;

file.deleteRecord(rollNo);

break;

case 5:

break;

default:

cout << "Invalid choice. Please try again." << endl;

return 0;

You might also like