Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.

Similar presentations


Presentation on theme: "Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately."— Presentation transcript:

1 Chapter 6 B Stacks

2 © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately array based or reference based Fixed size versus dynamic size –An array-based implementation Uses fixed-sized arrays –Prevents the push operation from adding an item to the stack if the stack’s size limit has been reached –A reference-based implementation Does not put a limit on the size of the stack

3 © 2004 Pearson Addison-Wesley. All rights reserved6 B-3 Comparing Implementations An implementation that uses a linked list versus one that uses a reference-based implementation of the ADT list –Linked list approach More efficient –ADT list approach Reuses an already implemented class –Much simpler to write –Saves time

4 © 2004 Pearson Addison-Wesley. All rights reserved6 B-4 Application: Algebraic Expressions When the ADT stack is used to solve a problem, the use of the ADT’s operations should not depend on its implementation To evaluate an infix expressions –Convert the infix expression to postfix form –Evaluate the postfix expression

5 © 2004 Pearson Addison-Wesley. All rights reserved6 B-5 Evaluating Postfix Expressions A postfix calculator –Requires you to enter postfix expressions Example: 2, 3, 4, +, * –When an operand is entered, the calculator Pushes it onto a stack –When an operator is entered, the calculator Applies it to the top two operands of the stack Pops the operands from the stack Pushes the result of the operation on the stack

6 © 2004 Pearson Addison-Wesley. All rights reserved6 B-6 Evaluating Postfix Expressions Figure 6.7 The action of a postfix calculator when evaluating the expression 2 * (3 + 4)

7 © 2004 Pearson Addison-Wesley. All rights reserved6 B-7 Evaluating Postfix Expressions To evaluate a postfix expression which is entered as a string of characters –Simplifying assumptions The string is a syntactically correct postfix expression No unary operators are present No exponentiation operators are present Operands are single lowercase letters that represent integer values

