stack - cpe.ku.ac.th

52
STACK

Transcript of stack - cpe.ku.ac.th

Page 1: stack - cpe.ku.ac.th

STACK

Page 2: stack - cpe.ku.ac.th

Stack ADT2

A stack is an abstract data type based on the list data model All operations are performed at one end of the list called the top of the stack (TOS) “LIFO (for last-in first-out) list” is a synonym for stack Common operations: ! push(x) puts the element x on top of the stack ! pop removes the topmost element from the stack.

Page 3: stack - cpe.ku.ac.th

Java Stack Interface3

public interface Stack { public void push(Object x); public Object pop(); public Object peek(); public boolean isEmpty(); public void clear();

}

Page 4: stack - cpe.ku.ac.th

From interface to implementation

Given that we want to support some interface, the designer still faces a choice ! What will be the best way to implement this interface

for my expected type of use? ! Choice of implementation can reflect many

considerations

Major factors we think about ! Speed for typical use case ! Storage space required

4

Page 5: stack - cpe.ku.ac.th

Array implementation of Stack5

class ArrayStack implements Stack {

private Object[] array; //Array that holds the Stack

private int index = 0; //First empty slot in Stack

public ArrayStack(int maxSize)

{ array = new Object[maxSize]; }

public void push(Object x) { array[index++] = x; }

public Object pop() { return array[--index]; }

public Object peek() { return array[index-1]; }

public boolean isEmpty() { return index == 0; }

public void clear() { index = 0; }

}

max-1

3210

4index

O(1) worst-case time for each operation

Question: What can go wrong?

…. What if maxSize is too small?

Page 6: stack - cpe.ku.ac.th

Linked List Implementation of Stack

6

class ListStack implements Stack {

private Node head = null; //Head of list that

//holds the Stack

public void push(Object x) { head = new Node(x, head); }

public Object pop() {

Node temp = head;

head = head.next;

return temp.data;

}

public Object peek() { return head.data; }

public boolean isEmpty() { return head == null; }

public void clear() { head = null; }

}

head

O(1) worst-case time for each operation (but constant is larger)

Note that array implementation can overflow, but the linked list version cannot

Page 7: stack - cpe.ku.ac.th

Use of Stack7

Infix to postfix conversion Postfix evaluation Function calls etc

Page 8: stack - cpe.ku.ac.th

Infix to postfix

