Chapter 6 Stacks 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 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 Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-4 FIGURE 6.1 A conceptual view of a stack Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-5 FIGURE 6.2 The operations on a stack Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-6 FIGURE 6.3 The StackADT interface in UML Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-7 Listing 6.1 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 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 written 3 4 + 2 * 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 [...]... 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 Copyright © 2005 Pearson 6-22 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... © 2005 Pearson 6-30 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 Copyright © 2005 Pearson... stack Copyright © 2005 Pearson 6-31 FIGURE 6.6 A linked implementation of a stack Copyright © 2005 Pearson 6-32 LinkedStack - the push Operation Copyright © 2005 Pearson 6-33 FIGURE 6.7 The stack after pushing element E Copyright © 2005 Pearson 6-34 LinkedStack - the pop Operation Copyright © 2005 Pearson 6-35 FIGURE 6.8 The stack after a pop operation Copyright © 2005 Pearson 6-36 ... expression program Copyright © 2005 Pearson 6-20 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 Copyright © 2005 Pearson 6-21 Using Stacks - Traversing a Maze • Run-time environments keep...FIGURE 6.4 Using a stack to evaluate a postfix expression Copyright © 2005 Pearson 6-11 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 Copyright © 2005 Pearson 6-12 Listing 6.2 Copyright © 2005 . Chapter 6 Stacks 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. solve problems • Examine various stack implementations • Compare stack implementations Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-3 Stacks • A stack is a linear collection. 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,