8 © 2004 Pearson Addison-Wesley. All rights reserved6 B-8 for (each character ch in the string) if (ch is an operand) { Push value that operand ch represents onto stack } else { // ch is an operator named op // evaluate and push the result operand1 = Pop the top of the stack operand2 = Pop the top of the stack result = operand1 op operand2 Push result onto stack } // end if } // end for

9 © 2004 Pearson Addison-Wesley. All rights reserved6 B-9 Converting Infix Expressions to Equivalent Postfix Expressions An infix expression can be evaluated by first being converted into an equivalent postfix expression Facts about converting from infix to postfix –Operands always stay in the same order with respect to one another –An operator will move only “to the right” with respect to the operands –All parentheses are removed

10 © 2004 Pearson Addison-Wesley. All rights reserved6 B-10 1.When you encounter an operand, append it to the output string postfixExp. 2.Push each “(“ on to the stack 3.When you encounter an operator, if the stack is empty, push the operator onto the stack. However, if the stack is not empty, pop operators of greater or equal precedence from the stack and append them to postfixExp. You stop when you encounter either a “(“ or an operator of lower precedence or when the stack becomes empty. You the push the new operator onto the stack. 4.When you encounter a “)”, pop operators off the stack and append them to the end of postfixExp until you encounter the matching”(“. 5.When you reach the end of the string, you append the remaining contents of the stack to postfixExp.

11 © 2004 Pearson Addison-Wesley. All rights reserved6 B-11 Converting Infix Expressions to Equivalent Postfix Expressions Figure 6.8 A trace of the algorithm that converts the infix expression a - (b + c * d)/e to postfix form

12 © 2004 Pearson Addison-Wesley. All rights reserved6 B-12 for (each character ch in the infix expression) switch (ch) { case operands: // append operand to end of postfixExp postfixExp = postfixExp + ch break case ‘(‘: // save ‘(‘ on stack stack.push(ch) break case ‘)‘: // pop stack unitl matching ‘(‘ while (top of stack is not ‘(“) { postfixExp = postfixExp + stack.pop() } // end while openParen = stack.pop() // remove the open parenthesis break

13 © 2004 Pearson Addison-Wesley. All rights reserved6 B-13 case operator: // process stack operators of // greater precedence while (!stack.isEmpty() and top of stack is not ‘(“ and precedence(ch) <= precedence(top of stack) ) { postfixExp = postfixExp + stack.pop() } // end while stack.push(ch) // save new operator break } // end switch } // end for while (!stack.isEmpty()) { postfixExp = postfixExp + stack.pop() } // end while

14 © 2004 Pearson Addison-Wesley. All rights reserved6 B-14 Application: A Search Problem High Planes Airline Company (HPAir) –Problem For each customer request, indicate whether a sequence of HPAir flights exists from the origin city to the destination city

15 © 2004 Pearson Addison-Wesley. All rights reserved6 B-15 Representing the Flight Data The flight map for HPAir is a graph –Adjacent vertices Two vertices that are joined by an edge –Directed path A sequence of directed edges Figure 6.9 Flight map for HPAir

16 © 2004 Pearson Addison-Wesley. All rights reserved6 B-16 A Nonrecursive Solution that Uses a Stack The solution performs an exhaustive search –Beginning at the origin city, the solution will try every possible sequence of flights until either It finds a sequence that gets to the destination city It determines that no such sequence exists The ADT stack is useful in organizing an exhaustive search Backtracking can be used to recover from a wrong choice of a city

17 © 2004 Pearson Addison-Wesley. All rights reserved6 B-17 stack.createStack() stack.push(originCity) // push origin city onto stack while (a sequence of flights from the origin to the destination has not been found) { if (you need to backtrack from the city on the top of the stack) { temp = stack.pop() } else { Select a destination city C for a flight from the city on the top of the stack stack.push(C) } // end if } // end while

18 © 2004 Pearson Addison-Wesley. All rights reserved6 B-18 A Nonrecursive Solution that Uses a Stack 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 © 2004 Pearson Addison-Wesley. All rights reserved6 B-19 stack.createStack() Clear marks on all cities stack.push(originCity) // push origin city onto stack Mark the origin as visited while (a sequence of flights from the origin to the destination has not been found) { // loop invariant: The stack contains a directed graph // from the origin city at the bottom of the stack to // the city at the top of the stack if (no flights exist from the city on the top of the stack to unvisited cities) { temp = stack.pop() // backtrack } else { Select an unvisited destination city C for a flight from the city on the top of the stack stack.push(C) Mark C as visited } // end if } // end while

20 © 2004 Pearson Addison-Wesley. All rights reserved6 B-20 A Nonrecursive Solution that Uses a Stack Figure 6.12 A trace of the search algorithm, given the flight map in Figure 6-9

21 © 2004 Pearson Addison-Wesley. All rights reserved6 B-21 A Recursive Solution Possible outcomes of the recursive search strategy –You eventually reach the destination city and can conclude that it is possible to fly from the origin to the destination –You reach a city C from which there are no departing flights –You go around in circles

22 © 2004 Pearson Addison-Wesley. All rights reserved6 B-22 A Recursive Solution A refined recursive search strategy searchR(originCity, destinationCity) Mark originCity as visited if (originCity is destinationCity) { Terminate -- the destination is reached } else { for (each unvisited city C adjacent to originCity) { searchR(C, destinationCity) }

23 © 2004 Pearson Addison-Wesley. All rights reserved6 B-23 The Relationship Between Stacks and Recursion The ADT stack has a hidden presence in the concept of recursion Typically, stacks are used by compilers to implement recursive methods –During execution, each recursive call generates an activation record that is pushed onto a stack Stacks can be used to implement a nonrecursive version of a recursive algorithm

24 © 2004 Pearson Addison-Wesley. All rights reserved6 B-24 Summary ADT stack operations have a last-in, first-out (LIFO) behavior Algorithms that operate on algebraic expressions are an important application of stacks A stack can be used to determine whether a sequence of flights exists between two cities A strong relationship exists between recursion and stacks


Download ppt "Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately."

Similar presentations


Ads by Google