队列:是一个有序列表遵循先进先出,后进后出的原则。
数组模拟队列:
maxsize代表最大容量
front代表队列头的前一个元素
rear代表队列尾的元素
import java.util.Scanner;
/**
* 使用数组模拟队列
*/
public class ArrayQueueDemo {
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue(5);
Scanner scanner = new Scanner(System.in);
char key = ' ';
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):查看队列头的数据");
System.out.println("\n请输入指令:");
key = scanner.next().charAt(0);//接收一个字符(0就是一个)
switch(key) {
case's':
arrayQueue.show();
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\n",res);
}catch(Exception e) {
System.out.println(e.getMessage());
}
break;
case'e':
scanner.close();//关闭控制台
loop = false;
break;
}
}
System.out.println("程序退出成功!");
}
}
class ArrayQueue{
//最大容量
private int maxsize;
//头指针
private int front;
//尾指针
private int rear;
//存放数据
private int[] arr;
public ArrayQueue(int maxsize){
this.maxsize = maxsize;
arr = new int[this.maxsize];
//默认指向队列头,front是队列头的前一个位置
front = -1;
//指向队列尾的数据
rear = -1;
}
//判断队列是否满,尾指针指向最大容量 - 1 时。
public boolean IsFull(){ return rear == maxsize - 1;}
//判断队列是否为空,头指针和尾指针指向同一个位置时。
public boolean IsEmpty(){
return rear == front;
}
public void addQueue(int n){
if (IsFull()){
System.out.println("队列满");
return;
}
rear++;
arr[rear] = n;
}
public int getQueue(){
if (IsEmpty()){
throw new RuntimeException("队列空");
}
front++;
return arr[front];
}
public int getQueue(int i){
if (i > maxsize || i < 0){
throw new RuntimeException("输入的值错误");
}
if (IsEmpty()){
throw new RuntimeException("队列空");
}
front++;
return arr[i];
}
public void show(){
if (IsEmpty()){
throw new RuntimeException("队列空");
}
//遍历
for (int i = 0; i < arr.length; i++){
System.out.printf("arr[%d] = %d\n",i,arr[i]);
}
}
public void show(int n){
if (n > maxsize || n < 0){
throw new RuntimeException("输入的值错误");
}
if (IsEmpty()){
throw new RuntimeException("队列空");
}
System.out.printf("arr[%d] = %d\n",n,arr[n]);
}
}
数组模拟环形队列:
maxsize代表最大容量
front代表队列头的元素,初始值为0
rear代表队列尾的元素的后一个元素,代表一个空间,初始值同样为0
队列满时:(rear+1)% maxsize = front (因为环形队列首尾相连,最后一个元素的后一个元素就是第一个元素)
队列有效数据个数:(rear + maxsize - front) % maxsize;
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
//这里设置4,其有效内存是3,因为最后一个是要空出来的
CircleArrayQueue queue = new CircleArrayQueue(4);
Scanner scanner = new Scanner(System.in);
char key = ' ';
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):查看队列头的数据");
System.out.println("\n请输入指令:");
key = scanner.next().charAt(0);//接收一个字符(0就是一个)
switch(key) {
case's':
queue.show();
break;
case'a':
System.out.println("请输入需要添加的数");
int value = scanner.nextInt();
queue.addQueue(value);
break;
case'g':
try {
int res = queue.getQueue();//上抛了异常,所以要处理异常
System.out.printf("取出的数据是%d\n",res);
}catch(Exception e) {
System.out.println(e.getMessage());
}
break;
case'e':
scanner.close();//关闭控制台
loop = false;
break;
}
}
System.out.println("程序退出成功!");
}
}
class CircleArrayQueue{
//最大容量
private int maxsize;
//头指针
private int front;
//尾指针
private int rear;
//存放数据
private int[] arr;
public CircleArrayQueue(int maxsize){
this.maxsize = maxsize;
arr = new int[this.maxsize];
//front含义为指向队列的第一个元素
front = 0;
//rear指向队列的最后一个元素的后一位,首尾相连接。
rear = 0;
}
//判断队列是否满 rear == maxsize -1 时。
public boolean IsFull(){ return rear == maxsize -1;}
//判断队列是否为空,头指针和尾指针指向同一个位置时。
public boolean IsEmpty(){
return rear == 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("队列空");
}
//front指向队列第一个元素
int value = arr[front];
front = (front + 1) % maxsize;
return value;
}
public int getQueue(int i){
if (i > maxsize || i < 0){
throw new RuntimeException("输入的值错误");
}
if (IsEmpty()){
throw new RuntimeException("队列空");
}
int value = arr[front];
front = (front + 1) % maxsize;
return value;
}
public void show(){
if (IsEmpty()){
throw new RuntimeException("队列空");
}
for (int i = front; i < front + size(); i++) {
System.out.printf("arr[%d] = %d\n",i % maxsize,arr[i]);
}
}
public int size(){
return (rear + maxsize - front) % maxsize;
}
public void show(int n){
if (n > maxsize || n < 0){
throw new RuntimeException("输入的值错误");
}
if (IsEmpty()){
throw new RuntimeException("队列空");
}
System.out.printf("arr[%d] = %d\n",n,arr[n]);
}
}