普通
思路分析:
1)将尾指针往后移: rear+1, 当front== rear [ 空]
2)若尾指针rear小于队列的最大下标maxSize-1,则将数据存入rear所指的数
组元素中,否则无法存入数据。rear == maxSize- 1[队列满]
代码实现:
package com.main.dataStructures;
public class ArrayQueueDemo{
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue(3);
arrayQueue.addQueue(2);
arrayQueue.addQueue(1);
arrayQueue.addQueue(3);
System.out.println(arrayQueue.headQueue());
arrayQueue.allQueue();
}
}
class ArrayQueue {
private int maxSize;
private int front; //指向队列头,front是指向队列头的前一个位置
private int rear; //指向队列尾,rear是指向队列尾的位置,包括队列尾
private int []arr; //存放数据
public ArrayQueue(int arrMaxSize){
maxSize = arrMaxSize;
front = -1;
rear = -1;
arr = new int[maxSize];
}
//判空
public boolean isEmpty(){
return front == rear;
}
//判满
public boolean isFull(){
return rear == maxSize-1;
}
//添加数据到队列
public void addQueue(int n){
if (isFull()){
throw new RuntimeException("队列已满,无法添加");
}
rear++;
arr[rear] = n;
}
//获取队列,出队列
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,无法获取数据");
}
front++;
return arr[front];
}
//显示队列所有数据
public void allQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空");
}
for (int ar : arr) {
System.out.println(ar);
}
}
//显示队列头
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,无法获取数据");
}
return arr[front+1];
}
}
环形
思路如下:
1.front变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说arr[front]就是队列的第一个元素
front的初始值= 0
2.rear 变量的含义做-一个调整: rear 指向队列的最后- -个元素的后一个位置.因为希望空出一- 个空间做为约定.
rear的初始值=0
3.当队列满时,条件是(rear +1) % maxSize==front [ 满]
4.对队列为空的条件,rear== front空
5.当我们这样分析,队列中有效的数据的个数(rear + maxSize- front) % maxSize //rear= 1 front=0
6.我们就可以在原来的队列.上修改得到,一个环形队列
代码实现:
package com.main.dataStructures;
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
char key = ' ';//接受用户输入
Scanner scanner = new Scanner(System.in);
boolean loop = true;
while (loop){
System.out.println("a:添加");
System.out.println("g:弹出");
System.out.println("l:显示全部");
System.out.println("h:头");
System.out.println("e:退出");
key = scanner.next().charAt(0);
switch (key){
case 'a':
System.out.println("输入一个数");
circleArrayQueue.addQueue(scanner.nextInt());
break;
case 'g':
circleArrayQueue.getQueue();
break;
case 'l':
circleArrayQueue.allQueue();
break;
case 'h':
System.out.println(circleArrayQueue.headQueue());
break;
case 'e':
scanner.close();
loop=false;
break;
default:
break;
}
}
}
}
class CircleArrayQueue{
private int maxSize;
private int front; //front 就指向队列的第一个元素
private int rear; //rear 指向队列的最后一个元素的后一个位置.
private int []arr; //存放数据
public CircleArrayQueue(int arrMaxSize){
maxSize = arrMaxSize;
arr = new int[maxSize];
}
//判空
public boolean isEmpty(){
return front == rear;
}
//判满
public boolean isFull(){
return (rear+1) % maxSize == front;
}
//添加数据到队列
public void addQueue(int n){
if (isFull()){
throw new RuntimeException("队列已满,无法添加");
}
arr[rear] = n;
rear = (rear+1)%maxSize;
}
//获取队列,出队列
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,无法获取数据");
}
int value = arr[front];
front = (front+1)%maxSize;
return value;
}
//显示队列所有数据
public void allQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空");
}
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d]=%d",i % maxSize,arr[i%maxSize]);
System.out.println();
}
}
public int size(){
return (rear-front+maxSize)%maxSize;
}
//显示队列头
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,无法获取数据");
}
return arr[front];
}
}