Lec5 Stacks
Lec5 Stacks
Stacks
• ADT
• Implementation(s)
• Applications
– Checking for Balanced Parentheses, Brackets, and Braces in
an Infix Algebraic Expression
– Transforming an Infix Expression to a Postfix Expression
– Evaluating Postfix Expressions
– Evaluating Infix Expressions
What is a stack?
• It is an ordered group of homogeneous items
• Stack principle: LAST IN FIRST OUT = LIFO
– It means: the last element inserted is the first one to be removed
• Only access to the stack is the top element
– consider trays in a cafeteria
• to get the bottom tray out, you must first remove all the elements above
Stack
• Push
– the operation to place a new item at the top of the stack
• Pop
– the operation to remove the next item from the top of the
stack
Stack
M
C C C
R push(M) R item = pop() R
X X item = M X
A A A
Stack Applications
• Real life
–Pile of books
–Plate trays
• More applications related to computer science
–Program execution stack
–Evaluating expressions
Array-based Stack Implementation
• Allocate an array of some size (pre-defined)
– Maximum N elements in stack
• Bottom stack element stored at element 0
• last index in the array is the top
• Increment top when one element is pushed, decrement
after pop
Stack Implementation: CreateS, isEmpty, isFull
element stack[MAX_STACK_SIZE];
int top = -1;
Boolean isEmpty(Stack) { if(top< 0) return FALSE; }
Boolean isFull(Stack) { if (top >= MAX_STACK_SIZE-1) return FALSE;
Push
• Advantages:
– always constant time to push or pop an element
– can grow to an infinite size
• Disadvantages
– the common case is the slowest of all the implementations
• Basic implementation
– list is initially empty
– push( ) method adds a new item to the head of the list
– pop( ) method removes the head of the list
Additional Notes
postfixVect
Infix
stackVect
to postfix conversion
infixVect
a+b-c)*d–(e+f)
postfixVect
(
Infix
stackVect
to postfix conversion
infixVect
+b-c)*d–(e+f)
postfixVect
a
(
Infix
stackVect
to postfix conversion
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
Infix
stackVect
to postfix conversion
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
Infix
stackVect
to postfix conversion
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
Infix
stackVect
to postfix conversion
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
Infix
stackVect
to postfix conversion
infixVect
*d–(e+f)
postfixVect
ab+c-
Infix
stackVect
to postfix conversion
infixVect
d–(e+f)
postfixVect
ab+c-
*
Infix
stackVect
to postfix conversion
infixVect
–(e+f)
postfixVect
ab+c-d
*
Infix
stackVect
to postfix conversion
infixVect
(e+f)
postfixVect
ab+c–d*
-
Infix
stackVect
to postfix conversion
infixVect
e+f)
postfixVect
ab+c–d*
(
-
Infix
stackVect
to postfix conversion
infixVect
+f)
postfixVect
ab+c–d*e
(
-
Infix
stackVect
to postfix conversion
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
Infix
stackVect
to postfix conversion
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
Infix
stackVect
to postfix conversion
infixVect
postfixVect
ab+c–d*ef+
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+-
Another Example: Transforming Infix to Postfix
50
The Program Stack
The program stack at 3 points in time; (a) when main begins execution;
(b) when methodA begins execution, (c) when methodB begins execution.
Recursive Methods
• A recursive method making many recursive calls
– Places many activation records in the program stack
– Thus, the reason recursive methods can use much memory
• Possible to replace recursion with iteration by using a
stack
Slides and figures have been collected from various publicly
available Internet sources for preparing the lecture slides of
IT2001 course. I acknowledge and thank all the original authors
for their contribution to prepare the content.