首页 > 编程语言 > 详细

学习java数据结构基础知识之队列

时间:2015-05-14 01:01:54      阅读:228      评论:0      收藏:0      [点我收藏+]
队列是先进先出。
利用java语言实现队列代码:
/*
 * 队列
 */
public class Queue {
	private int maxSize;          //最大队列
	private long[] queArray;      //队列数组
	private int front;            //队头
	private int rear;             //队尾
	private int nItems;           //当前队列元素个数
	
	//构造函数
	public Queue(int s) {
		super();
		this.maxSize = s;
		this.queArray = new long[maxSize];
		this.front = 0;
		this.rear = -1;
		this.nItems = 0;
	}
	//插入元素
	public void insert(long j){
		if(rear ==(maxSize-1)){
			rear =-1;
		}
			queArray[++rear] =j;
			nItems++;
	}
	//删除元素
	public long remove(){
		long temp =queArray[front++];
		if (front ==maxSize) {
			front =0;
		}
		nItems--;
		return temp;
	}
	//队列头
	public long peekFront() {
		return queArray[front];
	}
	//队列是否为空
	public boolean isEmpty(){
		return (nItems ==0);
	}
	//队列是否满了
	public boolean isFull() {
		return(nItems==maxSize);
	}
	//队列长度
	public int size() {
		return nItems;
	}

}
<pre name="code" class="java">public class QueueApp {

	public static void main (String[] arg) {
		Queue theQueue =new Queue(5);
		
		theQueue.insert(10);
		theQueue.insert(20);
		theQueue.insert(30);
		theQueue.insert(40);
		
		theQueue.remove();
		theQueue.remove();
		theQueue.remove();
		
		theQueue.insert(50);
		theQueue.insert(60);
		theQueue.insert(70);
		theQueue.insert(80);
		
		while (!theQueue.isEmpty()) {
			long n = theQueue.remove();
			System.out.print(n);
			System.out.print("  ");		
		}
		System.out.print(" ");
	}
}




学习java数据结构基础知识之队列

原文:http://blog.csdn.net/cool_easy/article/details/45703815

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!