280 likes | 449 Views
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.
E N D
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 • 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
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
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
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?
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?
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.
// 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;
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; }
Stack Application Infix to Postfix
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
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 -
Array Based Stack Implementation • r:\users\ds\stack