Java data structures - queue

class MyQueue {
     int Elements []; 

    public MyQueue () { 
        Elements = new new  int [0 ]; 
    } 

    // enqueue 
    public  void the Add ( int Element) {
         // create a new array 
        int [] = newArr new new  int [Elements +. 1 .length ];
         // the copy source elements of the array into a new array 
        for ( int I = 0; I <elements.length; I ++ ) { 
            newArr [I] = elements [I]; 
        } 
        
        // add elements into the new array
        newArr [elements.length] = Element; 
        Elements = newArr; 
    } 

    // dequeue 
    public  int poll () {
         // the 0th array elements taken 
        int Element Elements = [0 ];
         // Create a new array 
        int [] = newArr new new  int [-elements.length. 1 ];
         // the copy source elements of the array into a new array 
        for ( int I = 0; I <newArr.length; I ++ ) { 
            newArr [I] = elements [I +. 1 ]; 
        } 
        // replace the old array 
        elements =newArr;
         return Element; 
    } 
    
    // whether the queue is empty 
    public  Boolean isEmpty () {
         return elements.length == 0 ; 
    } 
} 

public  class the Main {
     public  static  void main (String [] args) {
         // create a queue 
        MyQueue = MQ new new MyQueue ();
         // enqueue 
        mq.add (. 9 ); 
        mq.add ( . 8 ); 
        mq.add ( . 7 ); 

        // dequeue 
        System.out.println (mq.poll ()); 
        mq.add ( . 6);
        System.out.println(mq.poll());
        System.out.println(mq.poll());
        System.out.println(mq.poll());
        System.out.println(mq.isEmpty());
    }
}

 

Guess you like

Origin www.cnblogs.com/GjqDream/p/11591808.html