0% found this document useful (0 votes)
43 views

Lect 8-Stack ADT

A stack is a linear data structure that follows the LIFO (last-in, first-out) principle. Elements can only be inserted and removed from one end of the stack, called the top. Common operations on a stack include push to insert and pop to remove elements. Converting expressions from infix to postfix notation using a stack avoids issues with operator precedence and parentheses. The conversion algorithm involves pushing operators onto a stack and outputting operands until an operator has higher precedence, at which point the operator is outputted instead.

Uploaded by

kaushik.gr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Lect 8-Stack ADT

A stack is a linear data structure that follows the LIFO (last-in, first-out) principle. Elements can only be inserted and removed from one end of the stack, called the top. Common operations on a stack include push to insert and pop to remove elements. Converting expressions from infix to postfix notation using a stack avoids issues with operator precedence and parentheses. The conversion algorithm involves pushing operators onto a stack and outputting operands until an operator has higher precedence, at which point the operator is outputted instead.

Uploaded by

kaushik.gr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

STACK ADT

Umarani Jayaraman- 19th Aug 2020


Linear/ Non-linear DS
•2

 Linear data structure: Data can be stored and


accessed in a sequential fashion
 Non-linear data structure: Data can be stored and
accessed in a non-sequential (hierarchical) fashion
Linear/ Non-linear DS
•3
Stack
 A stack is an ordered collection of homogeneous data
elements where insertion and deletion operations take
place at one end only
 Like an array and linked list, stack is also linear data
structure
 Stack ADT
 Push(item): insert an item into a stack
 Pop(): delete an item from a stack
 IsEmpty(): check a stack is empty or not
 IsFull(): check a stack is full or not
Representation of a stack
Array ADT- Push()
ALGORITHM push(A[0,….n-1], ITEM)
// I/P: The new item ITEM to be pushed into stack
// O/P: A stack with a newly pushed ITEM at the TOP position
// D/s: An array A with TOP as the pointer
top=-1
If (top >= n-1 then)
Print “stack is full”
Else
top=top+1
A[top]=ITEM
End If
End Algorithm
Array ADT- pop()
ALGORITHM pop(A[0,….n-1])
// I/P: A stack with ITEMs
// O/P: remove an ITEM from the top of stack
// D/s: An array A with TOP as the pointer

If (top < 0 then)


Print “stack is empty”
Else
ITEM= A[top]
top=top-1
End If
End Algorithm
Application of stack
 Expression conversion
 Expression evaluation
 Function calling
 Recursive function
 Parsing
Arithmetic expression
 A+ B* C/D– E ^F*G
 The problem to evaluate
this expression is in
which order it should
evaluate?
 In order to solve this
problem, we follow
operator precedence and
associative rule
Arithmetic expression (contd)
4+2*3/6–2^2*1
Scan 1: 4 + 2 * 3 / 6 – (2 ^ 2) *1
Scan 2: 4 + (2 * 3) / 6 – 4 *1
Scan 3: 4 + (6 / 6) – 4 *1
Scan 4: 4 + 1 – (4 *1)
Scan 5:(4+1) – 4
Scan 6: (5 – 4 )
Scan 7: 1
Arithmetic expression (contd)
 The problem with this conventional method also call it as
infix notations are
 We must scan the expression from left to right repeatedly
 Infix notation requires precedence and associativity rules
need to be followed
 Ambiguity is resolved using parenthesis
 These problem can be solved int the following two steps
 Conversion of a given expression into a special notation
where there is no parenthesis
 Evaluation of a special notation using stack
Special notation(s)
•12

 The conventional way of writing an expression is called infix


 Infix
 <operand> <op> <operand>

 A+B, C-D, E*F, G/H

 Prefix (polish notation)


 <op> <operand> <operand>

 +AB, -CD, *EF, /GH

 Postfix (reverse polish notation)


 <operand> <operand> <op>

 AB+, CD-, EF*, GH/


Need for prefix/ postfix
 Entirely unambiguous.
 Parentheses free
 In exactly one scan it is possible to evaluate the
given prefix/postfix notation
Infix to prefix and postfix conversion

 Moving Operators to the Right for Postfix Notation

postfix: ABC *+
 Moving Operators to the Left for Prefix Notation

prefix: +A*BC
Infix to prefix/postfix conversion
 Infix: A + B * C + D  Infix: A * B + C * D
 Prefix: + + A * B C D  Prefix: + * A B * C D
 Postfix: A B C * + D +  Postfix: A B * C D * +

 Infix: (A + B) * (C +  Infix: A + B + C + D
D)  Prefix: + + + A B C D
 Prefix: * + A B + C D  Postfix: A B + C + D
 Postfix: A B + C D + * +
Convert infix to postfix using stack
 Convert A * (B + C) * D to postfix notation.

Current Stack (grows


Move Output
Token toward left)

1 A empty A
2 * * A
3 ( (* A
4 B (* AB
5 + +(* AB
6 C +(* ABC
7 ) * ABC+
8 * * ABC+*
9 D * A B C + * D*
10 empty  
Convert infix to postfix
 (( A – ( B + C ) ) * D ) ^ ( E + F )
Evaluation of postfix expression
 Evaluate the expression 2 3 4 + * 5 * which was
created by the previous algorithm for infix to
postfix.
Stack (grows toward
Move Current Token
left)
1 2   2
2 3   32
3 4   432
4 +   72
5 *   14
6 5   5 14
7 *   70
Stack S, char ch, element;
while( tokens are available)
Algorithm for Infix to
{
ch=read(token)
postfix conversion
if (ch is operand) using stack
print ch
else if (ch is ‘(‘ )
push (S, ch)
else if (ch is ‘)’ )
{
while((element =pop(S)) ≠ ‘(‘)
print ch
}
else
{
while (priority (top of stack) ≥ priority (ch))
{
element = pop(S);
print (element);
}
push(S, ch);
}
} // end while tokens are not available
while( !empty(S))
{
element =pop(S);
print (element);
}
Summary
•20

 Stack ADT
 Infix to postfix
 Examples
 Algorithm for Infix to postfix

You might also like