|        | 
    
	
      | 您现在的位置: 爱51代码网 >> 主页设计 >> 文章正文 | 
    
    
      | 设计包含max函数的队列 | 
    
    
       | 
    
        
      |  
 
 
37.            datalist.add(i);   38.            helplist.add(0);   39.        }   40.        else{   41.            int max =datalist.get(helplist.getLast());   42.            if(i>max){   43.                helplist.add(datalist.size());   44.            }   45.            else{   46.                helplist.add(helplist.getLast());   47.            }   48.            datalist.add(i);   49.        }   50.    }   51.    public Integer pop(){   52.        if(datalist.size()==0){   53.            System.out.println("栈中已无数据!");   54.            return null;   55.        }   56.        helplist.removeLast();   57.        return datalist.removeLast();   58.           59.    }   60.    public Integer max(){   61.        if(datalist.size()==0){   62.            System.out.println("栈中已无数据!");   63.            return null;   64.        }   65.        return datalist.get(helplist.getLast());   66.    }   67.    public boolean isEmpty(){   68.        if(datalist.size()==0)   69.            return true;   70.        else    71.            return false;   72.            }   73.}   74.//用两个栈实现一个队列    75.class Queue{   76.    Stack stack1 = new Stack();   77.    Stack stack2 = new Stack();   78.    public void EnQueue(int i){   79.        stack1.push(i);   80.    }   81.    public Integer DeQueue(){   82.        int temp;   83.        if(stack2.isEmpty()==true){   84.            while(stack1.isEmpty()==false){   85.            temp = stack1.pop();   86.            stack2.push(temp);   87.            }   88.        }   89.        return stack2.pop();   90.    }   91.    public Integer MaxElement(){   92.        if(stack1.isEmpty())   93.            return stack2.max();   94.        else if(stack2.isEmpty()){   95.            return stack1.max();   96.        }   97.        return stack1.max()>stack2.max()?stack1.max():stack2.max();   98.    }   99.}  上一页  [1] [2]  
 |     
   
    
           上一篇文章: 随机从数组中取出指定的不重复的n个数代码 下一篇文章: 没有了 | 
    
     | 
 |