700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > php环行队列实现 java数组实现队列及环形队列实现过程解析

php环行队列实现 java数组实现队列及环形队列实现过程解析

时间:2022-12-18 00:11:12

相关推荐

php环行队列实现 java数组实现队列及环形队列实现过程解析

这篇文章主要介绍了java数组实现队列及环形队列实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码内容

ArrayQueue---用数组实现队列

package com.structure;

import java.util.Scanner;

/**

* @auther::9527

* @Description: 数组模拟队列

* @program: jstl2

* @create: -10-05 08:58

*/

public class ArrayQueueDemo {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

//测试

ArrayQueue queue = new ArrayQueue(3);

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("按提示输入......");

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.printf("取出的数是%d\n", res);

} catch (Exception e) {

System.out.println(e.getMessage());

}

break;

case 'h':

try {

int res = queue.headQueue();

System.out.printf("队列头部的数是%d\n", res);

} catch (Exception e) {

System.out.println(e.getMessage());

}

break;

case 'e':

scanner.close();

loop = false;

break;

default:

System.out.println("...输入不合法,请重新输入...");

break;

}

}

System.out.println("程序退出....");

}

}

//使用数组模拟队列--编写一个ArrayQueue

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;

rear = -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 void showQueue() {

if (isEmpty()) {

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 (isEmpty()) {

System.out.println("队列为空,没有数据....");

throw new RuntimeException("队列为空,没有数据....");

}

return arr[front + 1];

}

}

改进版---环形队列:

环形队列代码

package com.structure;

import java.util.Scanner;

/**

* @auther::9527

* @Description: 环形队列

* @program: jstl2

* @create: -10-05 09:53

*/

public class CircleArrayQueueDemo {

public static void main(String[] args) {

//创建一个环形队列 这里输入最大数是4,其实有效的是3

CircleArray queue = 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':

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.printf("取出的数据是%d\n", res);

} catch (Exception e) {

// TODO: handle exception

System.out.println(e.getMessage());

}

break;

case 'h': // 查看队列头的数据

try {

int res = queue.headQueue();

System.out.printf("队列头的数据是%d\n", res);

} catch (Exception e) {

// TODO: handle exception

System.out.println(e.getMessage());

}

break;

case 'e': // 退出

scanner.close();

loop = false;

break;

default:

break;

}

}

System.out.println("程序退出~~");

}

}

class CircleArray {

private int maxSize; //数组的最大容量

//front 变量调整:front就指向队列的第一个元素,即:arr[front]=arr[0]

private int front;

//rear 变量调整:rear 初始值为0 指向队列的最后一个元素的位置,因为需要空出一个位置作约束

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 isEmpty() {

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 (isEmpty()) {

throw new RuntimeException("队列为空,不能取数据");

}

//由于front是指向队列的第一个元素,所以需要

// 1、front的值先保存到临时变量

//2、将front后移一位,通过取模实现环形后移,

// 3、将临时变量返回

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.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);

}

}

//求出当前队列有效数据的个数

public int size() {

return (rear + maxSize - front) % maxSize;

}

//显示队列的头部数据

public int headQueue(){

//判断是否为空

if (isEmpty()){

throw new RuntimeException("队列为空,没有数据");

}

return arr[front];

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。