Vous êtes sur la page 1sur 6

1.

Stack
#include<stdio.h>
#include<conio.h>
#define s 5
void push();
void pop();
void display();
int i,top=0;val,a[s];
void main()
{
clrscr();
do
{
printf(\n1.push\n2.pop\n3.display\n4.exit\n);
printf(enter the choice);
scanf(%d,&ch);
switch(ch);
{
case 1:
push();
break;
case 2:
pop();
break();
case 3:
display();
break();
default:
printf(enter the 1to 4);
break;
}
}while(ch!=4);
getch();
}
void push()
{
printf(\n\t\tpush operation);
if(top==s)
printf(stack is overflow);
else
{

Printf(\nenter the value);


scanf(%d,&val);
a[++top]=val;
}
}
void pop()
{
if(top==0)
printf(stack is underflow);
else
{
Printf(\npoped item is%d,a[top]);
top--;
}
}
void display()
{
if(top==0)
printf(stack is empty);
else
{
for(i=top;i>=0;i--)
{
printf(%d,a[i]);
}
}
}

2.Infix to Postfix
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
char inf[40],post[40];
int top=0,str[20];
void postfix();
void push(int);
char pop();
void main(void)
{
clrscr();
printf("\n Enter the infix expression\n");
scanf("%s",&inf);
postfix();
getch();
}
void postfix()
{
int i,j=0;
for(i=0;inf[i]!='\0';i++)
{
switch(inf[i])
{
case '+':
while(str[top]>=1)
post[j++]=pop();
push(1);
break;
case '-':
while(str[top]>=1)
post[j++]=pop();
push(2);
break;
case '*':
while(str[top]>=3)
post[j++]=pop();
push(3);
b reak;
case '/':
while(str[top]>=3)

post[j++]=pop();
push(4);
break;
case '^':
while(str[top]>=4)
post[j++]=pop();
push(5);
break;
case '(':
push(0);
break;
case ')':
while(str[top]>=1)
post[j++]=pop();
top--;
break;
default:
post[j++]=inf[i];
}}
while (top>0)
post[j++]=pop();
printf("\n \t postfix expression is =\n\n\t\t %s",post);
}
void push(int ele)
{
top++;
str[top]=ele;
}
char pop()
{
int el;
char e;
el=str[top];
top--;
switch(el)
{
case 1:
e='+';
break;
case 2:
e='-';
break;
case 3:

e='*';
break;
case 4:
e='/';
break;
case 5:
e='^';
break;
}
return(e);
}
3.Postfix evaluation
#include<stdio.h>
#include<conio.h>
#include<string.h>
#includepop.h
#includepush.h
int =0,stack[20];
void main()
{
clrscr();
char expr[20];
int len,x,a,b,c,d,e,f,i;
printf(%s,&expr);
len=strlen(expr);
for(i=1;i<=len;i++)
{
if(expr[i]=+|| expr[i]==-|| expr[i]==*|| expr[i]==/)
{
b=pop();
a=pop();
swich(expr[i])
{
case +:
c=a+b;
break;
case -:
c=a-b;
break;
case *:
c=a*b;
break;

case /:
c=a/b;
break;
}
push(c);
}
else{
printf(enter the value%c,expr[i]);
scanf(%d,&x);
push(x);
}
}
printf(the resultant value%d,c);
getch();
}
Pop.h
#include<stdio.h>
Int stack[30],top=0;
int pop()
{
int t;
t=stack[top];
top--;
return(t);
}
Push.h
#include<stdio.h>
void push(int x)
{
Stack[++top]=x;
}

Vous aimerez peut-être aussi