Week 8 Stack
Week 8 Stack
Week#08
Stack and Its Implementation
Contact Info:
Room No: 112
Email: [email protected]
1
"stack n.
The set of things a person has to do in the future. "I haven't done it
yet because every time I pop my stack something new gets
pushed." If you are interrupted several times in the middle of a
conversation, "My stack overflowed" means "I forget what we
were talking about."
Stacks
Friedrich L. Bauer
German computer scientist
who proposed "stack method
of expression evaluation"
in 1955.
2
Introduction to Stack
• A stack is a linear structure in which items may be added
and removed only on one end.
3
Real-life Application of Stack
•Three everyday examples of such a structure
Stack of dishes
Stack of pennies
Stack of folded towels
4
Application of Stack in computer Science
•Stack is used in
Recursion
Expression Evaluation
String Comparison
Function calling
Memory manipulation to store address
Tree manipulation
Etc.
5
Problems that Use Stacks
CS 307
Computer Science
Fundamentals of
• The runtime stack used by a process (running program) to
keep track of methods in progress
• Search problems
• Undo, redo, back, forward
Stacks
6
Stack Representation
• Two basic operations associated with stacks
7
Stack Representation
• Example 6.1
• Suppose the following 6 elements are pushed, in order, onto an
empty stack:
AAA, BBB, CCC, DDD, EEE, FFF
There are three ways of picturing such a stack
9
Array Representation of Stack
• Stack may be represented in computer in various ways, by means of a
one-way list or a linear array.
• Each of the stacks are maintained by:
STACK = A linear array named
TOP = A pointer variable that contains the location of the top
element of the stack
MAXSTK = A variable that gives maximum number of elements
that can be held by the stack
10
Operations on STACK
• The operation of adding into and removing an item from a STACK
can be implemented by using following procedures
PUSH (Adding into STACK)
POP (Removing from STACK)
PRECONDITIONS:
• To execute the procedure PUSH, the precondition is to check whether
there is a room in STACK if not OVERFLOW
TOP = MAXSTK then OVERFLOW
11
Algorithms for PUSH and POP
• TOP and MAXSTK are global variables, the procedure may use only
PUSH(STACK, ITEM) and POP(STACK, ITEM)
• Note: the value of TOP is changed before insertion in PUSH and the value of TOP
is changed after deletion in POP
12
PUSH and POP in STACK
Example 6.2
13
Minimizing Overflow
• Essential difference between underflow and overflow in dealing stacks
14
Minimizing Overflow
15
Arithmetic Expressions
• Let Q be an arithmetic expression involving constants and
operations.
• Binary operations in Q may have different level of precedence.
• We assume the following three levels of precedence for the
usual five binary operations:
16
Arithmetic Expressions
17
Arithmetic Expressions: Notations
Infix Notation
• For most of the arithmetic operations, the operator symbol is
placed between its two operands.
For example:
A+B C-D E*F (G/H) +A
•With notation
(A+B)* C and A+(B*C)
18
Arithmetic Expressions: Notations
INFIX PREFIX
(A+B)*C [+AB]*C =*+ABC
A+(B*C) A+[*BC]=+A*BC
(A+B)/(C-D) [+AB]/[-CD]= /+AB-CD
•Refersto the analogous notation in which the operator symbol is placed after its
two operands
AB+ CD- EF* GHA/+
[+AB]/C*D-E
/[+AB]C*D-E
*/[+AB]CD-E
-*/+ABCDE
20
Evaluation of Postfix Expression using Stack
21
Evaluation of Postfix Expression using Stack
22
Evaluation of Postfix Expression using Stack
Symbol STACK
5
6
2
+
*
12
4
/
-
)
23
Evaluation of Postfix Expression using Stack
24
Rules for transforming from Infix to Postfix
• Rule#02: If same precedence operator are in the form +(- then its ok.
• Rule#03: if operator in a parenthesis occur then POP the operator (+) or
(*)
• Rule#04: if both operators in a parenthesis e.g. (-*) then POP using
LIFO.
25
Transforming Infix into Postfix Expression
(A+B/C*(D+E)-F)
Symbol Stack Postfix
(
A
+
B
/
C
*
(
D
+
E
)
-
F
)
26
Transforming Infix into Postfix Expressions
27
Transforming Infix into Postfix Expressions
28
Transforming Infix into Postfix Expressions
29