普通队列
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) {
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() {
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() {
return (rear + maxSize - front) % maxSize;
}
public int getHead() {
if (isEmpty()) {
throw new NullPointerException("队列空的,没头数据了兄弟!");
}
return queue[front];
}
}