// 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 ());}