Sunteți pe pagina 1din 23

Stacks and Applications

10/9/2009 Center for Development of Advanced Computing 1


Stacks
• Stack ADT
– It is a special kind of list
– Can only add/delete/look at one end commonly
referred to as top)

• Advantages
– They are simple, easy to understand
– Each operation is O(1)

10/9/2009 Center for Development of Advanced Computing 2


Stacks …
• Examples
– Pile of cards
– Cars in a driveway
• It is
– Push-down list
– Last In First Out (LIFO) list

10/9/2009 Center for Development of Advanced Computing 3


Stack ADT
• Data members
– Array, size of the stack and top of the stack

• Operations
– Push, pop, peep, IsEmpty, Is Full

10/9/2009 Center for Development of Advanced Computing 4


Cont …
class _stack_
{
int *st;
int size;
int tos;
public:
void push(int);
int pop();
int peep();
bool IsEmpty();
bool IsFull();
_stack_() ;
~_stack()_ ;
void display() ;
};
10/9/2009 Center for Development of Advanced Computing 5
Applications
• Function Activation Records
• Balancing symbols
• Expression evaluation (Postfix expression)
• Infix to prefix conversion
• Infix to postfix conversion …..

10/9/2009 Center for Development of Advanced Computing 6


App1: Activation records
• when a function is called in C++ (or any language):
– suspend the current execution sequence
– allocate space for parameters, locals, return value, …
– transfer control to the new function
• when the function terminates:
– deallocate parameters, locals, …
– transfer control back to the calling point (& possibly return a value)
• note: functions are LIFO entities
– main is called first, terminates last
– if main calls Foo and Foo calls Bar, then Bar terminates before Foo
which terminates before main
• a stack is a natural data structure for storing information
about function calls and the state of the execution

10/9/2009 Center for Development of Advanced Computing 7


Activation records
• an activation record stores info (parameters, locals, …) for
each invocation of a function
• when the function is called, an activation record is pushed
onto the stack
– when the function terminates, its activation record is popped
– note that the currently executing function is always at the top of the
stack

10/9/2009 Center for Development of Advanced Computing 8


Activation record .. recursion

10/9/2009 Center for Development of Advanced Computing 9


App 2: Balancing Symbols
• When analyzing arithmetic expressions, it is
important to determine whether an
expression is balanced with respect to
parentheses
– (a+b*(c/(d-e)))+(d/e)
• Problem is further complicated if braces or
brackets are used in conjunction with
parenthesis
• Solution is to use stacks!
10/9/2009 Center for Development of Advanced Computing 10
Balancing Symbols
• start with an empty stack of characters
• traverse the expression from left to right
– if next character is a left delimiter, push onto the
stack
– if next character is a right delimiter, must match
the top of the stack

10/9/2009 Center for Development of Advanced Computing 11


