Professional Documents
Culture Documents
ASD 2 - Stack p. 1
What is a Stack?
a data structure whose elements can only be accessed from the top
ASD 2 - Stack p. 2
What is a Stack?
a data structure whose elements can only be accessed from the top insertion and deletion of its top element
ASD 2 - Stack p. 2
What is a Stack?
a data structure whose elements can only be accessed from the top insertion and deletion of its top element LIFO: Last In First Out
ASD 2 - Stack p. 2
Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack
ASD 2 - Stack p. 3
Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack repeat
ASD 2 - Stack p. 3
Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack repeat get the topmost dirty dish
ASD 2 - Stack p. 3
Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack repeat get the topmost dirty dish wash it
ASD 2 - Stack p. 3
Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack repeat get the topmost dirty dish wash it put it on top of the clean dishes stack
ASD 2 - Stack p. 3
Analogy
Washing dishes: given a stack of dirty dishes, wash them and put them on clean dishes stack repeat get the topmost dirty dish wash it put it on top of the clean dishes stack until no more dirty dishes
ASD 2 - Stack p. 3
ASD 2 - Stack p. 4
ASD 2 - Stack p. 4
ASD 2 - Stack p. 4
ASD 2 - Stack p. 4
ASD 2 - Stack p. 4
ASD 2 - Stack p. 4
ASD 2 - Stack p. 4
Operations on Stack
isEmpty() = true
ASD 2 - Stack p. 5
Operations on Stack
isEmpty() = true false Push(0)
ASD 2 - Stack p. 5
Operations on Stack
isEmpty() = true false Push(0) Push(1) 1 0
ASD 2 - Stack p. 5
Operations on Stack
isEmpty() = true false
ll Y 1 $ 2 @
ASD 2 - Stack p. 5
Operations on Stack
isEmpty() = true true false
l l Y 1 $ 2 @ ll Y 0 $ 2 @
ASD 2 - Stack p. 5
Stack Implementation
Java has provided stack class
ASD 2 - Stack p. 6
Stack Implementation
Java has provided stack class We can still implement our own stack for special purpose
ASD 2 - Stack p. 6
Stack Implementation
Java has provided stack class We can still implement our own stack for special purpose linked list vs. array
ASD 2 - Stack p. 6
ASD 2 - Stack p. 7
Example 1: Reversal
sequentially read all characters of a string and push them to a stack
ASD 2 - Stack p. 8
Example 1: Reversal
sequentially read all characters of a string and push them to a stack pop the stack and output the character until the stack is empty
ASD 2 - Stack p. 8
Example 1: Reversal
sequentially read all characters of a string and push them to a stack pop the stack and output the character until the stack is empty e.g. checking palindrome
ASD 2 - Stack p. 8
ASD 2 - Stack p. 9
ASD 2 - Stack p. 9
ASD 2 - Stack p. 9
ASD 2 - Stack p. 9
Example 3: Calculator
Binary vs. unary operators binary: +, -, *, /, unary: sin, cos, tan, ln, exp Operator precedence must be respected
ASD 2 - Stack p. 10
Inx Example 1
3*5+4
ASD 2 - Stack p. 11
Inx Example 1
3*5+4
ASD 2 - Stack p. 11
Inx Example 1
3*5+4
ASD 2 - Stack p. 11
Inx Example 1
3*5+4
5 3 *
ASD 2 - Stack p. 11
Inx Example 1
3*5+4 Precedence of + is lower than precedence of * 5 3 *
ASD 2 - Stack p. 11
Inx Example 1
3*5+4
15
ASD 2 - Stack p. 12
Inx Example 1
3*5+4
4 15 +
ASD 2 - Stack p. 12
Inx Example 1
19
ASD 2 - Stack p. 13
Inx Example 2
3+5*4
ASD 2 - Stack p. 14
Inx Example 2
3+5*4
ASD 2 - Stack p. 14
Inx Example 2
3+5*4
ASD 2 - Stack p. 14
Inx Example 2
3+5*4
5 3 +
ASD 2 - Stack p. 14
Inx Example 2
3+5*4 Precedence of * is higher than precedence of + 5 3 +
ASD 2 - Stack p. 14
Inx Example 2
3+5*4 Precedence of * is higher than precedence of + 5 3 * +
ASD 2 - Stack p. 14
Inx Example 2
3+5*4 Precedence of * is higher than precedence of + 4 5 3 * +
ASD 2 - Stack p. 14
Inx Example 2
20 3 +
ASD 2 - Stack p. 15
Inx Example 2
23
20 $ 2 @ 3 $ 2 @
ASD 2 - Stack p. 15
Inx Example 3
3 * (5 + 4)
ASD 2 - Stack p. 16
Inx Example 3
3 * (5 + 4)
ASD 2 - Stack p. 16
Inx Example 3
3 * (5 + 4)
ASD 2 - Stack p. 16
Inx Example 3
3 * (5 + 4)
( 3 *
ASD 2 - Stack p. 16
Inx Example 3
3 * (5 + 4)
5 3
( *
ASD 2 - Stack p. 16
Inx Example 3
3 * (5 + 4) Precedence of + is higher than precedence of ( 5 3 ( *
ASD 2 - Stack p. 16
Inx Example 3
3 * (5 + 4) Precedence of + is higher than precedence of ( + 5 3 ( *
ASD 2 - Stack p. 16
Inx Example 3
3 * (5 + 4) Precedence of + is higher than precedence of ( 4 5 3 + ( *
ASD 2 - Stack p. 16
Inx Example 3
3 * (5 + 4) Pop and operate until ( is found 4 5 3 + ( *
ASD 2 - Stack p. 17
Inx Example 3
3 * (5 + 4) Pop and operate until ( is found 4 9 5 3 + ( *
ASD 2 - Stack p. 17
Inx Example 3
9 3 *
ASD 2 - Stack p. 18
Inx Example 3
27
9 $ 2 @ 3 $ 2 @ $ 2 @ * $ 2 @
ASD 2 - Stack p. 18
Postx Notation
Another alternative notation: postx notation, where the two operands precede the operator As also the case in prex notation, parantheses are not needed in postx notation also known as Reverse Polish notation, invented by Australian philosopher and computer scientist Charles Hamblin in the mid 1950s Inx 1 * (3 + 5) 3 + 5 * 4 - (10 * 5) (1 * 3) / (4 - 50 / 5) Postx 135+* 3 5 4 * + 10 5 * 1 3 * 4 50 5 / - /
ASD 2 - Stack p. 20
Exercises
1. If symbol * represents POP and other symbols represent PUSH, then write down the ouput of POP operations of this sequence: P*U*S*H*DA*N**POP***. 2. Insert symbol * into STACK in order to output the following sequences: (or show that the sequence is impossible to output) STACK KCATS TACKS TCKAS AKTCS KCSTA
ASD 2 - Stack p. 21
Exercises (2)
3. Reverse the content of a stack with the help of 2 additional stacks with the help of another stack and a few variables 4. Sort (output) the content of a stack with the help of 2 additional stacks with the help of another stack and a few variables
ASD 2 - Stack p. 22
Exercises (3)
5. Transform an inx expression to postx using a stack 6. Transform an inx expression to prex using two stacks 7. Dene functions (push, pop, top, isEmpty) for two stacks implemented in an array such that no collision may occur
ASD 2 - Stack p. 23