Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)

Similar presentations


Presentation on theme: "Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)"— Presentation transcript:

1 Chapter 6 Stacks

2 © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO) property The last item placed on the stack will be the first item removed –Analogy A stack of dishes in a cafeteria

3 © 2005 Pearson Addison-Wesley. All rights reserved6-3 ADT Stack ADT stack operations –Create an empty stack –Destroy a stack –Determine whether a stack is empty –Add a new item –Remove the item that was added most recently –Retrieve the item that was added most recently A program can use a stack independently of the stack’s implementation

4 © 2005 Pearson Addison-Wesley. All rights reserved6-4 ADT Stack // stack operations: bool isEmpty() const; // Determines whether a stack is empty. // Precondition: None. // Postcondition: Returns true if the // stack is empty; otherwise returns false.

5 © 2005 Pearson Addison-Wesley. All rights reserved6-5 ADT Stack bool push(StackItemType newItem); // Adds an item to the top of a stack. // Precondition: newItem is the item to // be added. // Postcondition: If the insertion is // successful, newItem is on the top of // the stack.

6 © 2005 Pearson Addison-Wesley. All rights reserved6-6 ADT Stack bool pop(); // Removes the top of a stack. // Precondition: None. // Postcondition: If the stack is not // empty, the item that was added most // recently is removed. However, if the // stack is empty, deletion is impossible.

7 © 2005 Pearson Addison-Wesley. All rights reserved6-7 ADT Stack bool pop(StackItemType& stackTop); // Retrieves and removes the top of a stack // Precondition: None. // Postcondition: If the stack is not empty, // stackTop contains the item that was added // most recently and the item is removed. // However, if the stack is empty, deletion // is impossible and stackTop is unchanged.

8 © 2005 Pearson Addison-Wesley. All rights reserved6-8 ADT Stack bool getTop(StackItemType& stackTop) const; // Retrieves the top of a stack. // Precondition: None. // Postcondition: If the stack is not empty, // stackTop contains the item that was added // most recently. However, if the stack is // empty, the operation fails and stackTop // is unchanged. The stack is unchanged.

9 © 2005 Pearson Addison-Wesley. All rights reserved6-9 Checking for Balanced Braces A stack can be used to verify whether a program contains balanced braces –An example of balanced braces abc{defg{ijk}{l{mn}}op}qr –An example of unbalanced braces abc{def}}{ghij{kl}m

10 © 2005 Pearson Addison-Wesley. All rights reserved6-10 Checking for Balanced Braces Requirements for balanced braces –Each time you encounter a “}”, it matches an already encountered “{” –When you reach the end of the string, you have matched each “{”

11 © 2005 Pearson Addison-Wesley. All rights reserved6-11 Checking for Balanced Braces Figure 6.3 Traces of the algorithm that checks for balanced braces

12 © 2005 Pearson Addison-Wesley. All rights reserved6-12 Checking for Balanced Braces aStack.createStack() balancedSoFar = true i = 0 while ( balancedSoFar and i < length of aString ){ ch = character at position i in aString ++i if ( ch is '{' ) // push an open brace aStack.push( '{' ) else if ( ch is '}' ) // close brace if ( !aStack.isEmpty() ) aStack.pop() // pop a matching open brace else // no matching open brace balancedSoFar = false // ignore all characters other than braces } if ( balancedSoFar and aStack.isEmpty() ) aString has balanced braces else aString does not have balanced braces

13 © 2005 Pearson Addison-Wesley. All rights reserved6-13 Implementations of the ADT Stack The ADT stack can be implemented using –An array –A linked list –The ADT list

14 © 2005 Pearson Addison-Wesley. All rights reserved6-14 Implementations of the ADT Stack Figure 6.4 Implementation of the ADT stack that use a) an array; b) a linked list; c) an ADT list

15 © 2005 Pearson Addison-Wesley. All rights reserved6-15 An Array-Based Implementation of the ADT Stack Private data fields –An array of items of type StackItemType –The index top Compiler-generated destructor and copy constructor Figure 6.5 An array-based implementation

