Design a queue to obtain the maximum value of the queue in O (1 ).

Source: Internet
Author: User
// Problem: design a queue to obtain the maximum value of the queue in O (1) time # include <stdio. h >#include <queue >#include <stack> // o (1) the maximum template in the stack <typename T> class maxstack {public: // void push (const T & Value) {data _. push (value); If (max_element _. empty () {max_element _. push (value);} else if (value> = max_element _. top () {max_element _. push (value) ;}// return the top element t top () {return data _. top () ;}// the void POP () {If (Data _. top () = max_element _. top () {Max_element _. pop ();} data _. pop () ;}// determines whether bool empty () {return data _. empty ();} // retrieves the maximum value T max () {If (! Max_element _. empty () {return max_element _. top () ;}} PRIVATE: STD: Stack <t> data _; STD: Stack <t> max_element _ ;}; // o (1) the maximum template in the queue <typename T> class maxqueue {public: // queue operation !!!! Void push (const T & Value) {push_stack _. push (value);} // element T front () {If (pop_stack _. empty () {While (! Push_stack _. empty () {pop_stack _. push (push_stack _. top (); push_stack _. pop () ;}return pop_stack _. top () ;}// team-out operation !!!! Void POP () {If (pop_stack _. Empty () {While (! Push_stack _. empty () {pop_stack _. push (push_stack _. top (); push_stack _. pop () ;}} pop_stack _. pop () ;}// empty operation !!!!! Bool isempty () {return push_stack _. Empty () & pop_stack _. Empty () ;}// retrieve the maximum value T max () {If (! Push_stack _. Empty ()&&! Pop_stack _. Empty () {return push_stack _. Max ()> pop_stack _. Max ()? Push_stack _. Max (): pop_stack _. Max ();} else if (push_stack _. Empty ()&&! Pop_stack _. Empty () {return pop_stack _. Max ();} else if (! Push_stack _. empty () & pop_stack _. empty () {return push_stack _. max () ;}else {Throw runtime_error;} PRIVATE: maxstack <t> push_stack _; maxstack <t> pop_stack _ ;}; // test case int main (INT argc, char ** argv) {maxqueue <int> max_queue; max_queue.push (1); max_queue.push (2); max_queue.push (6); max_queue.push (4); max_queue.push (5 ); max_queue.push (2); printf ("max % d \ n", max_queue.max (); max_queue.pop (); printf ("max % d \ n", max_queue.max ()); max_queue.pop (); printf ("max % d \ n", max_queue.max (); max_queue.pop (); printf ("max % d \ n", max_queue.max ()); max_queue.pop (); printf ("max % d \ n", max_queue.max (); max_queue.pop (); printf ("max % d \ n", max_queue.max ());}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.