CHP 5 Stack
CHP 5 Stack
STACK
FUNDAMENTALS OF DATA STRUCTURES (CSC248)
PREPARED BY:
MISS MAZNIE MANAF
DEFINITION OF STACK
A type of list
in which data could be inserted and removed from only
one end of the list known as the top
has Last In First Out (LIFO) / First in Last Out (FILO)
characteristics
EXAMPLE OF STACK
INTRODUCTION
Top
node Bottom
THE USES OF STACK (ADVANTAGES)
myStack.push ("Ahmad");
myStack.push ("Nurul");
System.out.println(myStack.pop());
}
}
EXAMPLE 2
Determine the value of x and y for the following program segment. Then,
draw the latest diagram:
BEFORE AFTER
Write a program which receives one D D
string data, then insert every character
A T
from the string into a stack of type char
named alpha. And then, removes the T S
vowels from the stack. A T
S R
Example: DATASTRUCT T C
R T
U
C
T
EXERCISE 4
$ Right to Left
*, / Left to Right,
whichever comes
first
Lowest +, - Left to right,
whichever comes
first
ARITHMETIC EXPRESSIONS
A+B*C
(A+B) * C
A–B+C
A – (B+C)
CONVERTION
[$CA]
[ + [$CA] B]
Prefix: + $ C A B
CONVERTION
[CA$]
[ [CA$] B +]
Postfix: C A $ B +
Application of Stack 1:
Infix to Postfix
else
Pop the operatorStack object and append that popped operator to the postfix string
Infix to Postfix Conversion (Con’t)
Example 4
Convert this infix notation to postfix notation
a+c–r/b*r
Solution
Infix operatorStack Postfix
a empty a
+ + a
c c ac
- - ac +
r - ac + r
/ -/ ac + r
b -/ ac + rb
* -* ac + rb /
r -* ac + rb / r
- ac + rb / r *
empty ac + rb / r * -
Parentheses in Infix notation
When a right parentheses “)” is found in the infix string, the operator
object is repeatedly popped, and the popped element appended to the
postfix string, until the operator on the top of the operatorStack object is
the left paretheses “(“
Then the left parentheses “(“ is popped but not appended to the postfix
string and the scan of the infix string is continued.
Parentheses in Infix notation (cont)
Example 5
Convert this infix notation to postfix notation
x – (y * a / b – (z + d * e) + c) / f
Solution
Infix operatorStack Postfix
x empty x
- - x
( -( x
y -( xy
* -( * xy
a -( * xya
/ -( / xya *
b -( / xya * b
- -( - xya * b/
( -( -( xya * b/
z -( -( xya * b/z
Solution (cont..)
Infix operatorStack Postfix
+ -( -( + xya * b/z
d -( - ( + xya * b/zd
* -( - ( + * xya * b/zd
e -( - ( + * xya * b/zde
) -( - xya * b/zde *+
+ -( + xya * b/zde *+-
c -( + xya * b/zde *+ -c
) - xya * b/zde *+ -c +
/ -/ xya * b/zde *+ -c +
f -/ xya * b/zde *+ -c + f
- xya * b/zde *+- c + f /
Solution (cont..)
Infix operatorStack Postfix
- xya * b/zde *+- c + f /
empty xya * b/zde *+-c+ f /-
Exercise 5
2. If the operator has a higher precedence than the top operator on the
operatorStack object,
push the operator onto the operatorStack object
This process continues until we reach the end of the infix expression.
Infix to Prefix Conversion (cont..)
Repeat the following actions from case 3 until the operatorStack object is
empty:
a) Pop opt1 from the operatorStack object
b) Pop opnd1 and opnd2 from the operandStack object
c) Join together opt1, opnd2 and opnd1 and push the result onto the operandStack
object
Example 1:
Convert infix notation to prefix notation
a+b*c
Solution
Infix operandStack operatorStack
a a
+ +
b b
a +
* b *
a +
c c *
b +
a
* bc +
a +
+ a * bc
Infix to Prefix Conversion (cont..)
Example 2:
Convert infix notation to prefix notation
(a – b) * c
Solution
Infix operandStack operatorStack
( (
a a (
- a -
(
b b -
a (
) - ab (
* - ab *
c c *
- ab
* - abc
Infix to Prefix Conversion (cont..)
Example 3:
Convert infix notation to prefix notation
a + (c – h) / (b * d)
Solution
Infix operandStack operatorStack
a a
+ a +
( a (
+
c c (
a +
- c -
a (
+
h h -
c (
a +
Solution
Infix operandStack operatorStack
) - ch (
a +
/ - ch /
a +
( - ch (
a /
+
b b (
-ch /
a +
Solution
Infix operandStack operatorStack
* b *
- ch (
a /
+
d d *
b (
- ch /
a +
) *bd (
-ch /
a +
Solution
Process each data in postfix expression from left to right until there is no
more data
1. If data is an operand , push to stack
2. If data is an operator ,
pop data into operand1
pop data into operand2
RESULT = operand2 operator operand1
Push RESULT to stack
ANSWER:
Exercise : infix ( A+B )*( C $ D )
postfix
if A = 4, B= 3, C = 2 and D = 1
ANSWER:
EXERCISE 7