Escolar Documentos
Profissional Documentos
Cultura Documentos
node
data
Addres
s of
next
node
STACK as a ADT
class Stack
{
node *top;
stack()
{
top=NULL;
//Constructor
// initialize top as NULL indicate
// stack is empty
}
int isempty();
void push(char x);
char pop();
};
Check Empty
int stack::isempty()
{
if(top==NULL)
return 1;
return 0;
}
top
//true
//false
NUL
L
push(a)
Void stack::push(char x)
{
node *p;
p=new node();
p->data=x;
p->next=top;
top=p;
}
top
NUL
L
Before
Push
top
NULL
After
Push
push(b)
Void stack::push(char x)
{
node *p;
p=new node();
p->data=x;
p->next=top;
top=p;
}
top
a
NULL
push(c)
void stack::push(char x)
{
node *p;
p=new node();
p->data=x;
p->next=top;
top=p;
}
top
b
a
NULL
c=Pop()
char stack::pop() p
{
char x;
node *p;
p=top;
x=p->data;
top=top->next;
delete (p);
return x;
}
top
a
NULL
top
NULL
b=Pop()
char stack::pop()
{
char x;
node *p;
p=top;
x=p->data;
top=top->next;
delete (p);
return x;
}
top
a
NULL
top
NULL
a=Pop()
char stack::pop()
{
char x;
node *p;
p=top;
x=p->data;
top=top->next;
delete (p);
return x;
}
top
NULL
top
NULL
Precedence Function
int precedence(char x)
{
if(x==()
return 0;
if(x==+ || x==-)
return 1;
if(x==* || x==/ || x==%)
return 2;
return 3;
}
a+b*c-d/f \0
End of infix
expression
//declaration
char infix[30];
1) Accept infix expression
a+b*c-d/f \0
j=0;
stack s;
for(i=0;infix[i]!=\0;i++)
{
//read token
char token=infix[i];
//check operator or operand
if(isalnum(token))
{
postfix[j]=token;
j++;
}
else
{
//operator
}
}
a
j
//operand
a+b*c-d/f \0
//operator
else
{
0 1 2 3 4 5 6 7
if(token==()
a
s.push(token)
else
if(token==))
{
j
while((x=s.pop())!=()
postfix[j++]=x;
}
else
{
while(precedence(token) <= precedence(s.top>data)) && !s.empty())
{
x=s.pop();
postfix[j]=x;
j++;
}
s.push(token);
}
}
a+b*c-d/f \0
if(isalnum(token))
{
postfix[j]=token;
j++;
}
j
//operand
a+b*c-d/f \0
//operator
else
0 1 2 3 4 5 6
{
a b
if(token==()
s.push(token)
else
if(token==))
j
{
while((x=s.pop())!=()
postfix[j++]=x;
}
else
{
while(!s.empty() && precedence(token) <=
precedence(s.top->data)) &&
{
x=s.pop();
postfix[j++]=x;
}
s.push(token);
}
a+b*c-d/f \0
if(isalnum(token))
//operand
{
postfix[j]=token;
j++;
}
a+b*c-d/f \0
//operator
0 1 2 3 4 5 6 7 8 9
else
{
a b c * +
if(token==()
s.push(token)
else
if(token==))
j
{
while((x=s.pop())!=()
postfix[j++]=x;
}
else
{
while(precedence(token) <= precedence(s.top->data))
&& !s.empty())
{
x=s.pop();
postfix[j++]=x;
}
s.push(token);
}
a+b*c-d/f \0
if(isalnum(token))
{
postfix[j]=token;
j++;
}
//operand
a+b*c-d/f \0
//operator
else
0 1 2 3 4 5 6 7
{
a b c * + d
if(token==()
s.push(token)
else
if(token==))
j
{
while((x=s.pop())!=()
postfix[j++]=x;
}
else
{
while(precedence(token) <= precedence(s.top>data)) && !s.empty())
{
x=s.pop();
postfix[j++]=x;
}
s.push(token);
}
a+b*c-d/f \0
0
j
if(isalnum(token))
{
postfix[j]=token;
j++;
}
//operand
a+b*c-d/f \0
\0
a+(b-d)*(c-d/e)+f
Postfix:=a
Postfix:=abdPostfix:
=ab
Postfix:=abd
Operand = a add to postfix
expression
+ : operator push into stack
( : Push into stack
b: in postfix expression
- Push into stack
(
+
(
+
d : in postfix expression
a+(b-d)*(c-d/e)+f
Postfix:=a
Postfix:=abdbdcde
Postfix:=abd-cPostfix:=abdcde/-*
Postfix:=abdPostfix:=abd-cd
cde/-*+
*
+
(
*
+
(
*
+
(
*
+
(
*
+
(
*
+
*
+
a+(b-d)*(c-d/e)+f
Postfix:=abd-cde/*+f
Postfix:=abd-cde/*+f+
Evaluation of postfix
expression
\0
A=3
B=2
C=4
D=1
F=5
\0
for(i=0;postfix[i]!=\0;i++)
{
token=postfix[i];
if(isalpha(token))
{
cout<<Enter the value of
<<token<<=;
cin>>val1;
s.push(val1);
}
else
{
//operator
}
}
A=3
B=2
C=4
D=1
F=5
\0
for(i=0;postfix[i]!=\0;i++)
{
token=postfix[i];
A=3
B=2
C=4
D=1
F=5
if(isalpha(token))
{
cout<<Enter the value of
<<token<<=;
cin>>val1;
s.push(val1);
}
else
{
//operator
}
}
\0
for(i=0;postfix[i]!=\0;i++)
{
token=postfix[i];
A=3
B=2
C=4
D=1
F=5
if(isalpha(token))
{
cout<<Enter the value of
<<token<<=;
cin>>val1;
s.push(val1);
}
else
{
//operator
}
}
4
2
3
\0
Else
{
char op1, op2;
int val1, val2;
op2=s.pop();
op1=s.pop();
A=3
B=2
C=4
D=1
F=5
val=evaluate(op1,op2,token);
s.push(val);
}
8
3
\0
Else
{
char op1, op2;
int val1, val2;
op2=s.pop();
op1=s.pop();
A=3
B=2
C=4
D=1
F=5
val=evaluate(op1,op2,token);
s.push(val);
}
11
\0
A=3
B=2
C=4
D=1
F=5
if(isalpha(token))
{
cout<<Enter the value of
<<token<<=;
cin>>val1;
s.push(val1);
}
1
11
\0
A=3
B=2
C=4
D=1
F=5
if(isalpha(token))
{
cout<<Enter the value of
<<token<<=;
cin>>val1;
s.push(val1);
}
5
1
11
\0
Else
{
char op1, op2;
int val1, val2;
op2=s.pop();
op1=s.pop();
A=3
B=2
C=4
D=1
F=5
val=evaluate(op1,op2,token);
s.push(val);
}
0
11
\0
Else
{
char op1, op2;
int val1, val2;
op2=s.pop();
op1=s.pop();
A=3
B=2
C=4
D=1
F=5
val=evaluate(op1,op2,token);
s.push(val);
}
11
\0
Val=s.pop();
//Val=11
A=3
B=2
C=4
D=1
F=5