队列介绍
- 队列是一个有序列表,可以用数组或链表来实现
- 遵循先进先出的原则.即:先存入队列的数据,要先取出.后存入的数据要后取出
队列实现
- 包含存放元素的数组,队列头指针,队列尾指针,最大容量
- 当插入元素时尾指针后移
- 判断是否满:尾指针 rear = maxSize - 1
- 当从队列获取元素: 头指针往后移
- 判断队列是否为空:rear == front
java 实现:
public class ArrayQueue {
/*存放元素的数组*/
private int[] elements;
/*最大容量*/
private int maxSize;
/*队列头指针*/
private int front;
/*队列尾指针*/
private int rear;
/**
* 构造函数初始化容量
*
* @param size 容量
*/
public ArrayQueue(int size) {
this.maxSize = size;
this.rear = -1;
this.front = -1;
elements = new int[size];
}
public ArrayQueue() {
}
/**
* 判断队列是否满
*
* @return boolean
*/
public boolean isFull() {
return rear == maxSize - 1;
}
/**
* 判断队列是否为空
*
* @return boolean
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 向队列中添加元素
*
* @param ele ele
*/
public void add(int ele) {
if (isFull()) {
throw new RuntimeException("队列已满,不能再添加元素");
}
rear++;
elements[rear] = ele;
}
/**
* 获取队列头的元素
*
* @return ele
*/
public int get() {
if (isEmpty()) {
throw new RuntimeException("队列为空");
}
front++;
return elements[front];
}
/**
* 展示队列所有元素
*/
public void show() {
for (int i = front + 1; i < maxSize; i++) {
System.out.println(elements[i]);
}
}
}
上面实现的队列并不能重复使用当头指针和尾指针移动到数组最后索引位置时此时队列为空且不能再往队列中添加元素,此时考虑将队列做成环形队列,队列头指针在数组某位此时数组头部有元素出队列,那这个位置又可以用来存储元素,实现循环利用.
相对上面的实现原则做出调整:
- front指向队列的第一个元素,front的初始值为0
- rear指向最后一个元素的后一个位置,rear初始值为0
- 当队列满时满足:front == (rear + 1) % maxSize
- 队列为空满足:front == rear
- 队列中有效数据的个数:(rear + maxSize - front) % maxSize
环形队列java实现:
/**
* 环形队列实现:
* 1. front指向队列的第一个元素,front的初始值为0
* 2. rear指向最后一个元素的后一个位置,rear初始值为0
* 3. 当队列满时满足:front == (rear + 1) % maxSize
* 4. 队列为空满足:front == rear
* 5. 队列中有效数据的个数:(rear + maxSize - front) % maxSize
*
* @author ycr
* @date 2021/3/25
*/
public class CircleQueue {
/*存放元素的数组*/
private int[] elements;
/*最大容量*/
private int maxSize;
/*队列头指针*/
private int front;
/*队列尾指针*/
private int rear;
/**
* 构造函数初始化容量
*
* @param size 容量
*/
public CircleQueue(int size) {
this.maxSize = size;
this.front = 0;
this.rear = 0;
elements = new int[size];
}
public CircleQueue() {
}
/**
* 判断队列是否满
* front == (rear + 1) % maxSize
*
* @return boolean
*/
public boolean isFull() {
return front == (rear + 1) % maxSize;
}
/**
* 判断队列是否为空
*
* @return boolean
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 向队列中添加元素
*
* @param ele ele
*/
public void add(int ele) {
if (isFull()) {
throw new RuntimeException("队列已满,不能再添加元素");
}
elements[rear] = ele;
rear = (rear + 1) % maxSize;
}
/**
* 获取队列头的元素
*
* @return ele
*/
public int get() {
if (isEmpty()) {
throw new RuntimeException("队列为空");
}
int value = elements[front];
front = (front + 1) % maxSize;
return value;
}
/**
* 展示队列所有元素
*/
public void show() {
for (int i = front; i < front + size(); i++) {
System.out.println(elements[i % maxSize]);
}
}
public int size() {
return (rear + maxSize - front) % maxSize;
}
/**
* 查看队列头节点元素
*
* @return
*/
public int peek() {
return elements[front];
}
/*public static void main(String[] args) {
char key = ' ';
CircleQueue queue = new CircleQueue(3);
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop) {
System.out.println("s(show) 显示队列");
System.out.println("e(exit) 退出程序");
System.out.println("g(get) 获取元素");
System.out.println("h(head) 查看队列头部");
System.out.println("a(add) 添加元素");
key = scanner.next().charAt(0);
switch (key) {
case 's':
queue.show();
break;
case 'a':
try {
System.out.println("输入一个数");
int value = scanner.nextInt();
queue.add(value);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'g':
try {
System.out.println("获取到元素: " + queue.get());
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'h':
System.out.println(queue.peek());
case 'e':
scanner.close();
loop = false;
break;
default:
break;
}
}
}*/
public static void main(String[] args) {
CircleQueue queue = new CircleQueue(5);
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
// queue.add(1);
queue.show();
queue.get();
queue.add(144);
System.out.println("-------------------------------");
queue.show();
}
}