Balancing symbols:
(a+b*[c/{d-e}])+(f/g)
Character Operation Stack contents
( Push (
a,+, b, * Skip (
[ Push ([
c, / Skip ([
{ Push ([{
d, -, e Skip ([{
} Pop and match ([
] Pop and match (
) Pop and match Empty
+ Skip Empty
( Push (
f, /, g skip (
) Pop and match Empty
10/9/2009 End of string Is stack empty
Center for Development of Advanced Computing 12
Evaluation of arithmetic
expressions
• Prefix: + a b
• Infix: a + b (what we use in grammar school)
• Postfix: a b +

10/9/2009 Center for Development of Advanced Computing 13


App 3: Infix to Postfix
• Rules:
Conversion
– Start from left of the infix expression
– Operands immediately go directly to output – postfix expression
– Operators are pushed into the stack (including parenthesis)
• Priority of the operators (assume)
- precision 1: * /
- precision 0: + -
- precision -1: (
• Lower precedence operator cannot be on top of a higher precedence
operator
– If a lower precedence operator is encountered, pop the operators and
append them to the output postfix string till we encounter a lesser
precedence operator or stack is empty.
• If we encounter a right parenthesis, pop from stack until we get matching
left parenthesis. Do not output parenthesis.
• If the infix string reaches end of string, pop all the operators from stack
and append them to the output postfix expression string
10/9/2009 Center for Development of Advanced Computing 14
Example: a+b*c+(d*e+f)*g

10/9/2009 Center for Development of Advanced Computing 15


Infix to Postfix Example
A+B*C-D/E
Infix Operation on Stack postfix
stack
a Empty a
+ Push + a
b + ab
* Check and push +* ab
c +* abc
- Check and push - abc*+
d - abc*+d
/ Check and push -/ abc*+d
e -/ abc*+de
End of string Pop till empty abc*+de/-
10/9/2009 Center for Development of Advanced Computing 16
Infix to Postfix Example #2
A*B-(C+D)+E
Infix Operation on stack Stack postfix
a Empty a
* Push * a
b * ab
- Check and push - ab*
( Push -( ab*
c -( ab*c
+ Check and push -(+ ab*c
d -(+ ab*cd
) Pop and append to postfix - ab*cd+
till ‘(‘
+ Check and push + ab*cd+-
e + ab*cd+-e
End of string Pop till empty ab*cd+-e+
10/9/2009 Center for Development of Advanced Computing 17
App 4: Postfix Evaluation
Rules:
Operand: push
Operator: pop 2 operands, perform the operation, push result back
onto stack

10/9/2009 Center for Development of Advanced Computing 18


Evaluation : 123+*

Character Stack operation


1 1
2 12
3 123
+ 15 Pop 3, 2 ;
2+3 = 5
Push 5
* 5 Pop 5, 1;
1*5 = 5
Push 5
End of string Result is 5

10/9/2009 Center for Development of Advanced Computing 19


Evaluation : 6 5 2 3 + 8 * + 3 + *
Character Stack Operation
6 6
5 65
2 652
3 6523
+ 655 Pop 3, 2; 2+3 = 5 ; push 5
8 6558
* 6 5 40 Pop 8, 5; 5*8 = 40; push 40
+ 6 45 Pop 40, 5; 5+40 = 45; push 45
3 6 45 3
+ 6 48 Pop 3, 45; 45+3 = 48; push 48
* 288 Pop 48, 6; 6*48 = 288; push 288
End of string Result is 288
10/9/2009 Center for Development of Advanced Computing 20
Infix to Prefix conversion
• Rules:
– Start from right of the infix expression
– Operands immediately go directly to output – output expression
– Operators are pushed into the stack (including right parenthesis)
• Priority of the operators (assume)
- precision 1: * /
- precision 0: + -
- precision -1: )
• Lower precedence operator cannot be on top of a higher precedence
operator
– If a lower precedence operator is encountered, pop the operators and
append them to the output postfix string till we encounter a lesser or equal
precedence operator or stack is empty.
• If we encounter a left parenthesis, pop from stack until we get matching
right parenthesis. Do not output parenthesis.
• If the infix string reaches end of string, pop all the operators from stack
and append them to the output expression string.
• Now reverse the output expression string. This is the final prefix
expression
10/9/2009 Center for Development of Advanced Computing 21
Infix to Prefix Example #1
A*B-(C+D)+E
Infix Operation on stack Stack Output string
E Empty E
+ Push + E
) Push +) E
D +) ED
+ Check and push +)+ ED
C +)+ Edc
( Pop and append to o/p till + Edc+
’)’
- Check and push +- Edc+
B +- Edc+b
* Check and push +-* Edc+b
A +-* Edc+ba
End of string Pop till empty Edc+ba*-+

10/9/2009 Prefix expression


Center is: of+-*ab+cde
for Development Advanced Computing 22
Thank you

10/9/2009 Center for Development of Advanced Computing 23

S-ar putea să vă placă și