Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.

Similar presentations


Presentation on theme: "Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem."— Presentation transcript:

1 Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem Solving with JAVA: Walls and Mirrors Carrano / Prichard Stacks

2 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Create an empty stack. Determine whether a stack is empty. Add a new item to the stack. Remove from the stack the item that was added most recently. Remove all the items from the stack. Retrieve from the stack the item that was added most recently. ADT Stack Operations

3 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.1 Stack of cafeteria dishes

4 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley LIFO (Last-In, First-Out) - Stack FIFO (First-In, First-Out) - Queue ADT Stack Operations

5 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley createStack() // Creates an empty stack isEmpty() // Determines whether a stack is empty push(newItem) throws StackException // Adds newItem to the top of the stacks. Throws StackException if failed. pop() throws StackException // Retrieves and then removes the top of the stack. Throws StackException if not successful. popAll() // Removes all items from the stack peek() throws StackException //Retrieves the top of the stack. Retrieval does not change the stack. Throws Stack Exception if failed. Pseudocode for the ADT Stack Operations

6 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley displayBackward(stack) // Displays the input line in reversed order by writing the contents of stack // Writing the contents of stack. stack = readAndCorrect() while (!stack.isEmpty()) { newChar = stack.pop() Write newChar } // end while Advance to new line Using the ADT stack in a solution

7 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley readAndCorrect() // Reads the input line and returns the corrected version as a stack. // For each character read, either enters it into the stack or, if it is // ‘<-’, corrects the contents of stack stack.createStack() Read newChar while (newChar is not the end-of-line symbol) { if (newChar is not ‘<-’) { stack.push(newChar) } else if (!stack.isEmpty()) { oldChar = stack.pop() } // end if Read newChar } // end while return stack Using the ADT stack in a solution

8 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Checking for Balanced Braces Simple Applications of the ADT Stack

9 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.2 Traces of the algorithm that checks for balanced braces

10 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Array-based Implementation Reference-based Implementation Comparing Implementations Implementations of the ADT Stack

11 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.3 Implementation of the ADT stack that use a) an array; b) a linked list; c) an ADT list

12 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.4 An array-based implementation

13 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.5 A reference-based implementation

14 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.6 An implementation that uses the ADT list

15 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.7 The action of a postfix calculator when evaluating the expression 2 * (3 + 4)

16 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.8 A trace of the algorithm that converts the infix expression a - (b + c * d)/e to postfix form

17 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.9 Flight map for HPAir

18 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.10 The stack of cities as you travel a) from P; b) to R; c) to X; d) back to R; e) back to P; f) to W

19 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.11 The stack of cities a) allowing revisits and b) after backtracking when revisits are not allowed

20 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.12 A trace of the search algorithm, given the flight map in Figure 6-9

21 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.13 A piece of a flight map

22 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.14 Visiting city P, then R, then X: a) box trace versus b) stack

23 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.15 Backtracking from city X, then R, then P: a) box trace versus b) stack

24 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.16 Flight map for Self-Test Exercise 9 and Exercise 11

25 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.17 Railroad switching system for Exercise 2

26 Data Abstraction and Problem Solving with JAVA Walls and Mirrors; Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Figure 6.18 Adjacency list for the flight map in Figure 6-9


Download ppt "Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem."

Similar presentations


Ads by Google