written 8.0 years ago by | • modified 8.0 years ago |
Mumbai University > Computer Engineering > Sem 3 > Data Structures
Marks: 10M
Years: Dec 2015
written 8.0 years ago by | • modified 8.0 years ago |
Mumbai University > Computer Engineering > Sem 3 > Data Structures
Marks: 10M
Years: Dec 2015
written 8.0 years ago by |
The stack operations are given below.
Stack() creates a new stack that is empty. It needs no parameters and returns an empty stack.
push(item) adds a new item to the top of the stack. It needs the item and returns nothing.
pop() removes the top item from the stack. It needs no parameters and returns the item. The stack is modified.
peek() returns the top item from the stack but does not remove it. It needs no parameters. The stack is not modified.
isEmpty() tests to see whether the stack is empty. It needs no parameters and returns a boolean value.
size() returns the number of items on the stack. It needs no parameters and returns an integer.
Program to demonstrate conversion of prefix expression to postfix expression:
#include <string.h>
#include <ctype.h>
char opnds[50][80],oprs[50];
int topr=-1,topd=-1;
pushd(char *opnd)
{
strcpy(opnds[++topd],opnd);
}
char *popd()
{
return(opnds[topd--]);
}
pushr(char opr)
{
oprs[++topr]=opr;
}
char popr()
{
return(oprs[topr--]);
}
int empty(int t)
{
if( t == 0) return(1);
return(0);
}
main()
{
char prfx[50],ch,str[50],opnd1[50],opnd2[50],opr[2];
int i=0,k=0,opndcnt=0;
gets(prfx);
printf(" Given Prefix Expression : %s\n",prfx);
while( (ch=prfx[i++]) != '\0')
{
if(isalnum(ch))
{
str[0]=ch; str[1]='\0';
pushd(str); opndcnt++;
if(opndcnt >= 2)
{
strcpy(opnd2,popd());
strcpy(opnd1,popd());
strcpy(str,opnd1);
strcat(str,opnd2);
ch=popr();
opr[0]=ch;opr[1]='\0';
strcat(str,opr);
pushd(str);
opndcnt-=1;
}
}
else
{
pushr(ch);
if(opndcnt==1)opndcnt=0; /* operator followed by single operand*/
}
}
if(!empty(topd))
{
strcpy(opnd2,popd());
strcpy(opnd1,popd());
strcpy(str,opnd1);
strcat(str,opnd2);
ch=popr();
opr[0]=ch;opr[1]='\0';
strcat(str,opr);
pushd(str);
}
printf(" Postfix Expression: ");
puts(opnds[topd]);
}
OUTPUT:
Given Prefix Expression: + A * B C
Postfix Expression: A B C * +