0% found this document useful (0 votes)
46 views24 pages

Stacks and Applications: 05/11/11 Center For Development of Advanced Computing 1

Stacks are a special type of list that can only add or remove elements from one end, called the top. Stacks have efficient O(1) time operations and are useful for applications like function calls, expression evaluation, and balancing symbols. Key aspects of stacks include the LIFO property, push and pop operations, and using arrays and pointers to implement a stack data structure. Common applications include storing function activation records, checking balanced delimiters, converting infix to postfix notation, and evaluating postfix expressions.

Uploaded by

Ashutosh Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
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)
46 views24 pages

Stacks and Applications: 05/11/11 Center For Development of Advanced Computing 1

Stacks are a special type of list that can only add or remove elements from one end, called the top. Stacks have efficient O(1) time operations and are useful for applications like function calls, expression evaluation, and balancing symbols. Key aspects of stacks include the LIFO property, push and pop operations, and using arrays and pointers to implement a stack data structure. Common applications include storing function activation records, checking balanced delimiters, converting infix to postfix notation, and evaluating postfix expressions.

Uploaded by

Ashutosh Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 24

Stacks and Applications

05/11/11 Center for Development of Advanced Computing 1


Stacks
• Stack
– 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)

05/11/11 Center for Development of Advanced Computing 2


Stacks …
• Example
– Pile of books
• It is
– Push-down list
– Last In First Out (LIFO) list

05/11/11 Center for Development of Advanced Computing 3


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

• Operations
– Push: placing an element in the stack
– Pop : removing an element in the stack
– Peep: check the top most element
– IsEmpty
– Is Full
05/11/11 Center for Development of Advanced Computing 4
Example of a stack
• Consider the elements Initially
{1,2,3,4}, stack ‘s’ and -1 0 1 2 3 4 5
tos at -1 tos

• Push Push 1 1
– Increment tos and place -1 0 1 2 3 4 5
the element
tos

• Pop Pop
Push 4
2
3 1 2 3 4
– Take the element and -1 0 1 2 3 4 5
decrement the tos

05/11/11 Center for Development of Advanced Computing 5


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

05/11/11 Center for Development of Advanced Computing 6


Applications
• Function Activation Records
• Balancing symbols
• Expression evaluation (Postfix expression)
• Infix to prefix conversion
• Infix to postfix conversion …..

05/11/11 Center for Development of Advanced Computing 7


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

05/11/11 Center for Development of Advanced Computing 8


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

05/11/11 Center for Development of Advanced Computing 9


Activation Record : Recursion
Int fact(int n)
{
if (n==1)
return 1;
else
return(n x fact(n-1));
}

Int main()
{
int fact(int) ;
int fact_val = 0;
fact_val = fact(4) ;
return 0 ;
}
05/11/11 Center for Development of Advanced Computing 10
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!
05/11/11 Center for Development of Advanced Computing 11
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

05/11/11 Center for Development of Advanced Computing 12


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
05/11/11 End of string Is stack empty
Center for Development Balanced
of Advanced Computing 13
Evaluation of arithmetic
expressions
• Prefix: + a b
• Infix: a + b (what we use in grammar school)
• Postfix: a b +

05/11/11 Center for Development of Advanced Computing 14


App 3: Infix to Postfix Conversion
• Rules:
– 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 2: * /
- Precision 1: + -
- Precision 0: (
• 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

05/11/11 Center for Development of Advanced Computing 15


Example: a+b*c+(d*e+f)*g

05/11/11 Center for Development of Advanced Computing 16


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/-
05/11/11 Center for Development of Advanced Computing 17
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+
05/11/11 Center for Development of Advanced Computing 18
App 4: Postfix Evaluation
Rules:
Operand: push
Operator: pop 2 operands, perform the operation, push result back
onto stack

05/11/11 Center for Development of Advanced Computing 19


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

05/11/11 Center for Development of Advanced Computing 20


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

05/11/11 Center for Development of Advanced Computing 21


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 2: * /
- Precision 1: + -
- Precision 0: )
• 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
05/11/11 Center for Development of Advanced Computing 22
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*-+
Prefix expression is: +-*AB+CDE
05/11/11 Center for Development of Advanced Computing 23
Thank you

05/11/11 Center for Development of Advanced Computing 24

You might also like