Professional Documents
Culture Documents
include<iostream.h>
#include<conio.h>
char infix[50],postfix[50];
char stack[50];
int top=-1;
void main()
{
char is;
int i,k=0;
void push(char);
char pop();
int pre(char);
clrscr();
cout<<"Enter any infix expression\n";
cin>>infix;
push('#');
for(i=0;infix[i];i++)
{
is=infix[i];
if(is==' ')
continue;
while(pre(is)<=pre(stack[top]))
{
if(stack[top]=='(')
break;
postfix[k]=pop();
k++;
}
if(stack[top]=='('&& is==')')
pop();
else
push(is);
}
while(stack[top]!='#')
{
postfix[k]=pop();
k++;
}
postfix[k]='\0';
cout<<"postfix expression is\n"<<postfix;
getch();
}
void push(char ch)
{
top++;
stack[top]=ch;
}
char pop()
{
char ch;
ch=stack[top];
top--;
return(ch);
}
int pre(char ch)
{
switch(ch)
{
case '+':
case '-':return(1);
case '*':
case '/':return(2);
case '^':
case '$':return(3);
case '#':return(-1);
case '(':return(5);
case ')':return(0);
default:return(4);
}
}
4. Demonstrate recursion –
a. Calculate GCD and LCM of 3 integer numbers
b. Solve Towers of Hanoi problem
c. Calculate the sum for a given number ‘n’ from 1 to n.
#include<iostream.h>
#include<conio.h>
int GCD(int m, int n)
{
if (n==0)
return m;
return GCD(n,m%n);
}
int LCM(int m, int n)
{
return m*n/GCD(m,n);
}
void main()
{
int m,n,p,gcd,lcm;
cout<<"Enter m, n and p”;
cin>>m>>n>>p;
gcd=GCD(m,GCD(n,p));
lcm=LCM(m,LCM(n,p));
cout<<”GCD=”<<gcd;
cout<<”lcm=”<<lcm;
getch();
}
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *list=NULL;
struct node *list_last=NULL;