Sunday 30 October 2011

C PROGRAM TO IMPLEMENT STACK


#define maxsize 4
main()
{
int s[maxsize],top=-1;
int choice,data;
void push(int *,int *,int);
int pop(int *,int *,int *);
void display(int *,int);
clrscr();
while(1)
{
printf("\n1:push.....2:pop.....3:display......4:exit\n");
printf("enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nenter the data to be pushed onto the stack\n");
scanf("%d",&data);
push(s,&top,data);
break;
case 2:if(!pop(s,&top,&data))
printf("stack underflow\n");
else
printf("%d popped successfully\n",data);
break;
case 3:display(s,top);
  break;
case 4:exit(0);
break;
default:printf("\ninvalid coice");
}
}
}
void push(int *s,int *top,int data)
{
if(*top==maxsize-1)
printf("\nstack overflow");
else
{
*top=*top+1;
s[*top]=data;
printf("\n%dis pushed successfully onto stack",data);
   }
 }
int pop(int *s,int *top,int *data)
{
if(*top==-1)
return(0);
else
{
*data=s[*top];
*top=*top-1;
return(1);
}
}
void display(int *s,int top)
{ int i;

if(top==-1)
printf("\nstack is empty");
else
{
printf("\nstack contents:-\n");
for(i=0;i<=top;i++)
printf("%d  ",s[i]);
}
}


EVALUATION OF POSTFIX EXPRESSION


#include<stdio.h>
#include<ctype.h>
#include<string.h>
main()
{
char infix[20];
float eval(char *);
clrscr();
printf("\nenter a valid infix expression:-\n");
gets(infix);
strrev(infix);
printf("\nresult of evaluation=%f\n",eval(infix));
getch();
}
float eval(char *infix)
{
float stack[20],x1,x2;
int i=0,top=-1;
char ch;
while((ch=infix[i++])!='\0')
{
if(isdigit(ch))
stack[++top]=ch-'0';
else
{
x1=stack[top--];
x2=stack[top--];
switch(ch)
{
case '+':stack[++top]=x1+x2;
break;
case '-':stack[++top]=x1-x2;
break;
case '*':stack[++top]=x1*x2;
break;
case '/':stack[++top]=x1/x2;
break;
}
     }
   }
  return(stack[top]);
 }

EVALUATION OF POSTFIX EXPRESSION


#include<stdio.h>
#include<ctype.h>
main()
{
char suffix[20];
float eval(char *);
clrscr();
printf("\nenter a valid postfix expression:-\n");
gets(suffix);
printf("\nresult of evaluation=%f\n",eval(suffix));
getch();
}
float eval(char *suffix)
{
float stack[20],x1,x2;
int i=0,top=-1;
char ch;
while((ch=suffix[i++])!='\0')
{
if(isdigit(ch))
stack[++top]=ch-'0';
else
{
x2=stack[top--];
x1=stack[top--];
switch(ch)
{
case '+':stack[++top]=x1+x2;
break;
case '-':stack[++top]=x1-x2;
break;
case '*':stack[++top]=x1*x2;
break;
case '/':stack[++top]=x1/x2;
break;
}
     }
   }
  return(stack[top]);
 }

C PROGRAM TO CONVERT INFIX EXP INTO PREFIX


#include<stdio.h>
#include<ctype.h>
#include<string.h>
main()
{
char infix[20],prefix[20],stack[20],ch,c;
int top=-1,i=0,j=0;
int preced(char);
clrscr();
printf("enter a valid infix expression\n");
gets(infix);
strrev(infix);
stack[++top]='#';
while((ch=infix[i++])!='\0')
{
if(isalnum(ch))
prefix[j++]=ch;
else
{
switch(ch)
{
case ')':stack[++top]=ch;
break;
case '(':while((c=stack[top--])!=')')
prefix[j++]=c;
break;
default:while(preced(ch)<preced(c=stack[top]))
{
prefix[j++]=c;
top--;
}
      stack[++top]=ch;
      }
    }
  }
while(top>0)
prefix[j++]=stack[top--];
prefix[j]='\0';
strrev(prefix);
printf("\nresultant prefix expression:-  %s",prefix);
getch();
}
int preced(char ch)
{
if(ch=='#'||ch==')')
return(0);
else if(ch=='+'||ch=='-')
return(1);
else if(ch=='*'||ch=='/'||ch=='%')
return(2);
else if(ch=='^'||ch=='$')
return(3);
}

C PROGRAM TO CONVERT INFIX EXP INTO POSTFIX


#include<stdio.h>
#include<ctype.h>
#include<string.h>
main()
{
char infix[20],postfix[20],stack[20],ch,c;
int top=-1,i=0,j=0;
int preced(char);
clrscr();
printf("enter a valid infix expression\n");
gets(infix);
stack[++top]='#';
while((ch=infix[i++])!='\0')
{
if(isalnum(ch))
postfix[j++]=ch;
else
{
switch(ch)
{
case '(':stack[++top]=ch;
break;
case ')':while((c=stack[top--])!='(')
postfix[j++]=c;
break;
default:while(preced(ch)<=preced(c=stack[top]))
{
postfix[j++]=c;
top--;
}
      stack[++top]=ch;
      }
    }
  }
while(top>0)
postfix[j++]=stack[top--];
postfix[j]='\0';
printf("\nresultant postfix expression:-%s",postfix);
getch();
}
int preced(char ch)
{
if(ch=='#'||ch=='(')
return(0);
else if(ch=='+'||ch=='-')
return(1);
else if(ch=='*'||ch=='/'||ch=='%')
return(2);
else if(ch=='^'||ch=='$')
return(3);
}