Professional Documents
Culture Documents
(Chapter 4)
Outline
•Introduction
•Stack Operations
•Stack Implementation
•Implementation of Push and Pop operations
•Applications
Recursive Programming
Evaluation of Expressions
•ADT for stacks
3
A B C D
A B C
Stack Implementation
• A common and a basic method of implementing
stacks is to make use of another fundamental
data structure eg., arrays. While arrays are
sequential data structures the other alternative of
employing linked data structures have been
successfully attempted and applied.
end POP
8
Applications
Recursive Programming
During the execution of a recursive program, to keep track of the calls
made to itself and to record the status of the parameters at the time of
the call, a stack data structure is used
Now for tail recursion, since the recursive call turns out to be
the last executed statement, there is no need that the local
variables must be pushed into a stack for them to be
“remembered” and “restored” on termination of the
recursive call.
10
Evaluation of Expressions
abc d
Prefix expression <Operand> <Operator> < Operator >
a * bcd
11
Procedure EVAL_POSTFIX(E)
X = get_next_character (E);
/* get the next character of expression E */
case x of
:x is an operand: Push x into stack S;
Example 1
Consider a stack DEVICE[1:3] of peripheral devices. The insertion
of the four items PEN,PLOTTER, JOY STICK and PRINTER into
DEVICES and deletion.
Example 2
Consider infix expression a + b*c – d
Compute the equivalent postfix expression
Example 3
Consider infix expression (a * b – f * h) ↑ d
Compute the equivalent prefix expression
ADT for Stacks
Data objects:
A finite set of elements of the same type
Operations:
Create an empty stack and initialize top of stack
CREATE( STACK)
Check if stack is empty
CHK_STACK_EMPTY(STACK) (Boolean function)
Check if stack is full
CHK_STACK_FULL(STACK) (Boolean function)
Push ITEM into stack STACK
PUSH(STACK, ITEM)
Pop element from stack STACK and output the element popped in ITEM
POP(STACK, ITEM)