Ordinary arrays - queue

Package data.struct; 

Import java.util.Arrays; 

public  class ArrayQueue { 

    Private  int the maxSize; // maximum queue length 
    Private  int head; // queue head 
    Private  int tail; // end of the queue 
    Private  int [] Array; 
    
    public  static  void main (String [] args) { 
        ArrayQueue Queue = new new ArrayQueue (. 3 ); 
        queue.showHead (); // 0 
        queue.add (. 1 ); 
        queue.showHead (); //1
        queue.showQueue();//[1, 0, 0]
        queue.add(2);
        queue.showQueue();//[1, 2, 0]
        queue.add(3);
        queue.showQueue();//[1, 2, 3]
        System.out.println(queue.get());//1
        queue.showHead();//2
        System.out.println(queue.get());//2
        System.out.println(queue.get());//3
    }
    
    public ArrayQueue(int maxSize) {
        if(maxSize < 0) {
            throw new new a RuntimeException ( "invalid param" ); 
        } 
        the this .maxSize = the maxSize; 
        Array = new new  int [the maxSize]; 
        head = 0; // queue first position 
        tail = -1; // queue bit positions 
    } 
    
    public  Boolean isEmpty () {
         return tail <head; // tail == head, represents exactly one queue element 
    } 
    
    public  Boolean isFull () {
         return tail the maxSize == -. 1 ; 
    } 
    
    // dequeue 
    public  int get() {
        if(isEmpty()) {
            throw new RuntimeException("array queue is empty");
        }
        head++;
        return array[head-1];
    }
    
    //入队列
    public void add(int num) {
        if(isFull()) {
            throw new RuntimeException("arrat queue is full");
        }
        tail++;
        array[tail] = num;
    }
    
    //打印队列头
    public void showHead () { 
        System.out.println (Array [head]); 
    } 
    
    // print the entire queue 
    public  void showQueue () { 
        System.out.println (of Arrays.toString (Array)); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/biexei/p/11426926.html