java队列简单声明和使用

/**
 * 队列声明和使用
 * @author
 *
 */
public class Queue {
	private long[] queArray;
	private int maxSize;
	private int front;
	private int rear;
	private int nltcms;
	
	public Queue(int s){
		maxSize=s;
		queArray=new long[maxSize];
		front=0;
		rear=-1;
		nltcms=0;
	}
	
	/**
	 * 向队尾添加数据
	 * @param j
	 */
	public void insert(long j){
		if (rear==maxSize-1)rear=-1;
		queArray[++rear]=j;
		nltcms++;
	}
	
	/**
	 * 向队首删除数据
	 * @return
	 */
	public long remove(){
		long tmp=queArray[front++];
		if (front==maxSize)front=0;
		nltcms--;
		return tmp;
	}
	
	/**
	 * 查看
	 * @return
	 */
	public long peekFront(){
		return queArray[front];
	}
	
	public boolean isEmpty(){
		return nltcms==0;
	}
	
	public boolean isfull(){
		return nltcms==maxSize;
	}
	
	/**
	 * 队列大小
	 * @return
	 */
	public int size(){
		return nltcms;
	}
}



/**
 * 队列测试
 * @author 
 *
 */
public class QueueApp {

	/**
	 * 入口
	 * @param args
	 */
	public static void main(String[] args) {
		Queue pheQueue=new Queue(5);
		pheQueue.insert(10);
		pheQueue.insert(20);
		pheQueue.insert(30);
		pheQueue.insert(40);
		System.out.println("删除:"+ pheQueue.remove());
		System.out.println("删除:"+ pheQueue.remove());
		System.out.println("删除:"+ pheQueue.remove());
		pheQueue.insert(50);
		pheQueue.insert(60);
		pheQueue.insert(70);
		pheQueue.insert(80);
		while (!pheQueue.isEmpty()) {
			long n=pheQueue.remove();
			System.out.print(n+" ");
		}
	}
}
队列的特点是:先进先出!

你可能感兴趣的:(java)