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