100 likes | 269 Views
Stacks. Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack. Stacks. Three container adapters stack: LIFO discipline queue: FIFO priority_queue : HPFO Stack <stack> Access only top Underlying sequence container: deque
E N D
Stacks • Stacks • Stack Examples • Stack API • More Examples/Uses • Base Conversion • Activation Records • RPN • Implementing a Stack
Stacks • Three container adapters • stack: LIFO discipline • queue: FIFO • priority_queue: HPFO • Stack • <stack> • Access only top • Underlying sequence container: deque • No iterators
Stack Example string str1, str2; stack <char> s; cin >> str1; for (size_ti = 0; i < str1.length (); ++i) s.push (str1[i]); while (! s.empty ()) { str2 += s.top (); s.pop (); } // <iomanip> for boolalpha cout << boolalpha << (str1 == str2) << endl;
CLASS stack CLASS stack <stack> <stack> Constructor Operations stack (); Create an empty stack bool empty () const; Check whether the stack is empty. Return true if it is empty and false otherwise.
CLASS stack <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.
CLASS stack <stack> Operations size_t size () const; Return the number of items on the stack. T& top (); 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 ().
Base Conversion (Hex) int num = 431, base = 16; stack <char> s; string digits = “0123456789ABCDEF”; do { s.push (digits[num % base]); num /= base; } while (num != 0);
Implementing a Stack typedefintvalue_type; class Stack { vector <value_type> v; public: Stack () { } // empty to start // const version too value_type& top () { return (??); } void push (const value_type& i) { ?? } void pop () { ?? } bool empty () const { return (??) } size_t size () const { return (??); } };