单队列和循环队列
队列
队列尾部添加,首部删除(先进先出)
单队列
单队列可能会出现假溢出问题(虽然有位置但是不能添加)
//这种队列有很大问题(不要用)
假溢出原因:队列不能循环添加值,用过的位置不能用了
队列对象代码
package com.how2java.tmallweb_springboot.test;
/**
* 手写队列
*/
public class MyQueue<T> {
private int maxSize = 10;//队列最大值(默认为10)
private int start = -1;//队列开始位置
private int end = -1;//队列结束为止
private int size = 0;//记录队列数据大小
//用于存储数据的数组
private T[] arr;
public MyQueue(){
arr = (T[]) new Object[maxSize];
}
//构造方法
public MyQueue(int maxSize){
this.maxSize = maxSize;
arr = (T[]) new Object[maxSize];
}
//添加
public void add(T t){
if(isFull()){
System.out.println("队列以及满了,不能再添加");
return;
}
arr[++end] = t;
size++;
}
//出队列
public T get(){
if(isEmpty()){
throw new RuntimeException("队列为空!");
}
size--;
return arr[++start];
}
//遍历队列
public void IteratorQueue(){
if(isEmpty())
return;
for(int i = start+1;i <= end;i++){
System.out.println(arr[i]);
}
}
//得到队列大小
public int getSize(){
return this.size;
}
//判断队列是否为空
public boolean isEmpty(){
return start == end;
}
//判断队列是否满了
public boolean isFull(){
return end==(maxSize-1);
}
}
测试代码
public static void main(String[] args) {
MyQueue<Integer> queue = new MyQueue<>(10);
//添加数据
for(int i = 5;i < 15;i++){
queue.add(i);
}
System.out.println(queue.getSize());
//遍历
queue.IteratorQueue();
//拿出数据
System.out.println(queue.get());
System.out.println(queue.getSize());
}
测试结果
10 //大小为10
5 //从5到14都是添加进去的
6
7
8
9
10
11
12
13
14
5 //拿出第一个
9 //剩余大小
循环队列
解决了队列假溢出问题。循环使用空间
注意:每次定义循环队列大小,使用时只能用定义大小-1的空间,另外一个空间是用来预留的。因为当最后一个空间用掉的话,start=end,导致报错(满了),其实没满。
队列对象代码
package com.how2java.tmallweb_springboot.test;
/**
* 手写队列
*/
public class MyQueue<T> {
private int maxSize = 10;//队列最大值(默认为10)
private int start = 0;//队列开始位置
private int end = 0;//队列结束为止
private int size = 0;//记录队列数据大小
//用于存储数据的数组
private T[] arr = null;
public MyQueue(){
arr = (T[]) new Object[maxSize];
}
//构造方法
public MyQueue(int maxSize){
this.maxSize = maxSize;
arr = (T[]) new Object[maxSize];
}
//添加
public void add(T t){
if(isFull()){
System.out.println("队列以及满了,不能再添加");
return;
}
arr[end] = t;
size++;
end = (end+1)%maxSize;//保证循环添加
}
//出队列
public T get(){
if(isEmpty()){
throw new RuntimeException("队列为空!");
}
size--;
T t = arr[start];
start = (start+1)%maxSize;//保证可以循环添加
return t;
}
//遍历队列
public void IteratorQueue(){
if(isEmpty())
return;
for(int i = start;i < start+size;i++){
System.out.println(arr[i%maxSize]);
}
}
//得到队列大小
public int getSize(){
return this.size;
}
//判断队列是否为空
public boolean isEmpty(){
return start == end;
}
//判断队列是否满了
public boolean isFull(){
return (end+1)%maxSize == start;
}
}
测试代码
public static void main(String[] args) {
MyQueue<Integer> queue = new MyQueue<>(10);
//添加数据
for(int i = 5;i < 14;i++){
queue.add(i);
}
System.out.println(queue.getSize());
//遍历
queue.IteratorQueue();
//拿出5个数据
for(int i = 0;i < 5;i++){
System.out.println(queue.get());
}
//输出现在的大小
System.out.println(queue.getSize());
//继续放入两个数据
for(int i = 0;i < 2;i++){
queue.add(i);
}
System.out.println(queue.size);
}
测试结果
9 //输出的队列的大小
5 //遍历
6
7
8
9
10
11
12
13
5 //拿出值
6
7
8
9
4 //剩余的大小
6 //加入两个数据后的大小
java自带队列的使用
public static void main(String[] args) {
//poll是出队列
//peek是拿头队列,但不会改变队列
//add入队列
Queue<Integer> queue = new LinkedList<>();
for(int i = 1;i < 5;i++){
//添加
queue.add(i);
}
System.out.println(queue.poll());//1
System.out.println(queue.poll());//2
System.out.println(queue.peek());//3
System.out.println(queue.poll());//3
System.out.println(queue.poll());//4
System.out.println(queue.peek());//null
}