欢迎关注公众号:
Java数据结构和算法之队列
package Javalearn.dataStructure;
/*
* @project project
* @author liyongping
* @creed: just do it
* @ date 2021/12/19 10:50
* @ version 1.0
*/
import javax.naming.SizeLimitExceededException;
import java.util.Scanner;
class queueTest{
public static void main(String[] args) {
queue q=new queue(3);
char key='a';
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 'a':
System.out.println("请输入一个数");
int value=scanner.nextInt();
q.addQueue(value);
break;
case 's':
q.showQueue();
break;
case 'g':
try {
int res=q.getQueue();
System.out.printf("从队列取出数据 %d\n", res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = q.queueHead();
System.out.printf("头部的元素为 %d\n", res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop=false;
break;
default:
break;
}
}
}
}
class queue {
int font;//队列的头部位置
int rear;//对列指针
int maxSize;//队列的长度
int [] queue;
public queue(int size){
maxSize=size;
queue=new int[maxSize];
rear=-1;
font=-1;
}
//队列是否满了
public boolean isFull(){
return rear==maxSize-1;
}
//队列是否为空
public boolean isEmpty(){
return rear== font;
}
//增加数据
public void addQueue(int addNum) {
if (this.isFull()) {
System.out.println("队列已满");
return;
}
rear++;
queue[rear]=addNum;
}
//查
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("列表中没有数据,运行异常");
}
font++;
return queue[font];
}
//打印列表
public void showQueue(){
if(isEmpty()){
System.out.println("数组为空,没有数据");
}
for (int i = 0; i <queue.length ; i++) {
System.out.printf("queue[%d]=%d\n",i,queue[i]);
}
}
//长度
public int queueHead(){
if(isEmpty()){
System.out.println("数组为空,没有数据");
}
return queue[font];
}
}
上述代码运行结果如下:
Java环形队列
package Javalearn.dataStructure;/*
* @project project
* @author liyongping
* @creed: just do it
* @ date 2021/12/19 16:08
* @ version 1.0
*/
import javax.naming.SizeLimitExceededException;
import java.util.Scanner;
class CircleQueue{
public static void main(String[] args) {
CircleArray q=new CircleArray(4);
char key='a';
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 'a':
System.out.println("请输入一个数");
int value=scanner.nextInt();
q.addQueue(value);
break;
case 's':
q.showQueue();
break;
case 'g':
try {
int res=q.getQueue();
System.out.printf("从队列取出数据 %d\n", res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
int res = q.headQueue();
System.out.printf("头部的元素为 %d\n", res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
scanner.close();
loop=false;
break;
default:
break;
}
}
}
}
class CircleArray {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public CircleArray(int arrMaxSize) {
this.maxSize = arrMaxSize;
this.arr = new int[this.maxSize];
}
public boolean isFull() {
return (this.rear + 1) % this.maxSize == this.front;
}
public boolean isEmpty() {
return this.rear == this.front;
}
public void addQueue(int n) {
if (this.isFull()) {
System.out.println("队列满,不能加入数据~");
} else {
this.arr[this.rear] = n;
this.rear = (this.rear + 1) % this.maxSize;
}
}
public int getQueue() {
if (this.isEmpty()) {
throw new RuntimeException("队列空,不能取数据");
} else {
int value = this.arr[this.front];
this.front = (this.front + 1) % this.maxSize;
return value;
}
}
public void showQueue() {
if (this.isEmpty()) {
System.out.println("队列空的,没有数据~~");
} else {
for(int i = this.front; i < this.front + this.size(); ++i) {
System.out.printf("arr[%d]=%d\n", i % this.maxSize, this.arr[i % this.maxSize]);
}
}
}
public int size() {
return (this.rear + this.maxSize - this.front) % this.maxSize;
}
public int headQueue() {
if (this.isEmpty()) {
throw new RuntimeException("队列空的,没有数据~~");
} else {
return this.arr[this.front];
}
}
}
上述代码运行结果如下: