队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作。遵循的是先入先出的原则。
数组的使用方法
使用数组模拟队列
一般来说,front指向头部,且指向头部的前一个位子
rear指向尾部(包含尾部的数据)
//使用数组模拟队列 编写一个ArrayQueue
class ArrayQueue{
private int MaxSize; //队列最大容量
private int front; //队列头
private int rear; //队列尾部
private int[] arr; //该数组用于存放数据,模拟队列
//创建队列的构造器
public ArrayQueue(int arrMaxSize)
{
MaxSize=arrMaxSize;
arr=new int[MaxSize];
front=-1; //指向队列头,front是指向队列头的前一个位子
rear=-1; //指向队列尾部(包含队列尾的数据)
}
//判断队列是否满
public boolean issFull(){
return rear==MaxSize-1;
}
//判断队列是否为空
public boolean issEmpty(){
return rear==front;
}
//添加数据到队列
public void addQueue(int n)
{
//判断队列是否满
if (issFull())
{
System.out.println("队列满,不能加入数据");
}
else {
rear++; //让rear后移
arr[rear]=n;
}
}
//获取队列的数据,出队列
public int getQueue()
{
//判断队列是否空
if (issEmpty())
{
//抛异常处理,不return -1的理由是有可能值为-1
throw new RuntimeException("队列为空");
}
else {
front++;
}
return arr[front];
}
//显示队列的所有数据
public void showQueue(){
if (issEmpty())
{
System.out.println("队列为空");
return;
}
for (int ss:arr)
{
System.out.print(ss+" ");
}
System.out.println();
}
//显示队列的头数据,不是取数据
public int headQueue(){
if (issEmpty())
{
System.out.println("队列为空");
throw new RuntimeException("队列为空");
}
return arr[front+1]; //因为front是头部的前一个数据
}
}
接着我们进行调用
ArrayQueue arrayQueue=new ArrayQueue(3);
arrayQueue.addQueue(3);
arrayQueue.addQueue(1);
arrayQueue.addQueue(3);
System.out.println(arrayQueue.issEmpty()); //false
System.out.println(arrayQueue.issFull()); //true
System.out.println(arrayQueue.getQueue()); //3
arrayQueue.showQueue(); //3 1 3
System.out.println(arrayQueue.headQueue()); //3
可能存在的问题:
1.目前数组使用一次就不能用,没有达到复用的效果
2.将这个数组使用算法,改进成一个环形的队列取模:%
集合的使用方法
LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。也可以将ArrayDeque类来当Queue接口。
1.添加元素
添加元素可以使用add()、offer()方法来添加
add()方法,在queue上限时继续添加,会抛出异常。
offer()方法,在queue上限时继续添加,会得到返回值false。
//暂时找不到怎么设置上限(╯▔皿▔)╯
Queue<String> queue=new ArrayDeque<>();
//Queue<String> queue=new LinkedList<>();
queue.offer("1");
queue.offer("3");
queue.add("4");
for(String q:queue){
System.out.println(q); //1,3,4
}
2.删除元素
删除元素可以使用poll()、remove() 方法来删除,他们都用来删除第一个元素,返回值为String。但是remove可以删除指定元素,返回值为boolean类型
remove()方法,删除空集合时,会抛出异常
poll()方法,删除空集合时,只是返回null
Queue<String> queue=new ArrayDeque<>();
//Queue<String> queue=new LinkedList<>();
queue.offer("1");
queue.offer("3");
queue.add("4");
boolean c=queue.remove("3");
System.out.println(c); //true
String s=queue.remove();
System.out.println(s); //1
String p=queue.poll();
System.out.println(p); //4
String f=queue.poll();
// String d=queue.remove();
System.out.println(f); //null 集合已经为空了
// System.out.println(d); //抛出异常,因为remove不能继续删除了
3.查询元素
查询元素可以使用peek()、element(),他们用来查询首元素
peek()方法,在队列为空的时候查询,返回值为null
element()方法,在队列为空的时候查询,会抛出异常
Queue<String> queue=new ArrayDeque<>();
//Queue<String> queue=new LinkedList<>();
queue.offer("1");
queue.offer("3");
queue.add("4");
queue.poll();
System.out.println(queue.peek()); //3
System.out.println(queue.element()); //3
queue.poll();
queue.poll();
System.out.println(queue.peek()); //null
// System.out.println(queue.element()); //抛出异常