Professional Documents
Culture Documents
#include<iostream.h>
using namespace std;
#define SIZE 50
int s[SIZE];
int top= -1;
void push(int elem)
{
s[++top]=elem;
}
int pop()
{
return(s[top--]);
}
int main()
{
char pofx[50],ch;
int i=0,op1,op2;
cout<<"\n\nRead the Postfix Expression:";
cin>>pofx;
while((ch=pofx[i++])!='\0')
{
if(isdigit(ch))
push(ch-'0');
else
{
op2=pop();
op1=pop();
switch(ch)
{
case '+':
push(op1+op2);break;
case '-':
push(op1-op2);break;
case '*':
push(op1*op2);break;
case '/':
push(op1/op2);break;
}
}
}
cout<<"\n Given Postfix Expn:"<<pofx;
cout<<"\n Result after Evaluation:"<<s[top];
return 0;
}
OUTPUT
Read the Postfix Expression: 32+51-*
Given Postfix Expn: 32+51-*
Result after Evaluation: 20
6. Develop a C++ program for traversing a binary tree in preorder, inorder and postorder using
recursive function.
#include<iostream>
using namespace std;
struct btree
{
struct btree *left;
struct btree *right;
int no;
};
void postorder(struct btree *trav);
void inorder(struct btree *trav);
void preorder(struct btree *trav);
struct btree * create(struct btree *trav);
int main()
{
struct btree *root=NULL;
char c;
while(1)
{
root=create(root);
cout<<"Do you want to continue : ";
cin>>c;
if(c=='n' ||c=='N')
break;
}
cout<<endl<<"Inoder is : ";inorder(root);
cout<<endl<<"Preorder is : ";preorder(root);
cout<<endl<<"Postorder is : ";postorder(root);
return 0;
}
struct btree * create(struct btree *trav)
{
if(trav==NULL)
{
trav=new btree;
trav->right=NULL;
trav->left=NULL;
cout<<"Enter the no : ";
cin>>trav->no;
return(trav);
}
char choice;
cout<<"Enter the left or right child : ";
cin>>choice;
if(choice == 'r' || choice == 'R')
{
trav->right=create(trav->right);
}
if(choice=='l' || choice=='L')
{
trav->left=create(trav->left);
}
return(trav);
}
void inorder(struct btree *trav)
{
if(trav==NULL)
return ;
inorder(trav->left);
cout<<" "<<trav->no;
inorder(trav->right);
}
void preorder(struct btree *trav)
{
if(trav==NULL)
return;
cout<<" "<<trav->no;
preorder(trav->left);
preorder(trav->right);
}
void postorder(struct btree *trav)
{
if(trav==NULL)
return;
postorder(trav->left);
postorder(trav->right);
cout<<" "<<trav->no;
}
OUTPUT
Enter the no : 5
Do you want to continue : y
Enter the left or right child : L L R
Enter the no : 4
Do you want to continue : Y
Enter the left or right child : R
Enter the no : 6
Do you want to continue : N
Inoder is : 4 5 6
Preorder is : 5 4 6
Postorder is : 4 6 5
7. Create a Binary Search Tree to implement the insert, search and delete operations using recursive
function.
# include<iostream>
using namespace std;
// Node Declaration
struct node
{
int info;
struct node *left;
struct node *right;
}*root;
// Class Declaration
class BST
{
public:
void find(int, node **, node **);
void insert(node *,node *) ;
void del(int);
void case_a(node *,node *);
void case_b(node *,node *);
void case_c(node *,node *);
void preorder(node *);
void inorder(node *);
void postorder(node *);
void display(node *, int);
BST()
{
root = NULL;
}
};
// Main Contains Menu
int main()
{
int choice, num;
BST bst;
node *temp;
while (1)
{
cout<<"-----------------"<<endl;
cout<<"Operations on BST"<<endl;
cout<<"-----------------"<<endl;
cout<<"1.Insert Element "<<endl;
cout<<"2.Delete Element "<<endl;
cout<<"3.Inorder Traversal"<<endl;
cout<<"4.Preorder Traversal"<<endl;
cout<<"5.Postorder Traversal"<<endl;
cout<<"6.Display"<<endl;
cout<<"7.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
temp = new node;
cout<<"Enter the number to be inserted : ";
cin>>temp->info;
bst.insert(root, temp);
break;
case 2:
if (root == NULL)
{
cout<<"Tree is empty, nothing to delete"<<endl;
continue;
}
cout<<"Enter the number to be deleted : ";
cin>>num;
bst.del(num);
break;
case 3:
cout<<"Inorder Traversal of BST:"<<endl;
bst.inorder(root);
cout<<endl;
break;
case 4:
cout<<"Preorder Traversal of BST:"<<endl;
bst.preorder(root);
cout<<endl;
break;
case 5:
cout<<"Postorder Traversal of BST:"<<endl;
bst.postorder(root);
cout<<endl;
break;
case 6:
cout<<"Display BST:"<<endl;
bst.display(root,1);
cout<<endl;
break;
case 7:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}
}
return 0;
}
8. Develop a C++ program to find shortest wire length between two components using Dijkstra’s
algorithm for a given circuit.
#include <climits>
#include <iostream>
using namespace std;
#define N 6
int minDist(int dist[], bool sptSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < N; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void displaySolution(int dist[], int n)
{
cout << "Component\tDistance from other component\n";
for (int i = 0; i < n; i++)
printf("%d\t\t%d\n", i, dist[i]);
}
void optimizeLength(int g[N][N], int src)
{
int dist[N];
bool sptSet[N];
for (int i = 0; i < N; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
//Find shortest path for all components.
for (int cnt = 0; cnt < N - 1; cnt++) {
//Pick the minimum distance component from the set of
//components not yet processed.
int u = minDist(dist, sptSet);
//Mark the picked component as processed.
sptSet[u] = true;
//Update dist value of the adjacent components of the picked component.
for (int v = 0; v < N; v++)
if (!sptSet[v] && g[u][v] && dist[u] != INT_MAX && dist[u] + g[u][v] < dist[v])
//Update dist[v] only if is not in sptSet, there is an edge from
//u to v, and total weight of path from src to v through u is
//smaller than current value of dist[v].
dist[v] = dist[u] + g[u][v];
}
displaySolution(dist, N);
}
int main()
{
int g[N][N] = { { 0, 0, 6, 7, 0, 4}, { 4, 0, 8, 0, 1, 2 },
{0, 9, 0, 2,0, 4 },{ 0, 0, 7, 0, 9, 5 }, { 0, 1, 0, 0, 6,7 }, { 6, 7, 0, 0, 2,3} };
cout << "Enter the starting component: ";
int s;
cin >> s;
optimizeLength(g, s);
return 0;
}
OUTPUT
Enter the starting component: 4
Component Distance from other component
0 5
1 1
2 9
3 11
4 0
5 3
9. Implement and test a C++ program to sort the given list of numbers using bubble sort.
#include<iostream>
using namespace std;
int main ()
{
int i, j,temp,pass=0;
int a[10] = {10,2,0,14,43,25,18,1,5,45};
cout <<"Input list ...\n";
for(i = 0; i<10; i++) {
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<10; i++) {
for(j = i+1; j<10; j++)
{
if(a[j] < a[i]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
pass++;
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<10; i++) {
cout <<a[i]<<"\t";
}
cout<<"\nNumber of passes taken to sort the list:"<<pass<<endl;
return 0;
}
OUTPUT
Input list …
10 2 0 14 43 25 18 1 5 45
Sorted Element List …
0 1 2 5 10 14 18 25 43 45
Number of passes taken to sort the list:10
10. Develop and test a C++ Program to check whether the given number is in the list using binary
search.
#include<iostream>
Using namespace std;
int main()
{
int a[20],i,num,low,high,mid,t,flag=0;
clrscr();
cout<<"\nEnter the size of array:";
cin>>num;
cout<<"\nEnter the elements in ascending order:";
for(i=1;i<=num;i++)
cin>>a[i];
cout<<"\nEnter the elements to be searched:";
cin>>t;
low=1;
high=num;
while(low<=high)
{
mid=(low+high)/2;
if(t<a[mid])
high=mid-1;
else if(t>a[mid])
low=mid+1;
else
{
cout<<"Element " <<t<<" is in position "<<mid;
flag=1;
break;
}
}
if(flag==0)
cout<<"\nElement not found";
return 0;
}
OUTPUT
Enter the size of array: 6
Enter the elements in ascending order:
10 20 30 40 50 60
Enter the elements to be searched: 50
Element 50 is at position 5