基于数组的循环队列和基于链表的队列

数据的插入和取出涉及对当前数据量nItems属性的修改,大量的插入、移除操作时,对nItems属性的操作可能影响性能,可参考不包含数据项个数属性的队列实现。

基于数组的循环队列

包含数据项个数属性

package datastructure.c4.queue.queuedef;

public class Queue {
	private int maxSize;
	private long[] queArray;
	private int front;
	private int rear;
	private int nItems;
	public Queue(int s){
		maxSize=s;
		queArray=new long[maxSize];
		front=0;
		rear=-1;
		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;
	}
}

package datastructure.c4.queue.queuedef;

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);
		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.println("");
	}
}

不包含数据项个数属性

package datastructure.c4.queue.nonitems;

public class QueueWithoutN {
	private int maxSize;
	private long[] queArray;
	private int front;
	/**
	 * rear作为写值的标志,插入时会在rear的下一个位置写入新值,
	 * 当rear=maxSize-1,也就是rear指向的数组的最后一个值,
	 * 要将rear设为-1,形成循环队列
	 */
	private int rear;
	public QueueWithoutN(int s){
		maxSize=s+1;
		queArray=new long[maxSize];
		front=0;
		rear=-1;
	}
	/**
	 * 新值位于rear的后一个位置
	 * @param j
	 */
	public void insert(long j){
		if(rear==maxSize-1){
			rear=-1;
		}
		queArray[++rear]=j;
	}
	/**
	 * 取值时取front位置当前值,front后移一位
	 * 当front是数组最后一个元素时,后移一位后front=maxSize,
	 * 令front=0
	 * @return
	 */
	public long remove(){
		long temp=queArray[front++];
		if(front==maxSize){
			front=0;
		}
		return temp;
	}
	public long peek(){
		return queArray[front];
	}
	/**
	 * 1、rear+1==front说明下次写入值会覆盖front当前值,说明队列为空
	 * 2、front+maxSize-1==rear --> rear-front=maxSize-1(数组下标最大值),
	 * 只能rear=maxSize-1,front=0(front是数组第一个元素,rear是数组最后一个元素),
	 * 下次写入也会覆盖front当前值
	 * @return front与rear位置差1的所有情况返回true
	 */
	public boolean isEmpty(){
		return (rear+1==front || front+maxSize-1==rear);
	}
	/**
	 * 队列为空时rear位于front前一个位置,
	 * 写满时,rear位于front前两个位置
	 * @return front与rear位置差2的所有情况返回true
	 */
	public boolean isFull(){
		return (rear+2==front || front+maxSize-2==rear);
	}
	/**
	 * 1、rear>=front,说明未循环,元素数量为rear与front之间的元素数
	 * 2、rear<front,说明已循环插入,(maxSize-front)为front到数组最后的元素数,
	 * (rear+1)为数组开始(下标为0)到rear间的元素数
	 * @return
	 */
	public int size(){
		if(rear>=front){
			return rear-front+1;
		}else{
			return (maxSize-front)+(rear+1);
		}
	}
}

基于双端链表的队列

package datastructure.c5.linklist.linkqueue;

public class Link {
	public long dData;
	public Link next;
	public Link(long d){
		dData=d;
	}
	public void displayLink(){
		System.out.print(dData+"  ");
	}
}

package datastructure.c5.linklist.linkqueue;

public class FirstLastList {
	private Link first;
	private Link last;
	public FirstLastList(){
		first=null;
		last=null;
	}
	public boolean isEmpty(){
		return first==null;
	}
	public void insertLast(long dd){
		Link newLink =new Link(dd);
		if(isEmpty()){
			first=newLink;
		}else{
			last.next=newLink;
		}
		last=newLink;
	}
	public long deleteFirst(){
		long temp=first.dData;
		if(first.next==null){
			last=null;
		}
		first=first.next;
		return temp;
	}
	public void displayList(){
		Link current=first;
		while(current!=null){
			current.displayLink();
			current=current.next;
		}
		System.out.println("");
	}
}

package datastructure.c5.linklist.linkqueue;

public class LinkQueue {
	private FirstLastList theList;
	public LinkQueue(){
		theList=new FirstLastList();
	}
	public boolean isEmpty(){
		return theList.isEmpty();
	}
	public void insert(long j){
		theList.insertLast(j);
	}
	public long remove(){
		return theList.deleteFirst();
	}
	public void displayQueue(){
		System.out.print("Queue (front-->rear):");
		theList.displayList();
	}
}

package datastructure.c5.linklist.linkqueue;

public class LinkQueueApp {
	public static void main(String[] args) {
		LinkQueue theQueue=new LinkQueue();
		theQueue.insert(20);
		theQueue.insert(40);
		
		theQueue.displayQueue();
		
		theQueue.insert(60);
		theQueue.insert(80);
		
		theQueue.displayQueue();
		
		theQueue.remove();
		theQueue.remove();
		
		theQueue.displayQueue();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值