16 © 2005 Pearson Addison-Wesley. All rights reserved6-16 An Array-Based Implementation of the ADT Stack const int MAX_STACK = maximum-size-of-stack; typedef desired-type-of-stack-item StackItemType; class Stack{ public: Stack(); bool isEmpty() const; bool push(StackItemType newItem); bool pop(); bool pop(StackItemType& stackTop); bool getTop(StackItemType& stackTop) const; private: // array of stack items StackItemType items[MAX_STACK]; // index to top of stack int top; };

17 © 2005 Pearson Addison-Wesley. All rights reserved6-17 An Array-Based Implementation of the ADT Stack // default constructor Stack::Stack(): top(-1){ }

18 © 2005 Pearson Addison-Wesley. All rights reserved6-18 An Array-Based Implementation of the ADT Stack bool Stack::isEmpty() const{ return top < 0; }

19 © 2005 Pearson Addison-Wesley. All rights reserved6-19 An Array-Based Implementation of the ADT Stack bool Stack::push(StackItemType newItem){ // if stack has no more room for // another item if (top >= MAX_STACK-1) return false; else{ ++top; items[top] = newItem; return true; }

20 © 2005 Pearson Addison-Wesley. All rights reserved6-20 An Array-Based Implementation of the ADT Stack bool Stack::pop(){ if (isEmpty()) return false; // stack is not empty; pop top else { --top; return true; }

21 © 2005 Pearson Addison-Wesley. All rights reserved6-21 An Array-Based Implementation of the ADT Stack bool Stack::pop(StackItemType& stackTop){ if (isEmpty()) return false; // stack is not empty; retrieve top else { stackTop = items[top]; --top; return true; }

22 © 2005 Pearson Addison-Wesley. All rights reserved6-22 An Array-Based Implementation of the ADT Stack bool Stack::getTop(StackItemType& stackTop) const{ if (isEmpty()) return false; // stack is not empty; retrieve top else { stackTop = items[top]; return true; }

23 © 2005 Pearson Addison-Wesley. All rights reserved6-23 A Pointer-Based Implementation of the ADT Stack A pointer-based implementation –Required when the stack needs to grow and shrink dynamically top is a reference to the head of a linked list of items A copy constructor and destructor must be supplied Figure 6.6 A pointer-based implementation

24 © 2005 Pearson Addison-Wesley. All rights reserved6-24 A Pointer-Based Implementation of the ADT Stack typedef desired-type-of-stack-item StackItemType; class Stack{ public: Stack(); Stack(const Stack& aStack); ~Stack(); bool isEmpty() const; bool push(StackItemType newItem); bool pop(); bool pop(StackItemType& stackTop); bool getTop(StackItemType& stackTop) const; private: struct StackNode { // a node on the stack StackItemType item; // a data item on the stack StackNode *next; // pointer to next node }; StackNode *topPtr; // pointer to first node in the stack };

25 © 2005 Pearson Addison-Wesley. All rights reserved6-25 A Pointer-Based Implementation of the ADT Stack // default constructor Stack::Stack() : topPtr(NULL){ }

26 © 2005 Pearson Addison-Wesley. All rights reserved6-26 A Pointer-Based Implementation of the ADT Stack // copy constructor Stack::Stack(const Stack& aStack){ if (aStack.topPtr == NULL) topPtr = NULL; // original stack is empty else { // copy first node topPtr = new StackNode; topPtr->item = aStack.topPtr->item; // copy rest of stack StackNode *newPtr = topPtr; for (StackNode *origPtr = aStack.topPtr->next; origPtr != NULL; origPtr = origPtr->next){ newPtr->next = new StackNode; newPtr = newPtr->next; newPtr->item = origPtr->item; } newPtr->next = NULL; }

27 © 2005 Pearson Addison-Wesley. All rights reserved6-27 A Pointer-Based Implementation of the ADT Stack // destructor Stack::~Stack(){ // pop until stack is empty while (!isEmpty()) pop(); }

28 © 2005 Pearson Addison-Wesley. All rights reserved6-28 A Pointer-Based Implementation of the ADT Stack bool Stack::isEmpty() const { return topPtr == NULL; }

29 © 2005 Pearson Addison-Wesley. All rights reserved6-29 A Pointer-Based Implementation of the ADT Stack bool Stack::push(StackItemType newItem) { // create a new node StackNode *newPtr = new StackNode; // set data portion of new node newPtr->item = newItem; // insert the new node newPtr->next = topPtr; topPtr = newPtr; return true; }

30 © 2005 Pearson Addison-Wesley. All rights reserved6-30 A Pointer-Based Implementation of the ADT Stack bool Stack::pop() { if (isEmpty()) return false; // stack is not empty; delete top else{ StackNode *temp = topPtr; topPtr = topPtr->next; // return deleted node to system temp->next = NULL; // safeguard delete temp; return true; }

31 © 2005 Pearson Addison-Wesley. All rights reserved6-31 A Pointer-Based Implementation of the ADT Stack bool Stack::pop(StackItemType& stackTop) { if (isEmpty()) return false; // not empty; retrieve and delete top else{ stackTop = topPtr->item; StackNode *temp = topPtr; topPtr = topPtr->next; // return deleted node to system temp->next = NULL; // safeguard delete temp; return true; }

32 © 2005 Pearson Addison-Wesley. All rights reserved6-32 A Pointer-Based Implementation of the ADT Stack bool Stack::getTop(StackItemType& stackTop) const { if (isEmpty()) return false; // stack is not empty; retrieve top else { stackTop = topPtr->item; return true; }

33 © 2005 Pearson Addison-Wesley. All rights reserved6-33 An Implementation That Uses the ADT List The ADT list can be used to represent the items in a stack If the item in position 1 is the top –push(newItem) insert(1, newItem) –pop() remove(1) –getTop(stackTop) retrieve(1, stackTop)

34 © 2005 Pearson Addison-Wesley. All rights reserved6-34 An Implementation That Uses the ADT List Figure 6.7 An implementation that uses the ADT list

35 © 2005 Pearson Addison-Wesley. All rights reserved6-35 An Implementation That Uses the ADT List #include "ListP.h" // list operations typedef ListItemType StackItemType; class Stack { public: Stack(); Stack(const Stack& aStack); ~Stack(); bool isEmpty() const; bool push(StackItemType newItem); bool pop(); bool pop(StackItemType& stackTop); bool getTop(StackItemType& stackTop) const; private: List aList; // list of stack items };

36 © 2005 Pearson Addison-Wesley. All rights reserved6-36 An Implementation That Uses the ADT List // default constructor Stack::Stack(){ }

37 © 2005 Pearson Addison-Wesley. All rights reserved6-37 An Implementation That Uses the ADT List // copy constructor Stack::Stack(const Stack& aStack) : aList(aStack.aList){ }

38 © 2005 Pearson Addison-Wesley. All rights reserved6-38 An Implementation That Uses the ADT List // destructor Stack::~Stack() { }

39 © 2005 Pearson Addison-Wesley. All rights reserved6-39 An Implementation That Uses the ADT List bool Stack::isEmpty() const { return aList.isEmpty(); }

40 © 2005 Pearson Addison-Wesley. All rights reserved6-40 An Implementation That Uses the ADT List bool Stack::push(StackItemType newItem){ return aList.insert(1, newItem); }

41 © 2005 Pearson Addison-Wesley. All rights reserved6-41 An Implementation That Uses the ADT List bool Stack::pop() { return aList.remove(1); }

42 © 2005 Pearson Addison-Wesley. All rights reserved6-42 An Implementation That Uses the ADT List bool Stack::pop(StackItemType& stackTop) { if (aList.retrieve(1, stackTop)) return aList.remove(1); else return false; }

43 © 2005 Pearson Addison-Wesley. All rights reserved6-43 An Implementation That Uses the ADT List bool Stack::getTop(StackItemType& stackTop) const { return aList.retrieve(1, stackTop); }

44 © 2005 Pearson Addison-Wesley. All rights reserved6-44 Comparing Implementations Fixed size versus dynamic size –An array-based implementation Prevents the push operation from adding an item to the stack if the stack’s size limit has been reached –A pointer-based implementation Does not put a limit on the size of the stack

45 © 2005 Pearson Addison-Wesley. All rights reserved6-45 Comparing Implementations An implementation that uses a linked list versus one that uses a pointer-based implementation of the ADT list –ADT list approach reuses an already implemented class Much simpler to write Saves time

46 © 2005 Pearson Addison-Wesley. All rights reserved6-46 Applications Section 5.2 Section 6.4 Section 6.5

47 © 2005 Pearson Addison-Wesley. All rights reserved6-47 Section 5.2: Defining Languages A language –A set of strings of symbols –Examples: English, C++ –If a C++ program is one long string of characters, the language C++Programs is defined as C++Programs = {strings w : w is a syntactically correct C++ program}

48 © 2005 Pearson Addison-Wesley. All rights reserved6-48 Defining Languages A language does not have to be a programming or a communication language –Example: AlgebraicExpressions = {w : w is an algebraic expression} –The grammar defines the rules for forming the strings in a language A recognition algorithm determines whether a given string is in the language –A recognition algorithm for a language is written more easily with a recursive grammar

49 © 2005 Pearson Addison-Wesley. All rights reserved6-49 The Basics of Grammars Symbols used in grammars –x | y means x or y –x y means x followed by y – means any instance of word that the definition defines

50 © 2005 Pearson Addison-Wesley. All rights reserved6-50 Example: C++ Identifiers A C++ identifier begins with a letter and is followed by zero or more letters and digits Language C++Ids = {w : w is a legal C++ identifier} Grammar = | | = a | b | … | z | A | B | …| Z | _ | $ = 0 | 1 | … | 9

51 © 2005 Pearson Addison-Wesley. All rights reserved6-51 Section 6.4: Algebraic Expressions Infix expressions –An operator appears between its operands Example: a + b Prefix expressions –An operator appears before its operands Example: + a b Postfix expressions –An operator appears after its operands Example: a b +

52 © 2005 Pearson Addison-Wesley. All rights reserved6-52 Algebraic Expressions Infix expressions: a + b * c + ( d * e + f ) * g Postfix expressions: abc*+de*f+g*+ Prefix expressions: ++a*bc*+*defg

53 © 2005 Pearson Addison-Wesley. All rights reserved6-53 Algebraic Expressions To convert a fully parenthesized infix expression to a prefix form –Move each operator to the position marked by its corresponding open parenthesis –Remove the parentheses –Example Infix expression: ( ( a + b ) * c ) Prefix expression: * + a b c

54 © 2005 Pearson Addison-Wesley. All rights reserved6-54 Algebraic Expressions To convert a fully parenthesized infix expression to a postfix form –Move each operator to the position marked by its corresponding closing parenthesis –Remove the parentheses –Example Infix form: ( ( a + b ) * c ) Postfix form: a b + c *

55 © 2005 Pearson Addison-Wesley. All rights reserved6-55 Algebraic Expressions Prefix and postfix expressions –Never need Precedence rules Association rules Parentheses – Have Simple grammar expressions Straightforward recognition and evaluation algorithms

56 © 2005 Pearson Addison-Wesley. All rights reserved6-56 Prefix Expressions Grammar = | = + | - | * | / = a | b | … | z

57 © 2005 Pearson Addison-Wesley. All rights reserved6-57 Postfix Expressions Grammar = | = + | - | * | / = a | b | … | z The recursive case for prefix form to postfix form conversion postfix(exp)= postfix(prefix1) + postfix(prefix2) +

58 © 2005 Pearson Addison-Wesley. All rights reserved6-58 Fully Parenthesized Infix Expressions Grammar = | ( ) = + | - | * | / = a | b | … | z Fully parenthesized expressions –Do not require precedence rules or rules for association –Are inconvenient for programmers

59 © 2005 Pearson Addison-Wesley. All rights reserved6-59 Evaluating Prefix Expressions A recursive algorithm that evaluates a prefix expression evaluatePrefix(inout strExp:string):float ch = first character of expression strExp Delete first character from strExp if (ch is an identifier) return value of the identifier else if (ch is an operator named op) { operand1 = evaluatePrefix(strExp) operand2 = evaluatePrefix(strExp) return operand1 op operand2 }

60 © 2005 Pearson Addison-Wesley. All rights reserved6-60 Converting Prefix Expressions to Equivalent Postfix Expressions A recursive algorithm that converts a prefix expression to postfix form convert(inout pre:string, out post:string) ch = first character of pre Delete first character of pre if (ch is a lowercase letter) post = post + ch else { convert(pre, post) post = post + ch }

61 © 2005 Pearson Addison-Wesley. All rights reserved6-61 Algebraic Expression Operations using Stacks 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 expression –Convert the infix expression to postfix form –Evaluate the postfix expression

62 © 2005 Pearson Addison-Wesley. All rights reserved6-62 Converting Infix Expressions to Equivalent Postfix Expressions 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

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

64 © 2005 Pearson Addison-Wesley. All rights reserved6-64 Converting Infix Expressions to Equivalent Postfix Expressions initialize postfixExp to the null string for (each character ch in the infix expression){ switch (ch){ case ch is an operand: append ch to the end of postfixExp break case ch is an operator: store ch until you know where to place it break case ch is '(' or ')': discard ch break } First draft of an algorithm

65 © 2005 Pearson Addison-Wesley. All rights reserved6-65 Converting Infix Expressions to Equivalent Postfix Expressions for (each character ch in the infix expression){ switch (ch){ case operand: // append operand to end of PE postFixExp += ch break case '(': // save '(' on stack aStack.push(ch) break case ')': // pop stack until matching '(' while (top of stack is not '('){ postFixExp += (top of aStack) aStack.pop() } aStack.pop() // remove the open parenthesis break A pseudocode algorithm

66 © 2005 Pearson Addison-Wesley. All rights reserved6-66 Converting Infix Expressions to Equivalent Postfix Expressions case operator: // process stack operators of // greater precedence while (!aStack.isEmpty() and top of stack is not '(' and precedence(ch) <= precedence(top of aStack)){ postfixExp += top of aStack) aStack.pop() } aStack.push(ch) // save new operator break } A pseudocode algorithm

67 © 2005 Pearson Addison-Wesley. All rights reserved6-67 Evaluating Postfix Expressions A postfix calculator –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

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

69 © 2005 Pearson Addison-Wesley. All rights reserved6-69 Evaluating Postfix Expressions 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 operand2 = top of stack pop the stack operand1 = top of stack pop the stack result = operand1 op operand2 push result onto stack } A pseudocode algorithm

70 © 2005 Pearson Addison-Wesley. All rights reserved6-70 Section 6.5: A Search Problem High Planes Airline Company (HPAir) –For each customer request, indicate whether a sequence of HPAir flights exists from the origin city to the destination city The flight map for HPAir is a graph –Adjacent vertices are two vertices that are joined by an edge –A directed path is a sequence of directed edges

71 © 2005 Pearson Addison-Wesley. All rights reserved6-71 Application: A Search Problem Figure 6.10 Flight map for HPAir

72 © 2005 Pearson Addison-Wesley. All rights reserved6-72 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 Backtracking can be used to recover from a wrong choice of a city

73 © 2005 Pearson Addison-Wesley. All rights reserved6-73 A Nonrecursive Solution That Uses a Stack Figure 6.13 A trace of the search algorithm, given the flight map in Figure 6-10

74 © 2005 Pearson Addison-Wesley. All rights reserved6-74 A Nonrecursive Solution That Uses a Stack aStack.createStack() clear marks on all cities aStack.push(originCity) // push origin city onto aStack 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 path 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) aStack.pop() // backtrack else{ select an unvisited destination city C for a flight from the city on the top of the stack aStack.push(C) mark C as visited } Draft of the search algorithm

75 © 2005 Pearson Addison-Wesley. All rights reserved6-75 A Nonrecursive Solution That Uses a Stack boolean searchS(originCity, destinationCity) // searches for a sequence of flights // from originCity to destinationCity aStack.createStack() clear marks on all cities aStack.push(originCity) // push origin onto aStack mark the origin as visited while (!aStack.isEmpty() and destinationCity is not at the top of the stack){ // loop invariant: the stack contains a directed path // from the origin city at the bottom of the stack to // the city at the top of the stack Final version of the search algorithm

76 © 2005 Pearson Addison-Wesley. All rights reserved6-76 A Nonrecursive Solution That Uses a Stack // originCity to destinationCity if (no flights exist from the city on the top of the stack to unvisited cities) aStack.pop() // backtrack else { select an unvisited destination city C for a flight from the city on the top of the stack aStack.push(C) mark C as visited } if ( aStack.isEmpty() ) return false // no path exists else return true // path exists Final version of the search algorithm

77 © 2005 Pearson Addison-Wesley. All rights reserved6-77 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

78 © 2005 Pearson Addison-Wesley. All rights reserved6-78 A Recursive Solution A refined recursive search strategy + searchR(in originCity:City, in destinationCity:City):boolean 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)

79 © 2005 Pearson Addison-Wesley. All rights reserved6-79 The Relationship Between Stacks and 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

80 © 2005 Pearson Addison-Wesley. All rights reserved6-80 Summary ADT stack operations have a last-in, first- out (LIFO) behavior Stack applications –Algorithms that operate on algebraic expressions –Flight maps A strong relationship exists between recursion and stacks


Download ppt "Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)"

Similar presentations


Ads by Google