public class ArrayQueueDemo {
// 队列最大容量
private static final int maxSize = 5;
public static void main(String[] args) throws Exception {
// 创建队列
ArrayQueue aQueue = new ArrayQueue(5);
boolean loop = true;// 用来判断是否退出
Scanner scanner = new Scanner(System.in);
char key = ' ';// 判断用户输入的指令
while (loop) {
System.out.println("s(show)展示队列全部数据");
System.out.println("g(get)获取队列数据");
System.out.println("a(add)添加队列数据");
System.out.println("h(head)展示队列头数据");
System.out.println("e(exit)退出程序");
key = scanner.next().charAt(0);
switch (key) {
case 's':
aQueue.getAllQueue();
break;
case 'g':
int data = aQueue.getQueue();
System.out.printf("%d出列",data);
System.out.println();
break;
case 'a':
int d = 0;
System.out.println("请输入要入队的值:");
d = scanner.nextInt();
aQueue.add(d);
System.out.printf("%d入列",d);
System.out.println();
break;
case 'h':
int head = aQueue.getHead();
System.out.println("当前队列的头为:" + head);
break;
case 'e':
loop = false;
break;
default:
break;
}
}
}
public static int[] addQueue(int[] queue,int front,int rear,int data) throws Exception {
if (front == rear) {
System.out.println("这是一个空队列");
}
if (rear < queue.length - 1) {
queue[rear] = data;
System.out.printf("%d已入队",data);
System.out.println();
}else {
throw new Exception("队列满,无法入列");
}
return queue;
}
}
class ArrayQueue{
private int maxSize;// 队列最大容量
private int front;// 队列头
private int rear;// 队列尾
private int[] queue;// 队列
// 创建队列的构造器
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
this.queue = new int[maxSize];
this.front = -1;
this.rear = -1;
}
// 判断队列是否已满
public boolean isFull() {
return rear == maxSize-1;
}
// 判断队列是否为空
public boolean isEmpty() {
return front == rear;
}
// 添加数据到队列 入列
public void add(int n) throws Exception {
if (isFull()) {
System.out.println("队列已满,入不了队了兄弟!");
}
rear++;
queue[rear] = n;
}
// 获取队列数据 出列
public int getQueue() throws Exception {
if (isEmpty()) {
throw new NullPointerException("队列已空,没人了兄弟别移了!");
}
int data = queue[0];
for (int i = 0; i < queue.length - 1; i++) {
queue[i] = queue[i+1];
}
return data;
}
// 获取队列全部数据
public void getAllQueue() {
if (isEmpty()) {
System.out.println("队列空的,没人了兄弟!");
}else {
for (int i = 0;i < queue.length;i++) {
System.out.printf("%d\t",queue[i]);
}
System.out.println();
}
}
// 显示队列当前头数据
public int getHead() {
if (isEmpty()) {
throw new NullPointerException("队列空的,没头数据了兄弟!");
}
return queue[0];
}
}
环形队列
public class CircleArrayQueueDemo {
public static void main(String[] args) {
// 创建队列
CircleArrayQueue aQueue = new CircleArrayQueue(5);
boolean loop = true;// 用来判断是否退出
Scanner scanner = new Scanner(System.in);
char key = ' ';// 判断用户输入的指令
while (loop) {
System.out.println("s(show)展示队列全部数据");
System.out.println("g(get)获取队列数据");
System.out.println("a(add)添加队列数据");
System.out.println("h(head)展示队列头数据");
System.out.println("e(exit)退出程序");
key = scanner.next().charAt(0);
switch (key) {
case 's':
aQueue.getAllQueue();
break;
case 'g':
int data = 0;
try {
data = aQueue.getQueue();
System.out.printf("%d出列",data);
System.out.println();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
case 'a':
int d = 0;
System.out.println("请输入要入队的值:");
d = scanner.nextInt();
aQueue.add(d);
System.out.println();
break;
case 'h':
try {
int head = aQueue.getHead();
System.out.println("当前队列的头为:" + head);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case 'e':
loop = false;
break;
default:
break;
}
}
System.out.println("程序結束");
}
}
class CircleArrayQueue{
private int maxSize;// 队列最大容量
private int front;// 队列头: 指向队列头部第一个元素
private int rear;// 队列尾:指向队列尾部后一个元素
private int[] queue;// 队列
// 创建队列的构造器
public CircleArrayQueue(int arrMaxSize) {
maxSize = arrMaxSize;
queue = new int[maxSize];
}
// 判断队列是否已满
public boolean isFull() {
// maxSize = 3
// rear = 5
// front = 0
return (rear + 1) % maxSize == front;
}
// 判断队列是否为空
public boolean isEmpty() {
return rear == front;
}
// 添加数据到队列 入列
public void add(int n){
if (isFull()) {
System.out.println("队列已满,入不了队了兄弟!");
return;
}
queue[rear] = n;
System.out.printf("%d入列",n);
// 环形队列 需要考虑取模
rear = (rear + 1) % maxSize;
}
// 获取队列数据 出列
public int getQueue() throws Exception {
if (isEmpty()) {
throw new NullPointerException("队列已空,没人了兄弟别移了!");
}
int data = queue[front];
// 环形队列需要考虑取模
front = (front + 1) % maxSize;
return data;
}
// 获取队列全部数据
public void getAllQueue() {
if (isEmpty()) {
System.out.println("队列空的,没人了兄弟!");
return;
}
for(int i = front;i < front + size();i++) {
System.out.printf("arr[%d]=%d\n",i%maxSize,queue[i%maxSize]);
}
}
// 获取环形队列的有效长度
public int size() {
// maxSize = 3
// rear = 4
// front = 0
return (rear + maxSize - front) % maxSize;
}
// 显示队列当前头数据
public int getHead() {
if (isEmpty()) {
throw new NullPointerException("队列空的,没头数据了兄弟!");
}
return queue[front];
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)