Professional Documents
Culture Documents
Global Declarations /: Char Int Char
Global Declarations /: Char Int Char
Global Declarations /: Char Int Char
#include<stdio.h>
#define SIZE 50 /* Size of Stack */
#include<string.h>
#include <ctype.h>
char s[SIZE];
int top=-1;/* Global declarations */
push(char elem)
{/* Function for PUSH operation */
s[++top]=elem;
}
char pop()
{/* Function for POP operation */
return(s[top--]);
}
int pr(char elem)
{/* Function for precedence */
switch(elem)
{
case')':return 0;
break;
case'+':
case'-':return 1;
case'*':
case'/':return 2;
}
}
main()
{/* Main Program */
char infx[50],prfx[50],ch,elem;
int i=0,k=0;
printf("\n\nRead the Infix Expression ? ");
scanf("%s",infx);
strrev(infx);
while((ch=infx[i++])!='\0')
{
if(ch==')') push(ch);
else
if(isalnum(ch))prfx[k++]=ch;
else
if(ch=='(')
{
while( s[top]!=')')
prfx[k++]=pop();
elem=pop();/* Remove ) */
}
else
{/* Operator */
while(pr(s[top])>pr(ch))
prfx[k++]=pop();
push(ch);
}
}
while( s[top]!='\0')/* Pop from stack till empty */
prfx[k++]=pop();
strrev(prfx);
strrev(infx);
printf("\n\nGiven Infix Expn: %s Prefix Expn: %s\n",infx,prfx);
}
Prefix Evaluation
#include<stdio.h>
#include<ctype.h>
#include<string.h>
Void main()
float stack[50];
Int I, top=-1;
Int val;
Char a[30];
Scanf(“%s”, a);
Strrev(a);
While (a[i]!=’/0’)
If( isalpha(a[i])
Scanf(“%d”, &val);
Stack[++top]=val;
If(a[i] ==’+’)
{
Stack[top-1]=stack[top] + stack[top-1];
Top--;
If(a[i] ==’-’)
Stack[top-1]=stack[top] + stack[top-1];
Top--;
If(a[i] ==’*’)
Stack[top-1]=stack[top] + stack[top-1];
Top--;
If(a[i] ==’/’)
Stack[top-1]=stack[top] + stack[top-1];
Top--;
I++;
Printf(“%f”, stack[top]);
Postfix Evaluation
#include<stdio.h>
#include<ctype.h>
Void main()
float stack[50];
Int val;
Char a[30];
Scanf(“%s”, a);
While (a[i]!=’\0’)
If( isalpha(a[i])
Scanf(“%d”, &val);
Stack[++top]=val;
If(a[i] ==’+’)
Stack[top-1]=stack[top-1] + stack[top];
Top--;
If(a[i] ==’-’)
Stack[top-1]=stack[top-1] - stack[top];
Top--;
If(a[i] ==’*’)
Stack[top-1]=stack[top-1] * stack[top];
Top--;
If(a[i] ==’/’)
Stack[top-1]=stack[top-1] / stack[top];
Top--;
I++;
Printf(“%f”, stack[top]);
Challenging Exercise 2
To facilitate a thorough net surfing, any web browser has back and forward buttons that allow the user
to move backward and forward through a series of web pages. To allow the user to move both forward
and backward two stacks are employed. When the user presses the back button, the link to the current
web page is stored on a separate stack for the forward button. As the user moves backward through a
series of previous pages, the link to each page is moved in turn from the back to the forward stack.
When the user presses the forward button, the action is the reverse of the back button. Now the item
from the forward stack is popped, and becomes the current web page. The previous web page is
pushed on the back stack. Simulate the functioning of these buttons using array implementation of
Stack. Also provide options for displaying the contents of both the stacks whenever required.
Middle Level: Implement web browser navigation using both the stacks [2 Marks]
High Level: Use a single array to implement both the stacks [2 Marks]