Вы находитесь на странице: 1из 40

Assignment No 1

Ms. Ashwini Abhale

Implement stack as an abstract data


type using linked list and use this
ADT
for
conversion
of
Infix
expression to postfix and evaluation
of postfix expression

Stack as an ADT using


Linked list
LIFO

Last in first out

typedef struct node


{
char data;
struct node *next;
} node;

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

Infix to postfix Converstion


1) Accept Infix Expression
2) Scan infix expression from left to right char by
char till \0 (end of string)
3) If current char is operand
move that token in postfix expression
4) If Current char(token) is operator
check precedence of current operator(token) and top operator
of stack
If precedence of current operator(token) > precedence of
top symbol then push token into stack
Else if precedence of current operator(token) <= precedence
of top symbol then pop all operator from the stack till
precedence of current operator(token) > precedence of top
symbol or stack is empty

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

cout<<\n Enter Infix


expression;
cin>>infix;
0

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

Pop all the operator from the stack


While(!s.empty())
{
x=s.pop();
postfix[j++]=x;
}
Postfix[j]=\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

) : pop all operator till (

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

Scan postfix expression from left to


right
If operand then push into stack
Else //operator
Pop two operand accept the
value and evaluate
Push result again into stack
Final result will be in stack

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);
}

Pop two operand


Op2 = 4
Op1 = 2
Val = 2*4 =8

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);
}

Pop two operand


Op2 = 8
Op1 = 3
Val = 3+8 =11

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);
}

Pop two operand


Op2 = 5
Op1 = 1
Val = 1/5 =0

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);
}

Pop two operand


Op2 = 0
Op1 = 11
Val = 11-0 =11

11

\0

Val=s.pop();

//Val=11

A=3
B=2
C=4
D=1
F=5