1 / 10

Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN

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

giona
Download Presentation

Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN

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 • Stacks • Stack Examples • Stack API • More Examples/Uses • Base Conversion • Activation Records • RPN • Implementing a Stack

  2. Stacks • Three container adapters • stack: LIFO discipline • queue: FIFO • priority_queue: HPFO • Stack • <stack> • Access only top • Underlying sequence container: deque • No iterators

  3. 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;

  4. 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.

  5. 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.

  6. 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 ().

  7. 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);

  8. 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 (??); } };

More Related