用数组实现的队列

public class Queue {
    private int maxSize;//数组大小
    private long[] queArray;
    private int front;//队头
    private int rear;//队尾
    private int nItems;//数据项个数
    
    //create Queue
    public Queue(int s){
        maxSize = s;
        queArray = new long[s];
        front = 0;
        rear = -1;
        nItems = 0;
    }
    
    //insert Queue
    public void insert(long j){
        if(rear == maxSize-1)//循环
            rear = -1;
        queArray[++rear] = j;
        nItems++;
    }
    
    //remove Queue
    public long remove(){
        long temp = queArray[front++];
        if(front == maxSize)
            front = 0;
        nItems--;
        
        return temp;
    }
    
    public long peedFront(){
        return queArray[front];
    }
    
    public boolean isEmpty(){
        return nItems == 0;
    }
    
    public boolean isFull(){
        return nItems == maxSize;
    }
    
    public int ItemSize(){
        return nItems;
    }
}
public class QueueApp {
    public static void main(String[] args) {
        Queue theQueue = new Queue(5);
        theQueue.insert(10);
        theQueue.insert(20);
        theQueue.insert(30);
        theQueue.insert(40);// rear 3 nItems 4
        
        theQueue.remove();
        theQueue.remove();
        theQueue.remove();// front 3 nItems 1
        
        theQueue.insert(50);
        theQueue.insert(60);
        theQueue.insert(70);
        theQueue.insert(80);// rear 2 nItems 5
        
        while(!theQueue.isEmpty()){
            System.out.println(theQueue.remove());
        }
    }
}

 


队列

  计算机科学中,队列是一种数据结构,有点类似栈,只是在队列中第一个被插入的数据项也会被最先移除(FIFO),而在栈中,最后插入的数据项最先移除(LIFO)。

 

posted @ 2013-01-26 12:10  tangrongyue  Views(3480)  Comments(0Edit  收藏  举报