队列
数组模拟队列
前提
- front指的是队列中的第一个数据的前一个位置,rear指的是队列中的最后一个数据的位置,maxSize指的是数组长度即队列大小
- front 的初始值 = -1,rear 的初始值 = -1
- 队列为空的判断条件:front == rear
- 队列满的判断条件:rear = maxSize - 1
- 队列中有效的数据个数:rear
思路
-
创建一个类来模拟队列
class Queue{ private int maxSize;//数组最大长度 private int front;//队列头,指向第一个数据的前一个位置 private int rear;//队列尾,指向最后一个数据的位置 private int[] arr;//用于存放数据,模拟队列 //队列构造器 public Queue(int arrMaxSize){ maxSize = arrMaxSize; arr = new int[maxSize]; front = -1;//指向队列头的前一个位置 rear = -1;//指向队列尾的位置,即最后一个数据 } }
-
判断队列是否为空
//队列是否为空 public boolean isEmpty(){ return front == rear; }
-
判断队列是否满
//队列是否已满 public boolean isFull(){ return rear == maxSize - 1; }
-
入队列,添加数据
//入队列,即添加数据 public void addQueue(int n){ if (isFull()){ System.out.println("队列已满!"); return; } rear ++; arr[rear] = n; }
-
出队列,取出数据
//出队列,即取出数据 public int getQueue(){ if (isEmpty()){ throw new RuntimeException("队列为空!"); } return arr[++front]; }
-
显示所有数据
//显示队列所有数据 public void showQueue(){ if (isEmpty()){ System.out.println("队列为空!"); return; } for (int i = 0; i < arr.length; i++) { System.out.print("arr["+ i +"]="+ arr[i] + "\t"); } System.out.println(); }
-
显示队列头数据
//取队列头的数据 public int headQueue(){ if (isEmpty()){ throw new RuntimeException("队列为空!"); } return arr[++front]; }
缺点
数组无法进行复用,也就是数据存入队列后,队列满了的情况下取出所有数据,在重新添加数据到队列时,会出现队列满的情况
完整代码
package ArrayQueue;
import java.util.Scanner;
/**
* Created by lenovo on 2021/3/13.
*/
public class ArrayQueue {
public static void main(String[] args) {
Queue queue = new Queue(3);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
//输出用户菜单
while(loop){
System.out.println("s(show):显示队列");
System.out.println("a(add):添加数据");
System.out.println("g(get):取出数据");
System.out.println("h(head):显示队列头的数据");
System.out.println("e(exit):退出程序!");
key = scanner.next().charAt(0);
switch (key){
case 's':
queue.showQueue();
break;
case 'a':
System.out.println("请输入一个数:");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case 'g':
try {
int res = queue.getQueue();
System.out.println("取出的数据为:" + res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = queue.headQueue();
System.out.println("取出的数据为:" + res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
}
}
System.out.println("程序退出!");
}
}
class Queue{
private int maxSize;//数组最大长度
private int front;//队列头
private int rear;//队列尾
private int[] arr;//用于存放数据,模拟队列
//队列构造器
public Queue(int arrMaxSize){
maxSize = arrMaxSize;
arr = new int[maxSize];
front = -1;//指向队列头的前一个位置
rear = -1;//指向队列尾的位置,即最后一个数据
}
//队列是否已满
public boolean isFull(){
return rear == maxSize - 1;
}
//队列是否为空
public boolean isEmpty(){
return front == rear;
}
//入队列,即添加数据
public void addQueue(int n){
if (isFull()){
System.out.println("队列已满!");
return;
}
rear ++;
arr[rear] = n;
}
//出队列,即取出数据
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空!");
}
return arr[++front];
}
//显示队列所有数据
public void showQueue(){
if (isEmpty()){
System.out.println("队列为空!");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.print("arr["+ i +"]="+ arr[i] + "\t");
}
System.out.println();
}
//取队列头的数据
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空!");
}
return arr[++front];
}
}
数组模拟环形队列
前提
- front指的是队列中第一个数据的位置,rear指的是队列中最后一个数据的后一个位置,maxSize指的是数组长度即队列大小
- front 的初始值 = 0,rear 的初始值 = 0
- 队列为空的判断条件:front == rear
- 队列满的判断条件:(rear + 1)% maxSize == front
- 队列中有效的数据个数:(rear + maxSize - front) % maxSize
思路
- 创建一个类模拟环形队列
class CQueue{
private int maxSize;
private int front;//指向队列的第一个数据的位置
private int rear;//指向队列最后一个数据的后一个位置
private int[] arr;
//队列构造器
public CQueue(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()){
System.out.println("队列已满!");
return;
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}
- 取出数据,出队列
//取出数据出队列
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空!");
}
int temp = arr[front];
front = (front + 1) % maxSize;
return temp;
}
- 获取队列中有效数据的个数
//获取队列中有效数据的个数
public int size(){
return (rear + maxSize - front) % maxSize;
}
- 显示队列所有数据
//显示队列所有数据
public void showQueue(){
if (isEmpty()){
System.out.println("队列为空!");
return;
}
for (int i = front; i < front + size(); i++) {
System.out.print("arr[" + i % maxSize + "]=" + arr[i % maxSize] + "\t");
}
System.out.println();
}
- 显示队列头数据
//取出队列头的数据
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空!");
}
return arr[front];
}
解决的问题
解决了数组无法进行复用的情况,在取出所有数据后再重新存入数据到队列中,可以存放成功,不会出现队列满的情况
完整代码
package ArrayQueue;
import java.util.Scanner;
/**
* Created by lenovo on 2021/3/13.
*/
public class CircleQueue {
public static void main(String[] args) {
CQueue cqueue = new CQueue(4);
char key = ' ';
Scanner scanner = new Scanner(System.in);
boolean loop = true;
//输出用户菜单
while(loop){
System.out.println("s(show):显示队列");
System.out.println("a(add):添加数据");
System.out.println("g(get):取出数据");
System.out.println("h(head):显示队列头的数据");
System.out.println("e(exit):退出程序!");
key = scanner.next().charAt(0);
switch (key){
case 's':
cqueue.showQueue();
break;
case 'a':
System.out.println("请输入一个数:");
int value = scanner.nextInt();
cqueue.addQueue(value);
break;
case 'g':
try {
int res = cqueue.getQueue();
System.out.println("取出的数据为:" + res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = cqueue.headQueue();
System.out.println("取出的数据为:" + res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop = false;
break;
}
}
System.out.println("程序退出!");
}
}
class CQueue{
private int maxSize;
private int front;//指向队列的第一个数据的位置
private int rear;//指向队列最后一个数据的后一个位置
private int[] arr;
//队列构造器
public CQueue(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()){
System.out.println("队列已满!");
return;
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}
//取出数据出队列
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空!");
}
int temp = arr[front];
front = (front + 1) % maxSize;
return temp;
}
//显示队列所有数据
public void showQueue(){
if (isEmpty()){
System.out.println("队列为空!");
return;
}
for (int i = front; i < front + size(); i++) {
System.out.print("arr[" + i % maxSize + "]=" + arr[i % maxSize] + "\t");
}
System.out.println();
}
//获取队列中有效数据的个数
public int size(){
return (rear + maxSize - front) % maxSize;
}
//取出队列头的数据
public int headQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空!");
}
return arr[front];
}
}