队列 数组实现
package one.queue;
import java.util.Scanner;
public class ArrayQueueDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayQueue queue = new ArrayQueue(3);
while (true) {
System.out.print("s(showQueue):显示队列数据\t");
System.out.print("a(addQueue):添加元素\t");
System.out.print("m(isFull):队列满了?\t");
System.out.print("k(isEmpty):队列空的?\t");
System.out.print("h(headQueue):队列头数据 \t");
System.out.println("g(getQueue):数据出队列");
String key = scanner.next();
switch (key) {
case "s":
queue.showQueue();
break;
case "a":
int i = scanner.nextInt();
queue.addQueue(i);
break;
case "m":
System.out.println(queue.isFull());
break;
case "k":
System.out.println(queue.isEmpty());
break;
case "h":
System.out.println(queue.headQueue());
break;
case "g":
try {
System.out.println(queue.getQueue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
default:
System.out.println("????????????");
scanner.close();
return;
}
}
}
}
class ArrayQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public ArrayQueue(int arrMaxSize) {
this.maxSize = arrMaxSize;
this.front = -1;
this.rear = -1;
this.arr = new int[arrMaxSize];
}
public boolean isFull() {
return rear == maxSize - 1;
}
public boolean isEmpty() {
return rear == front;
}
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列满了,不能加入" + n);
return;
}
rear++;
arr[rear] = n;
}
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空。。。");
}
front++;
return arr[front];
}
public void showQueue() {
for (int i = front + 1; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public int headQueue() {
return arr[front + 1];
}
}
队列 环形数组实现
package one.queue;
import java.util.Scanner;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CircleArrayQueue queue = new CircleArrayQueue(4);
while (true) {
System.out.print("s(showQueue):显示队列数据\t");
System.out.print("a(addQueue):添加元素\t");
System.out.print("m(isFull):队列满了?\t");
System.out.print("k(isEmpty):队列空的?\t");
System.out.print("h(headQueue):队列头数据 \t");
System.out.println("g(getQueue):数据出队列");
String key = scanner.next();
switch (key) {
case "s":
queue.showQueue();
break;
case "a":
int i = scanner.nextInt();
queue.addQueue(i);
break;
case "m":
System.out.println(queue.isFull());
break;
case "k":
System.out.println(queue.isEmpty());
break;
case "h":
System.out.println(queue.headQueue());
break;
case "g":
try {
System.out.println(queue.getQueue());
} catch (Exception e) {
System.err.println(e.getMessage());
}
break;
default:
System.out.println("????????????");
scanner.close();
return;
}
}
}
}
class CircleArrayQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public CircleArrayQueue(int arrMaxSize) {
this.maxSize = arrMaxSize;
this.arr = new int[arrMaxSize];
}
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
public boolean isEmpty() {
return rear == front;
}
public void addQueue(int n) {
if (isFull()) {
System.out.println("队列满了,不能加入" + n);
return;
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空。。。");
}
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
public void showQueue() {
for (int i = front; i < front + (rear + maxSize - front) % maxSize; i++) {
System.out.println("arr[" + i % maxSize + "] = " + arr[i % maxSize]);
}
System.out.println();
}
public int headQueue() {
return arr[front];
}
}