Design a queue containing max Functions

Source: Internet
Author: User

Question: Suppose there is a queue with three operations: 1. enQueue (v): Add v to the queue. 2. deQueue: Delete the first element in the queue and return this element 3. maxElement: design a Data Structure and Algorithm for the maximum element in the return queue to minimize the time complexity of MaxElement operations. Analysis and Solution: First implement a stack with max functions, and then use two stacks to implement a queue [java] import java. util. shortlist; public class Test_3_7 {/*** @ param args */public static void main (String [] args) {Queue queue = new Queue (); queue. enQueue (4); System. out. println (queue. maxElement (); queue. enQueue (1); System. out. println (queue. maxElement (); queue. enQueue (5); System. out. println (queue. maxElement (); queue. enQueue (3); System. out. println (queue. ma XElement (); queue. deQueue (); System. out. println (queue. maxElement (); queue. deQueue (); System. out. println (queue. maxElement (); queue. deQueue (); System. out. println (queue. maxElement () ;}}// first implement a Stack class Stack {category list <Integer> datalist = new category list <Integer> (); counter list <Integer> helplist = new counter list <Integer> (); public void push (int I) {if (datalist. size () = 0) {datalist. add (I); Helplist. add (0);} else {int max = datalist. get (helplist. getLast (); if (I> max) {helplist. add (datalist. size ();} else {helplist. add (helplist. getLast ();} datalist. add (I) ;}} public Integer pop () {if (datalist. size () = 0) {System. out. println ("no data in the stack! "); Return null;} helplist. removeLast (); return datalist. removeLast ();} public Integer max () {if (datalist. size () = 0) {System. out. println ("no data in the stack! "); Return null;} return datalist. get (helplist. getLast ();} public boolean isEmpty () {if (datalist. size () = 0) return true; else return false ;}// use two stacks to implement a Queue class Queue {Stack stack1 = new Stack (); stack stack2 = new Stack (); public void EnQueue (int I) {stack1.push (I);} public Integer DeQueue () {int temp; if (stack2.isEmpty () = true) {while (stack1.isEmpty () = false) {temp = stack1.pop (); stack2.pu Sh (temp) ;}} return stack2.pop ();} public Integer MaxElement () {if (stack1.isEmpty () return stack2.max (); else if (stack2.isEmpty ()) {return stack1.max ();} return stack1.max ()> stack2.max ()? Stack1.max (): stack2.max () ;}} output: 4455553

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.