0% found this document useful (0 votes)
62 views8 pages

Program:-1: AIM:-// Write A Program To Convert Infix Expression Into Postfix Expression

The document contains 3 programs related to data structures and algorithms. Program 1 converts an infix expression to postfix notation. Program 2 evaluates a postfix expression. Program 3 implements quicksort to sort an array of numbers. Program 4 finds the minimum cost spanning tree of a graph using Prim's algorithm.

Uploaded by

Leena Rohilla
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views8 pages

Program:-1: AIM:-// Write A Program To Convert Infix Expression Into Postfix Expression

The document contains 3 programs related to data structures and algorithms. Program 1 converts an infix expression to postfix notation. Program 2 evaluates a postfix expression. Program 3 implements quicksort to sort an array of numbers. Program 4 finds the minimum cost spanning tree of a graph using Prim's algorithm.

Uploaded by

Leena Rohilla
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAM:-1

AIM:- //* Write a program to convert infix expression into postfix expression *//
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
char infix[20],postfix[20],stack[20];
int tos=-1;
int precedence(char ch);
void push(int ele);
char pop();
void main()
{
clrscr();
int i,j=0;
char popped,elem;
cout<<"enter the infix expression";
gets(infix);
push('#');
for(i=0;infix[i]!='\0';i++)
{
if(infix[i]=='(')
{
elem=infix[i];
push(elem);
}
else
if(infix[i]==')')
{
popped=pop();
while(popped!='(')
{
postfix[j]=popped;
j++;
popped=pop();
}}
else
if(infix[i]=='^'||infix[i]=='*'||infix[i]=='/'||infix[i]=='+'||infix[i]=='-')
{
while(precedence(stack[tos])>=precedence(infix[i]))
{
popped=pop();
postfix[j]=popped;
j++;
}
push(infix[i]);
}
else
postfix[j++]=infix[i];
}
while(tos>0)
{
popped=pop();
postfix[j++]=popped;
}
postfix[j]='\0';
cout<<"\n postfix:"<<postfix<<endl;
getch();
}
int precedence(char ch)
{
switch(ch)
{
case '^':return 5;
case'/':return 4;
case'*':return 4;
case'+':return 3;
case'-':return 3;
default :return 0;
}}
void push(int ele)
{
tos++;
stack[tos]=ele;
}
char pop()
{
char item;
if(tos==-1)
{
cout<<"stack empty";
getch();
return(0);
}
else
{
item=stack[tos];
tos--;
}
return(item);
}

OUTPUT:-
enter the infix expression(a+b-c)

postfix:ab+c-
PROGRAM:-2
AIM: //* Write a program to evaluate postfix expression *//

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
int convert(char v)
{
if(v==48)
return 0;
else
if(v==49)
return 1;
else
if(v==50)
return 2;
else
if(v==51)
return 3;
else
if(v==52)
return 4;
else
if(v==53)
return 5;
else
if(v==54)
return 6;
else
if(v==55)
return 7;
else
if(v==56)
return 8;
else
if(v==57)
return 9;
}
void main()
{
clrscr();
int i,j,top=0,l,stack[20];
char pox[20];
cout<<"enter the postfix expression";
gets(pox);
l=strlen(pox);
for(i=0;i<l;i++)
{
if(pox[i]>=48&&pox[i]<=57)
{
j=convert(pox[i]);
stack[top]=j;
top++;
}
if(pox[i]=='+'||pox[i]=='-'||pox[i]=='*'||pox[i]=='/')
{
if(pox[i]=='+')
{
stack[top-2]=stack[top-2]+stack[top-1];
top--;
}
else
if(pox[i]=='-')
{
stack[top-2]=stack[top-2]-stack[top-1];
top--;
}
else
if(pox[i]=='*')
{
stack[top-2]=stack[top-2]*stack[top-1];
top--;
}
else
if(pox[i]=='/')
{
stack[top-2]=stack[top-2]/stack[top-1];
top--;
}
}
}top--;
cout<<"The result is: "<<stack[top];
getch();
}

OUTPUT:-
enter the postfix expression95-4+
The result is: 8
PROGRAM:-3

AIM: //* Write a program to implement quick sort *//


#include<iostream.h>
#include<conio.h>
int num[50];
int position(int,int);
void main()
{
int lower[5],upper[5],n,top,beg,i,loc,end;
clrscr();
top=0;
cout<<"Enter the size of array " ;
cin>>n;
cout<<"Enter the element of Array ";
for(i=0;i<=n-1;i++)
{
cin>>num[i];
}
lower[top]=0;
upper[top]=n-1;
while(top>=0)
{
beg=lower[top];
end=upper[top];
top--;
loc=position(beg,end);
if(loc>beg+1)
{
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc<end-1)
{
top++;
lower[top]=loc+1;
upper[top]=end;
}}
cout<<"The sorted list : "<<"\n";
for(i=0;i<n;i++)
{
cin>>num[i];
}
getch();
}
int position(int left,int right)
{
int t;
while(t)
{
while(left<right && num[left]<=num[right])
{
right--;
}
if(left==right)
return(left);
t=num[left];
num[left]=num[right];
num[right]=t;
while(right>left && num[right]>=num[left])
{
left ++ ;
}
if(left == right)
return(left);
t=num[left];
num[left]=num[right];
num[right]=t;
}}

OUTPUT:-
Enter the size of array 2
Enter the element of Array 9
2
The sorted list :
2
9
PROGRAM NO.- 4
AIM: //* Write a program to implement minimum cost spanning tree *//

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,i,j,a[100][100],c[100],s[100],e[100];
int temp,k,x,y,z,l,d[100],r[100],sum;
clrscr();
cout<<"enter the no. of vertices :=\t";
cin>>n;
cout<<"enter the no. of edges:=\t";
cin>>m;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cout<<"enter the edge between \t"<<i<<"and\t"<<j<<":=\t";
cin>>a[i][j];
}}
k=1;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[i][j]!=-1)
{
c[k]=a[i][j];
s[k]=i;
e[k]=j;
k++;
}
}
}
for(j=1;j<=m;j++)
{
for(i=j+1;i<m;i++)
{
if(c[j]>c[i])
{
temp=c[j];
c[j]=c[i];
c[i]=temp;
temp=s[j];
s[j]=s[i];
s[i]=temp;
temp=e[j];
e[j]=e[i];
e[i]=temp;
}
}
}
for(i=1;i<=n;i++)
{
d[i]=i;
}
l=1;
cout<<"\n there is an edge between:=\n";
for(k=1;k<=m;k++)
{
x=c[k];
y=c[k];
z=e[k];
if(d[j]!=d[z])
{
cout<<"\n vertex \t"<<y<<"and\t"<<z<<"cost\t"<<x;
d[z]=d[y];
r[l]=x;
l++;
}
}
sum=0;
for(i=1;i<=l-1;i++)
{
sum=sum+r[i];
}
cout<<"\n total cost is:=\t"<<sum;
getch();
}

OUTPUT:-
enter the no. of vertices := 2
enter the no. of edges:= 3
enter the edge between 1and 1:= 2
enter the edge between 1and 2:= 1
enter the edge between 2and 1:= 3
enter the edge between 2and 2:= 2

there is an edge between:=

vertex 1and 2cost 1


total cost is:= 1

You might also like