When an operand is read, output it When ( is read, push it When an operator is read: ! If TOS item has higher or equal precedence, repeatedly pop until TOS has an

element of lower precedence or stack empty, then push the operator ! If the stack is empty or TOS item has lower precedence, just push it

When ) is found, pop until we find the matching ( ( has the lowest precedence when in the stack but has the highest precedence when in the input When we reach the end of input, pop until the stack is empty

8

Page 9: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack:

Output:

9

Page 10: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: (

Output:

10

Page 11: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: (

Output: 8

11

Page 12: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( +

Output: 8

12

Page 13: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( +

Output: 8 9

13

Page 14: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + *

Output: 8 9

14

Page 15: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * (

Output: 8 9

15

Page 16: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * (

Output: 8 9 4

16

Page 17: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * ( +

Output: 8 9 4

17

Page 18: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * ( +

Output: 8 9 4 5

18

Page 19: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * ( + *

Output: 8 9 4 5

19

Page 20: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * ( + *

Output: 8 9 4 5 7

20

Page 21: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * ( +

Output: 8 9 4 5 7 *

21

Page 22: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * (

Output: 8 9 4 5 7 * +

22

Page 23: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * ( +

Output: 8 9 4 5 7 * +

23

Page 24: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * ( +

Output: 8 9 4 5 7 * + 6

24

Page 25: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + * (

Output: 8 9 4 5 7 * + 6 +

25

Page 26: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( + *

Output: 8 9 4 5 7 * + 6 +

26

Page 27: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: ( +

Output: 8 9 4 5 7 * + 6 + *

27

Page 28: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: (

Output: 8 9 4 5 7 * + 6 + * +

28

Page 29: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack:

Output: 8 9 4 5 7 * + 6 + * +

29

Page 30: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: +

Output: 8 9 4 5 7 * + 6 + * +

30

Page 31: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: +

Output: 8 9 4 5 7 * + 6 + * + 3

31

Page 32: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: + *

Output: 8 9 4 5 7 * + 6 + * + 3

32

Page 33: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: + *

Output: 8 9 4 5 7 * + 6 + * + 3 5

33

Page 34: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: +

Output: 8 9 4 5 7 * + 6 + * + 3 5 *

34

Page 35: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack:

Output: 8 9 4 5 7 * + 6 + * + 3 5 * +

35

Page 36: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: +

Output: 8 9 4 5 7 * + 6 + * + 3 5 * +

36

Page 37: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack: +

Output: 8 9 4 5 7 * + 6 + * + 3 5 * + 4

37

Page 38: stack - cpe.ku.ac.th

Example

(8 + 9 * (4 + 5 * 7 + 6)) + 3 * 5 + 4

Stack:

Output: 8 9 4 5 7 * + 6 + * + 3 5 * + 4 +

38

Page 39: stack - cpe.ku.ac.th

More Examples

Refer to the slides by Otávio Braga linked from the class website

39

Page 40: stack - cpe.ku.ac.th

Postfix evaluation

Starting with an empty stack, we scan the postfix expression from left to right Each time we encounter an argument, we push it onto the stack When we encounter an operator, we pop the stack twice, remembering the operands popped We then apply the operator to the two popped values (with the second as the left operand) and push the result onto the stack

40

Page 41: stack - cpe.ku.ac.th

Example41

Evaluating 3 4 + 2 *

Page 42: stack - cpe.ku.ac.th

Postfix to Expression Tree42

A 2 * 2 A * B * - B 2 * + A B - /

1. Push A 2. Push 2 3. Pop 2 4. Pop A 5. Create tree_1 6. Push tree_1 7. Push 2 8. Push A 9. Pop A 10. Pop 2 11. Create tree_2 12. Push tree_2 13. Push B 14. Pop B 15. Pop tree_2 16. Create tree_3 17. Push tree_3 18. Pop tree_3 19. Pop tree_1 20. Create tree_4 21. Push tree_4

tree_1

tree_2

tree_3

tree_4

22. Push B 23. Push 2 24. Pop 2 25. Pop B 26. Create tree_5 27. Push tree_5 28. Pop tree_5 29. Pop tree_4 30. Create tree_6 31. Push tree_6 32. Push A 33. Push B 34. Pop B 35. Pop A 36. Create tree_7 37. Push tree_7 38. Pop tree_7 39. Pop tree_6 40. Create tree_8 41. Push tree_8

tree_5

tree_6 tree_7

tree_8

Page 43: stack - cpe.ku.ac.th

Evaluating expression tree43

public static int eval(TreeCell root) { int left_result, right_result; if (root.datum is operand) return operand’s value; left_result = eval(root.left); right_result = eval(root.right); switch (root.datum) { case + : return left_result + right_result; case * : return left_result * right_result; : : } }

If at leaf node (operand node) return the operand’s value Otherwise:

Evaluate the left sub-tree Evaluate the right sub-tree Combine the results from both sub-trees with the operator at root

Page 44: stack - cpe.ku.ac.th

Function calls

An important application of stacks is normally hidden from view ! A stack is used to allocate space in the computer’s

memory to the variables belonging to the various functions of a program

Example: a recursive factorial function ! Has a parameter n and a return value ! As fact calls itself recursively, different calls with different

parameter are active at the same time ! A frame for each call is created and place on TOS ! When a given call return, its frame gets popped off

44

Page 45: stack - cpe.ku.ac.th

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; }

Page 46: stack - cpe.ku.ac.th

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; }

public static int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; }

Page 47: stack - cpe.ku.ac.th

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; }

public static int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; }

public static int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return fact; }

Page 48: stack - cpe.ku.ac.th

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; }

public static int factorial(int 2) { int fact; if (n > 1) fact = factorial(1) * 2; else fact = 1; return fact; }

public static int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return 1; }

Page 49: stack - cpe.ku.ac.th

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; }

public static int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return fact; }

public static int factorial(int 1) { int fact; if (n > 1) fact = factorial(n - 1) * n; else fact = 1; return 1; }

Page 50: stack - cpe.ku.ac.th

public static int factorial(int 3) { int fact; if (n > 1) fact = factorial(2) * 3; else fact = 1; return fact; }

public static int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return 2; }

Page 51: stack - cpe.ku.ac.th

public static int factorial(int 3) { int fact; if (n > 1) fact = 2 * 3; else fact = 1; return fact; }

public static int factorial(int 2) { int fact; if (n > 1) fact = 1 * 2; else fact = 1; return 2; }

Page 52: stack - cpe.ku.ac.th

public static int factorial(int 3) { int fact; if (n > 1) fact = 2 * 3; else fact = 1; return 6; }