数组实现队列
package com.fly.arrayQueue;
import java.util.Scanner;
public class ArrayQueue {
public static void main(String[] args) {
ArrayQueueDemo arrayQueueDemo = new ArrayQueueDemo(4);
char key=' ';
Scanner scanner = new Scanner(System.in);
boolean loop=true;
while (loop){
System.out.println("s:显示队列");
System.out.println("e:退出队列");
System.out.println("a:添加数据队列");
System.out.println("g:取出数据");
System.out.println("h:显示头");
key = scanner.next().charAt(0);
switch (key){
case 's':
arrayQueueDemo.showQueue();
break;
case 'a':
System.out.println("输入一个整数:");
int value = scanner.nextInt();
arrayQueueDemo.addQueue(value);
break;
case 'g':
try {
int res=arrayQueueDemo.getQueue();
System.out.println("取出的数据是:"+res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try {
System.out.println("队列头的数据:");
arrayQueueDemo.showHead();
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':
System.out.println("程序退出!");
scanner.close();
loop=false;
break;
default:
break;
}
}
}
static class ArrayQueueDemo{
public int maxSize;
public int front;
public int rear;
public int[] arr;
public ArrayQueueDemo(int arrMaxSize){
maxSize=arrMaxSize;
front=-1;
rear=-1;
arr=new int[arrMaxSize];
}
public boolean isFull(){
return rear==maxSize-1;
}
public boolean isEmpty(){
return front==rear;
}
public void addQueue(int n){
if (isFull()==true){
System.out.println("队列已满");
}
rear++;
arr[rear]=n;
}
public int getQueue(){
if (isEmpty()==true){
System.out.println("队列为空");
throw new RuntimeException("队列为空");
}
front++;
int res=arr[front];
arr[front]=0;
return res;
}
public void showQueue(){
if (isEmpty()){
System.out.println("队列为空,无法遍历");
}else {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+",");
}
System.out.println("");
}
}
public void showHead(){
if (isEmpty()==true){
System.out.println("队列为空");
throw new RuntimeException("队列为空");
}
System.out.println(arr[front+1]);
}
}
}
数组实现环形队列
package com.fly.arrayQueue;
public class CircleArrayQueueDemo {
public static void main(String[] args) {
CircleArrayQueue circleArrayQueue = new CircleArrayQueue(4);
circleArrayQueue.add(1);
circleArrayQueue.all();
circleArrayQueue.add(2);
circleArrayQueue.all();
circleArrayQueue.add(3);
System.out.println(circleArrayQueue.get());
System.out.println(circleArrayQueue.headQueue());
System.out.println(circleArrayQueue.lastQueue());
circleArrayQueue.all();
}
static class CircleArrayQueue {
private int MaxSize;
private int rear;
private int front;
private int[] arr;
public CircleArrayQueue(int maxSize) {
MaxSize = maxSize;
arr = new int[maxSize];
rear = 0;
front = 0;
}
public boolean isFull() {
return (rear + 1) % MaxSize == front;
}
public boolean isEmpty() {
return rear == front;
}
public void add(int n) {
if (isFull()) {
throw new RuntimeException("满啦");
}else {
arr[rear] = n;
rear = (rear + 1) % MaxSize;
System.out.println(rear+","+front);
}
}
public int get() {
if (isEmpty()) {
throw new RuntimeException("The Queue is Empty!");
}
int frontBottle = arr[front];
front = (front + 1) % MaxSize;
return frontBottle;
}
public void all() {
if (isEmpty()) {
throw new RuntimeException("The Queue is Empty!");
}
for (int i = front; i < (front +size()); i++) {
System.out.printf("arr[%d]:%d\n", i%MaxSize, arr[i%MaxSize]);
}
}
public int size(){
return (rear+MaxSize-front)%MaxSize;
}
public int headQueue() {
return arr[front];
}
public int lastQueue() {
return arr[rear-1];
}
}
}