一、普通队列
class ArrayQueue{
private int maxNums;
private int front;
private int rear;
private int[] arr;
public ArrayQueue(int maxNums) {
this.maxNums = maxNums;
this.front = 0;
this.rear = 0;
this.arr = new int[maxNums];
}
public boolean isEmpty() {
return this.front == this.rear;
}
public boolean isFull() {
return this.rear == this.maxNums;
}
public void addData(int n) {
if (isFull()) {
System.out.println("队列已经满了");
return;
}
this.arr[rear++] = n;
}
public int getData() {
if (isEmpty()) {
throw new RuntimeException("队列为空");
}
return this.arr[front++];
}
public void findAll() {
if (isEmpty()) {
System.out.println("队列为空");
return ;
}
for(int i=this.front; i<this.rear; i++) {
System.out.print(this.arr[i] + " ");
}
System.out.print("\n");
}
public static void main(String[] args) {
ArrayQueue queue = new ArrayQueue(10);
Scanner scanner = new Scanner(System.in);
boolean loop = true;
char ch = ' ';
while(loop) {
System.out.println("1:添加");
System.out.println("2:是否为空");
System.out.println("3:是否已满");
System.out.println("4:遍历");
System.out.println("5:取出");
ch = scanner.next().charAt(0);
if (ch == '1') {
int val = scanner.nextInt();
queue.addData(val);
} else if (ch == '2') {
System.out.println(queue.isEmpty());
} else if (ch == '3') {
System.out.println(queue.isFull());
} else if (ch == '4') {
queue.findAll();
} else if (ch == '5') {
System.out.println(queue.getData());
} else {
break;
}
}
System.out.println("程序结束");
}
}
二、环形队列
class CircleQueue{
private int maxNums;
private int front;
private int rear;
private int[] arr;
public CircleQueue(int maxNums) {
this.maxNums = maxNums;
this.front = 0;
this.rear = 0;
this.arr = new int[maxNums];
}
public boolean isEmpty() {
return this.front == this.rear;
}
public boolean isFull() {
return (this.rear+1)%this.maxNums == this.front;
}
public void addData(int n) {
if (isFull()) {
System.out.println("队列已经满了");
return;
}
this.arr[rear] = n;
this.rear = (this.rear+1)%maxNums;
}
public int getData() {
if (isEmpty()) {
throw new RuntimeException("队列为空");
}
int val = this.arr[this.front];
this.front = (this.front+1)%maxNums;
return val;
}
public void findAll() {
if (isEmpty()) {
System.out.println("队列为空");
return ;
}
for(int i=this.front; i<this.front + size(); i++) {
System.out.print(this.arr[i%maxNums] + " ");
}
System.out.print("\n");
}
public int size() {
return (this.rear+this.maxNums-this.front)%maxNums;
}
public static void main(String[] args) {
CircleQueue queue = new CircleQueue(10);
Scanner scanner = new Scanner(System.in);
boolean loop = true;
char ch = ' ';
while(loop) {
System.out.println("1:添加");
System.out.println("2:是否为空");
System.out.println("3:是否已满");
System.out.println("4:遍历");
System.out.println("5:取出");
ch = scanner.next().charAt(0);
if (ch == '1') {
int val = scanner.nextInt();
queue.addData(val);
} else if (ch == '2') {
System.out.println(queue.isEmpty());
} else if (ch == '3') {
System.out.println(queue.isFull());
} else if (ch == '4') {
queue.findAll();
} else if (ch == '5') {
System.out.println(queue.getData());
} else {
break;
}
}
System.out.println("程序结束");
}
}