队列
介绍:
队列是一个有序列表,可以用数组或者链表来实现,遵循先入先出的原则(先存入队列的数据,要先取出,后存入的数据要后取出)
数组模拟队列:
思路分析:
1)将尾指针往后移(rear+1),当front==rear[空队列]
2)若尾指针rear小于队列的最大下表maxSize-1,则将数据存入rear所指的数组元素中,否则无法存入数据. rear==maxSize-1[队列满]
代码实现:
import java.util.Scanner;
/**
* @author 山渐青
* 数组模拟队列
*/
public class ArrayQueueDemo {
public static void main(String[] args){
// 创建一个队列
ArrayQueue arrayQueue = new ArrayQueue(3);
char key = ' '; //用户输入
Scanner scanner = new Scanner(System.in);
boolean loop = true;
// 输出一个用户菜单
while(loop){
System.out.println("s(show): 显示队列");
System.out.println("e(exit): 退出程序");
System.out.println("a(add): 添加数据到队列");
System.out.println("g(get): 从队列取出数据");
System.out.println("h(head): 查看队列头的数据");
key = scanner.next().charAt(0); // 接收用户输出第一个字符
switch (key){
case 's':
arrayQueue.showQueue();
break;
case 'e':
scanner.close();
loop = false;
break;
case 'a':
System.out.println("输入一个数");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try{
int res = arrayQueue.getQueue();
System.out.printf("取出的数据是%d",res);
} catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try{
int res = arrayQueue.headQueue();
System.out.printf("队列头数据是%d",res);
} catch (Exception e){
System.out.println(e.getMessage());
}
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
// 使用数组模拟队列--编写一个类
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 isFull(){
return rear == maxSize-1;
}
// 队列为空
public boolean isNull(){
return rear == front;
}
// 添加数据到队列
public void addQueue(int n){
// 判断队列是否满
if(isFull()){
System.out.println("队列满,不能加入数据");
return;
}
rear++;
arr[rear] = n;
}
// 取出数据
public int getQueue(){
// 判断是否为空
if(isNull()){
//抛出异常,自动返回,不需要return
throw new RuntimeException("队列空,不能取数据");
}
front++;
return arr[front];
}
//显示队列的所有数据
public void showQueue(){
if(isNull()){
System.out.println("队列为空");
return;
}
for(int i=0; i<arr.length; i++){
System.out.printf("arr[%d]=%d\n",i,arr[i]);
}
}
// 显示队列的头数据
public int headQueue(){
if(isNull()){
//抛出异常,自动返回,不需要return
throw new RuntimeException("队列空,不能取数据");
}
return arr[front+1];
}
}
数组模拟环形队列
思路分析:
1)front变量的含义做一个调整:front就指向队列的第一个元素,arr[front]就是队列的第一个元素
front初始值为0
2)rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置,空出一个空间做一个约定,rear初始值为0
3)当队列满时,条件是(rear + 1)%maxSize = front[满],当队列为空时,rear==front[空]
4)队列中有效的数据的个数(rear+maxSize-front)%maxSize
代码实现:
import java.util.Scanner;
/**
* @author 山渐青
*/
public class CircleArrayQueue {
public static void main(String[] args){
// 创建一个队列
CircleArray arrayQueue = new CircleArray(4);
char key = ' '; //用户输入
Scanner scanner = new Scanner(System.in);
boolean loop = true;
// 输出一个用户菜单
while(loop){
System.out.println("s(show): 显示队列");
System.out.println("e(exit): 退出程序");
System.out.println("a(add): 添加数据到队列");
System.out.println("g(get): 从队列取出数据");
System.out.println("h(head): 查看队列头的数据");
key = scanner.next().charAt(0); // 接收用户输出第一个字符
switch (key){
case 's':
arrayQueue.showQueue();
break;
case 'e':
scanner.close();
loop = false;
break;
case 'a':
System.out.println("输入一个数");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g':
try{
int res = arrayQueue.getQueue();
System.out.printf("取出的数据是%d",res);
} catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try{
int res = arrayQueue.headQueue();
System.out.printf("队列头数据是%d",res);
} catch (Exception e){
System.out.println(e.getMessage());
}
break;
default:
break;
}
}
System.out.println("程序退出");
}
}
class CircleArray{
private int maxSize; //表的最大容量
private int front; //队列头
private int rear; //队列尾
private int arr[]; //该数组模拟队列,存放数据
public CircleArray(int arrMaxSize){
maxSize = arrMaxSize;
arr = new int[maxSize];
}
// 判断队列是否满
public boolean isFull(){
return (rear+1)%maxSize == front;
}
// 是否为空
public boolean isNull(){
return rear == front;
}
// 添加数据到队列
public void addQueue(int n){
// 判断队列是否满
if(isFull()){
System.out.println("队列满,不能加入数据");
return;
}
// 直接将数据加入
arr[rear] = n;
// 将rear后移
rear = (rear+1)%maxSize;
}
// 取出数据
public int getQueue(){
// 判断是否为空
if(isNull()){
//抛出异常,自动返回,不需要return
throw new RuntimeException("队列空,不能取数据");
}
// 1.先把front 对应的值保存到临时变量中
// 2.将front后移 ,考虑取模,防止越界
// 3.将临时保存的变量返回
int value = arr[front];
front = (front+1)%maxSize;
return value;
}
// 求出当前队列有效数据个数
public int size(){
return (rear + maxSize - front)%maxSize;
}
//显示队列的所有数据
public void showQueue(){
if(isNull()){
System.out.println("队列为空");
return;
}
// 从front开始遍历,遍历多少个元素
for(int i=front; i<front+size(); i++){
System.out.printf("arr[%d]=%d\n",i % maxSize,arr[i % maxSize]);
}
}
// 显示队列的头数据
public int headQueue(){
if(isNull()){
//抛出异常,自动返回,不需要return
throw new RuntimeException("队列空,不能取数据");
}
return arr[front];
}
}