数据结构与算法之队列

目录

1. 队列简介

2. 数组模拟队列

3. 数组模拟队列的代码实现

4. 数组模拟队列的缺点

5. 环形队列介绍

6. 数组模拟环形队列的代码实现

7.环形队列代码实现的测试


1. 队列简介

队列是线性结构的一种,其满足先进先出的特点。

2. 数组模拟队列

实现的思想主要是创建两个数分别代表队列的头部和尾部,增加数据后尾部数据+1,取出数据后头部数据+1.

3. 数组模拟队列的代码实现

这里的实现主要包括了向队列添加数据和取出数据以及遍历队列。这里使用的是object数组,在具体使用时,将object改成需要使用的数据类型即可。

public class QueueTest {
	private int maxSize;
	private int rear=-1;//表示队列的尾部(+1表示下一个添加的索引)
	private int front=-1;//表示队列的头部(+1表示下一个读取的索引)
	private Object[] queue;//表示一个队列的数组
	public QueueTest(int maxSize) {
		//创建一个队列
		this.maxSize=maxSize;
		queue=new Object[maxSize];
	}
	//增加数据
	public boolean addMember(Object newMember) {
		if(rear<maxSize-1) {
			rear++;
			queue[rear]=newMember;
			return true;
		}else {
			return false;
		}
	}
	//取出数据
	public Object getMember() {
		if(front<rear) {
			front++;
			return queue[front];
		}else {
			return null;
		}
	}
	//遍历队列
	public void showQueue() {
		for(int i=front;i<rear;) {
			i+=1;
			queue[i].toString();
		}
	}
}

4. 数组模拟队列的缺点

在上面的实现中,我们的数组在取出数据以后前面的 空间便没有用了。此后哪怕是队列空了也没办法使用前面已经使用过的数组,这样如果数组全部都使用完了要重新创建一个队列,很浪费资源。解决的方法是将队列变成环形队列。

5. 环形队列介绍

环形队列在当数组全部被使用时,再添加一个数据会将尾部数据继续加1,这时候用数组长度取模得到的就是数组前面空出的位置,实现了数组的重复使用。

6. 数组模拟环形队列的代码实现

这里主要实现了向队列中添加数据、取出数据、遍历队列中的数据、取出头部元素和求出队列中的数据个数。在中间会需要使用到判断队列是否满和队列是否为空两个小判断。

public class CircleQueueTest {
	private int rear=0;//尾指针,取模最大长度表示当前添加数据索引
	private int front=0;//头指针,取模最大长度表示当前读取数据索引
	private int maxSize;//队列最大长度
	private int[] circleQueue;//表示一个环形队列
	public CircleQueueTest(int maxSize) {
		this.maxSize=maxSize;
		circleQueue=new int[maxSize];
	}
	
	//增加数据
	public boolean addMember(int newMember) {
		if(!isFill()) {
			circleQueue[rear%maxSize]=newMember;
			rear++;
			return true;
		}
		return false;
	}
	//取出数据
	public int getMember() throws Exception{
		if(!isEmpty()) {
			int temporary=front%maxSize;
			front++;
			return circleQueue[temporary];
		}
		throw new Exception();
	}
	//遍历队列
	public void show() {
		if(!isEmpty()) {
			for(int i=front;i!=rear;i++) {
				System.out.println(circleQueue[i%maxSize]);
			}
		}else {
			System.out.println("队列为空");
		}
	}
	//求当前队列的元素个数
	public int getNum() {
		if(isEmpty())
			return 0;
		return (rear-front)%maxSize;
	}
	//显示头元素
	public int getHead() throws Exception{
		if(!isEmpty())
			return circleQueue[front];
		throw new Exception();
	}
	//判断是否已满
	public boolean isFill() {
		if(rear!=front&&rear%maxSize==front%maxSize)
			return true;
		return false;
	}
	//判断是否为空
	public boolean isEmpty() {
		if(rear==front)
			return true;
		return false;
	}
	
	public static void main(String[] args) {
		CircleQueueTest circleQueueTest=new CircleQueueTest(3);
		circleQueueTest.addMember(1);
		circleQueueTest.addMember(2);
		circleQueueTest.addMember(3);
		circleQueueTest.addMember(4);
		int a1;
		int a2;
		try {
			int head=circleQueueTest.getHead();
			System.out.println("头元素为:"+head);
			a1 = circleQueueTest.getMember();
			a2 = circleQueueTest.getMember();
			System.out.println("第一个元素为:"+a1+"*****第二个元素为:"+a2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		circleQueueTest.addMember(5);
		System.out.println("*************");
		circleQueueTest.show();
	}
}

7.环形队列代码实现的测试

上面的代码中包含了测试,其中再向队列添加元素“4”时,因为队列已满,没办法添加元素。但是在后面取出数据以后再向队列添加数据“5”又可以添加成功,所以在后面遍历数据时,遍历出来的数据时“3”和“5”。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值