1 / 28

Stacks

Stacks. What is a stack?. A collection of data items in which elements are added removed from only one end (top) LIFO (last-in-first-out). Stack Examples. Trays Kleenex Coins http://www.dcss.mcmaster.ca/~ea97f1/demonstration/ADTDemo.html. Spring Loaded Stack of Plates.

nuru
Download Presentation

Stacks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks

  2. What is a stack? • A collection of data items in which • elements are • added • removed • from only one end (top) • LIFO (last-in-first-out)

  3. Stack Examples • Trays • Kleenex • Coins http://www.dcss.mcmaster.ca/~ea97f1/demonstration/ADTDemo.html

  4. Spring Loaded Stack of Plates • If plate is added to stack, • those below it are pushed down and can't be accessed • If plate is removed from stack, • those below it pop up one position • The top element of the stack can be accessed • The stack empty when no more plates in it

  5. STL Stack bool empty() const // Is stack empty? size_type size() const // # elms in stack value_type& top() // Top elm of stack void push(const value_type&) // Add elm to stack void pop() // Rem elm from stack Operators: =, <, == ..\stl\stl docs\stack.html

  6. Stack Example Stack1.push(2) 2 3 Stack1.push(3) 2 5 3 Stack1.push(5) 2 X = Stack1.top() X = 5 Stack1.pop() 3 2

  7. Stack Application • A program is to be written to • convert nonnegative integers • from base ten to binary • using repeated division by 2 • Successive remainders give the binary digits, but in reverse order. • How does one print them in correct order?

  8. 0 r 1 2 | 1 r 1 2 | 3 r 0 2 | 6 r 1 2 | 13 r 0 2 | 26 Remainders 0 and 1 are generated in right-to-left order. We need to "stack" them up, then print them out from top to bottom. 26 is equivalent to what base 2 number?

  9. BASE-CONVERSION ALGORITHM • Create an empty stack to hold the remainders. • While Number != 0 do the following: • Calculate Remainder that results when Number is divided by 2. • Push Remainder onto the stack of remainders. • Replace Number by integer quotient of Number divided by 2. • End while. • While the stack of remainders is not empty: • Remove the Remainder from the top of the stack of remainders. • Display Remainder. • End while.

  10. // Program that uses a stack to convert the base-ten // representation of a positive integer to base two. // // Input(keyboard): A positive integer. // Output(screen): Base-two representation of the // number. // BASECONV.CPP #include <iostream> #include <stack> int main() { unsigned number; // the number to be converted unsigned remainder; // remainder when Number divd by 2 Stack<int,deque<int> > stackOfRemainders; char response;

  11. do { cout << "Enter positive integer to convert: "; cin >> number; while ( number != 0 ) { remainder = number % 2; stackOfRemainders.Push(remainder); number /= 2; } cout << "Base two representation: "; while ( !stackOfRemainders.Empty() ) { remainder = stackOfRemainders.Pop() cout << remainder; } cout << endl; cout << "\nMore (Y or N)? "; cin >> response; } while (response == 'Y' || response == 'y'); return 0; }

  12. Stack Application Infix to Postfix

  13. Infix to Postfix Algorithm 1. Scan the string from left to right. 2. If a symbol is an operand, write it. 3. If a symbol is an operator a) pop and write every operator from stack that has precedence higher or equal new operator(stop at left parand or empty stack) b) push the operator onto the stack 4. If opening left parand is seen, push it on the stack. 5. On right parand • Pop and write all operators to the left parand • Discard both parands 6. When string has been scanned, pop and write remaining operators on the stack

  14. Programming Assignment • Write a program which converts an infix notation expression into a postfix expression. • Use four operators: + - * / plus ( ) • For operands use letters: A, B, C, D, ... • A + B * C - E ==> A B C * + E -

  15. Array Based Stack Implementation • r:\users\ds\stack

  16. THE END

More Related