Presentation is loading. Please wait.

Presentation is loading. Please wait.

Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.

Similar presentations


Presentation on theme: "Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass."— Presentation transcript:

1 Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass Stack (3 slides) Class Stack Using a Stack to Create a Hex # Using a Stack to Create a Hex # Uncoupling Stack Elt’sUncoupling Stack Elt’s (6 slides) Uncoupling Stack Elt’s Activation Records Activation Records RPN Stacks Infix Notation Infix Notation Summary SlidesSummary Slides (4 slides) Summary Slides

2 Main Index Contents 2 Stacks A stack is a sequence of items that are accessible at only one end of the sequence.

3 Main Index Contents 3 Pushing/Popping a Stack Because a pop removes the item last added to the stack, we say that a stack has LIFO (last- in/first-out) ordering.

4 Main Index Contents 4 4 Main Index Contents CLASS stack Constructor stack(); Create an empty stack CLASS stackOperations bool empty(); const Check whether the stack is empty. Return true if it is empty and false otherwise. The stack API

5 Main Index Contents 55 Main Index Contents CLASS stack Operations void pop(); Remove the item from the top of the stack. Precondition:The stack is not empty. Postcondition:Either the stack is empty or the stack has a new topmost item from a previous push. void push(const T& item); Insert the argument item at the top of the stack. Postcondition: The stack has a new item at the top.

6 Main Index Contents 66 Main Index Contents CLASS stack Operations int size() const; Return the number of items on the stack. T& top() const; Return a reference to the value of the item at the top of the stack. Precondition:The stack is not empty. const T& top() const; Constant version of top().

7 Main Index Contents 7 Stack operations #include … stack s; int i; for (i = 1; i <= 5; i++) s.push(i);// top  5, 4, 3, 2, 1 cout << "stack size = " << s.size( ) << endl;// size = 5 cout << "Popping the stack: << endl; while (!s.empty( )) { cout << s.top.( ) << '\t';// 5 4 3 2 1 s.pop( ); } cout << endl; …

8 Main Index Contents 8 Stack operations (continued) top( ) returns a reference to the item at the top of the stack; allows the programmer to modify its value. stack s; s.push(22); s.push(33); s.push(99); cout << s.top( ) << endl; s.top( ) = 55;// 99 replaced by 55 while(!s.empty( )) { cout << s.top( ) << '\t'; s.pop( ); } …

9 Main Index Contents 9 Application: number system conversion The function multibaseOutput takes an arbitrary positive integer and an integer representing the target base of a new number system (2 for binary, 8 for octal, and 16 for hexadecimal). The function then converts the decimal number to the target base and displays the number in new number base as a string. string multibaseOutput(int num, int b) { string digitChar = "0123456789ABCDEF", numStr = ""; stack stk; // extract base-b digits of decimal num do { stk.push(digitChar[num % b); num /= b; } while (num != 0);

10 Main Index Contents 10 Application: number system conversion (continued) // flush the stack while (!str.empty( )) { numStr += stk.top( );// concatenate digit character on top // of stk stack to numStr string stk.pop( ); } return numStr; } // The following slide shows in a stepwise manner how a decimal integer 431 is converted and stored in numStr. Obviously, the same function works for octal and binary conversion as well. Exercise: design a driver main function to test the above function.

11 Main Index Contents 11 Main Index Contents Using a Stack to convert a decimal integer to a Hex Number

12 Main Index Contents 12 Main Index Contents Uncoupling Stack Elements: operation needs two stacks

13 Main Index Contents 13 Main Index Contents Uncoupling Stack Elements

14 Main Index Contents 14 Main Index Contents Uncoupling Stack Elements

15 Main Index Contents 15 Main Index Contents Uncoupling Stack Elements

16 Main Index Contents 16 Main Index Contents Uncoupling Stack Elements

17 Main Index Contents 17 Main Index Contents Uncoupling Stack Elements

18 Main Index Contents 18 Uncoupling example The function uncouple removes the first occurrence of a target (if any) from stack s. The function returns true if target is removed and false otherwise. template bool uncouple (stack &s, const T &target) { stack tmpStk; bool foundTaget = true;// assume the target is on the stack while (!s.empty( ) && s.top( ) != target) { tmp.push(s.top( )); s.pop( ); };

19 Main Index Contents 19 Uncoupling example (continued) if (!s.empty( ))// found target s.pop( ); else// target not on the stack s foundTarget = false; while (!tmpStk.empty( )) { s.push(tmpStk.top( )); tmpStk.pop( ); } return foundTarget; }

20 Main Index Contents 20 Main Index Contents

21 Main Index Contents 21 Why the pop( ) returns void? If the pop( ) returns an element by value, it is inefficient, because it involves a call to the copy constructor for type T. Returning a reference to the element on top of the stack is not feasible, because the element is no longer on the stack and must be save somewhere before returning a reference to it. If the choice is to use dynamic memory, a memory leak will result unless the dynamic memory is eventually deleted.

22 Main Index Contents 22 Other applications: recursive code and runtime stack Stack is extensively used for the operation of recursive algorithms. There are numerous interesting and practical applications in this regard. We will be discuss a few examples after the recursive algorithms are discussed.

23 Main Index Contents 23 Main Index Contents

24 Main Index Contents 24 Main Index Contents Infix Expression Rules The figure below gives input precedence, stack precedence, and rank used for the operators +, -, *, /, %, and ^, along with the parentheses. Except for the exponential operator ^, the other binary operators are left-associative and have equal input and stack precedence. Precedence SymbolInput precedence Stack precedence Rank + -1 1 * / %2 2 ^4 3 (5 0 )0 0 0

25 Main Index Contents 25 Main Index Contents Summary Slide 1 §- Stack -Storage Structure with insert (push) and erase (pop) operations occur at one end, called the top of the stack. -The last element in is the first element out of the stack, so a stack is a LIFO structure.

26 Main Index Contents 26 Main Index Contents Summary Slide 2 §- Recursion -The system maintains a stack of activation records that specify: 1)the function arguments 2)the local variables/objects 3)the return address -The system pushes an activation record when calling a function and pops it when returning.

27 Main Index Contents 27 Main Index Contents Summary Slide 3 §- Postfix/RPN Expression Notation -places the operator after its operands -easy to evaluate using a single stack to hold operands. -The rules: 1) Immediately push an operand onto the stack. 2) For a binary operator, pop the stack twice, perform the operation, and push the result onto the stack. 3) At the end a single value remains on the stack. This is the value of the expression.

28 Main Index Contents 28 Main Index Contents Summary Slide 4 §- Infix notation -A binary operator appears between its operands. -More complex than postfix, because it requires the use of operator precedence and parentheses. -In addition, some operators are left-associative, and a few are right-associative.


Download ppt "Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass."

Similar presentations


Ads by Google