Upcoming SlideShare
×

# Ch06 Stack

2,352 views

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
2,352
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
89
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Ch06 Stack

1. 1. Chapter 6 Stacks
2. 2. Chapter Objectives <ul><li>Examine stack processing </li></ul><ul><li>Define a stack abstract data type </li></ul><ul><li>Demonstrate how a stack can be used to solve problems </li></ul><ul><li>Examine various stack implementations </li></ul><ul><li>Compare stack implementations </li></ul>
3. 3. Stacks <ul><li>A stack is a linear collection whose elements are added and removed from one end </li></ul><ul><li>A stack is LIFO – last in, first out </li></ul><ul><li>The last element to be put on the stack is the first element to be removed </li></ul><ul><li>A stack is usually depicted vertically, with additions and deletions occurring at the top of the stack </li></ul>
4. 4. FIGURE 6.1 A conceptual view of a stack
5. 5. FIGURE 6.2 The operations on a stack
6. 6. FIGURE 6.3 The StackADT interface in UML
7. 7. Listing 6.1
8. 8. Using Stacks <ul><li>Stacks are particularly helpful when solving certain types of problems </li></ul><ul><li>Consider the undo operation in an application </li></ul><ul><ul><li>keeps track of the most recent operations in reverse order </li></ul></ul>
9. 9. Postfix Expressions <ul><li>Let's examine a program that uses a stack to evaluate postfix expressions </li></ul><ul><li>In a postfix expression, the operator comes after its two operands </li></ul><ul><li>We generally use infix notation, with parentheses to force precedence: </li></ul><ul><li>(3 + 4) * 2 </li></ul><ul><li>In postfix notation, this would be written </li></ul><ul><li>3 4 + 2 * </li></ul>
10. 10. Postfix Expressions <ul><li>To evaluate a postfix expression: </li></ul><ul><ul><li>scan from left to right, determining if the next token is an operator or operand </li></ul></ul><ul><ul><li>if it is an operand, push it on the stack </li></ul></ul><ul><ul><li>if it is an operator, pop the stack twice to get the two operands, perform the operation, and push the result onto the stack </li></ul></ul><ul><li>At the end, there will be one value on the stack, which is the value of the expression </li></ul>
11. 11. FIGURE 6.4 Using a stack to evaluate a postfix expression
12. 12. Postfix Expressions <ul><li>To simplify the example, let's assume the operands to the expressions are integer literals </li></ul><ul><li>Our solution uses an ArrayStack , though any implementation of a stack would suffice </li></ul>
13. 13. Listing 6.2
14. 14. Listing 6.2 (cont.)
15. 15. Listing 6.3
16. 16. Listing 6.3 (cont.)
17. 17. Listing 6.3 (cont.)
18. 18. Listing 6.3 (cont.)
19. 19. Listing 6.3 (cont.)
20. 20. FIGURE 6.5 A UML class diagram for the postfix expression program
21. 21. Using Stacks - Traversing a Maze <ul><li>A classic use of a stack is to keep track of alternatives in maze traversal or other trial and error algorithms </li></ul><ul><li>Using a stack in this way simulates recursion </li></ul><ul><ul><li>Recursion is when a method calls itself either directly or indirectly </li></ul></ul>
22. 22. Using Stacks - Traversing a Maze <ul><li>Run-time environments keep track of method calls by placing an activation record for each called method on the run-time stack </li></ul><ul><li>When a method completes execution, it is popped from the stack and control returns to the method that called it </li></ul><ul><ul><li>Which is now the activation record on the top of the stack </li></ul></ul>
23. 23. Using Stacks - Traversing a Maze <ul><li>In this manner, we can traverse a maze by trial and error by using a stack to keep track of moves that have not yet been tried </li></ul>
24. 24. Listing 6.4
25. 25. Listing 6.4 (cont.)
26. 26. Listing 6.4 (cont.)
27. 27. Listing 6.4 (cont.)
28. 28. Listing 6.4 (cont.)
29. 29. Listing 6.5
30. 30. Listing 6.5 (cont.)
31. 31. The LinkedStack Class <ul><li>Now let's examine a linked implementation of a stack </li></ul><ul><li>We will reuse the LinearNode class that we used in Chapter 3 to define the linked implementation of a set collection </li></ul><ul><li>Internally, a stack is represented as a linked list of nodes, with a reference to the top of the stack and an integer count of the number of nodes in the stack </li></ul>
32. 32. FIGURE 6.6 A linked implementation of a stack
33. 33. LinkedStack - the push Operation
34. 34. FIGURE 6.7 The stack after pushing element E
35. 35. LinkedStack - the pop Operation
36. 36. FIGURE 6.8 The stack after a pop operation
37. 37. The ArrayStack Class <ul><li>Now let's examine an array-based implementation of a stack </li></ul><ul><li>We'll make the following design decisions: </li></ul><ul><ul><li>maintain an array of Object references </li></ul></ul><ul><ul><li>the bottom of the stack is at index 0 </li></ul></ul><ul><ul><li>the elements of the stack are in order and contiguous </li></ul></ul><ul><ul><li>an integer variable top stores the index of the next available slot in the array </li></ul></ul><ul><li>This approach allows the stack to grow and shrink at the higher indexes </li></ul>
38. 38. FIGURE 6.9 An array implementation of a stack
39. 39. ArrayStack - the push Operation //----------------------------------------------------------------- // Adds the specified element to the top of the stack, expanding // the capacity of the stack array if necessary. //----------------------------------------------------------------- public void push (T element) { if (size() == stack.length) expandCapacity(); stack[top] = element; top++; }
40. 40. FIGURE 6.10 The stack after pushing element E
41. 41. ArrayStack - the pop Operation
42. 42. FIGURE 6.11 The stack after popping the top element
43. 43. The java.util.Stack Class <ul><li>The Java Collections framework defines a Stack class with similar operations </li></ul><ul><li>It is derived from the Vector class and therefore has some characteristics that are not appropriate for a pure stack </li></ul><ul><li>The java.util.Stack class has been around since the original version of Java, and has been retrofitted to meld with the Collections framework </li></ul>
44. 44. FIGURE 6.12 A UML description of the java.util.Stack class
45. 45. Analysis of Stack Operations <ul><li>Because stack operations all work on one end of the collection, they are generally efficient </li></ul><ul><li>The push and pop operations, for both linked and array implementations, are O(1) </li></ul><ul><li>Likewise, the other operations for all implementations are O(1) </li></ul><ul><li>We'll see that other collections (which don't have that characteristic) aren't as efficient for all operations </li></ul>