1)队列的定义与实现形式-方式
2)队列实现思路(非环形,下面进行优化)
3)代码实现(注意并不是环形)
package DataStructures.queue;import java.util.Scanner;
public class ArrayQueueDemo {public static void main(String []args){ArrayQueue arrayQueue = new ArrayQueue(3);char key = ' '; Scanner scanner = new Scanner(System.in);boolean loop = true;while(loop){System.out.println("s(show):显示队列");System.out.println("e(exit):退出程序");System.out.println("a(add):添加数据到队列");System.out.println("g(get):从队列取出数据");System.out.println("h(head):查看队列头的数据");key = scanner.next().charAt(0); System.out.println("=========================");switch (key){case 's':arrayQueue.showQueue();break;case 'a':System.out.println("输入一个数");int value = scanner.nextInt();arrayQueue.addQueue(value);break;case 'g': try {System.out.println("取出的数是:" + arrayQueue.getQueue());}catch (Exception e){System.out.println(e.getMessage());}break;case 'h':try{System.out.println("队列头的数据是:"+arrayQueue.headQueue());}catch (Exception e){System.out.println(e.getMessage());}break;case 'e'://退出scanner.close();loop = false;break;default:break;}}System.out.println("程序退出~~~~~");}
}
class ArrayQueue{private int maxSize; private int front; private int rear; private int[] arr; public ArrayQueue(int arrMaxSize){maxSize = arrMaxSize;arr = new int[maxSize];front = -1; rear = -1; }public boolean isFull(){return rear == maxSize - 1;}public boolean isEmpty(){return rear == front;}public void addQueue(int n){if (isFull()){System.out.println("队列已满,不能加入");return;}rear++; arr[rear] = n;System.out.println("添加成功");}public int getQueue(){if (isEmpty()){throw new RuntimeException("队列为空,不能取数据");}front ++;return arr[front];}public void showQueue(){if (isEmpty()){System.out.println("队列为空");return;}for (int i = 0; i < maxSize;i++){System.out.println("arr["+i+"] = "+arr[i]);}}public int headQueue(){if (isEmpty()){throw new RuntimeException("队列为空");}return arr[front+1];}
}
上面代码实现完成后运行,会发现添加输入后取出,取出的位置不能再进行添加了,也就是添加完取出就不能再用了,没达到复用的效果
4)所以下面进行优化(改进成环形数组,取模来实现)
ps:这里最后有效数据个数,把他啊想象成一个环形,有可能rear在前有可能front在前,括号里加一个maxSize是防止rear在front后面形成负数,然后取模。
package DataStructures.queue;import java.util.Scanner;public class CircleArrayQueueDemo {public static void main(String []args){CircleArrayQueue arrayQueue = new CircleArrayQueue(4);System.out.println("测试数组模拟环形队列");char key = ' '; Scanner scanner = new Scanner(System.in);boolean loop = true;while(loop){System.out.println("s(show):显示队列");System.out.println("e(exit):退出程序");System.out.println("a(add):添加数据到队列");System.out.println("g(get):从队列取出数据");System.out.println("h(head):查看队列头的数据");key = scanner.next().charAt(0); System.out.println("=========================");switch (key){case 's':arrayQueue.showQueue();break;case 'a':System.out.println("输入一个数");int value = scanner.nextInt();arrayQueue.addQueue(value);break;case 'g': try {System.out.println("取出的数是:" + arrayQueue.getQueue());}catch (Exception e){System.out.println(e.getMessage());}break;case 'h':try{System.out.println("队列头的数据是:"+arrayQueue.headQueue());}catch (Exception e){System.out.println(e.getMessage());}break;case 'e'://退出scanner.close();loop = false;break;default:break;}}System.out.println("程序退出~~~~~");}
}class CircleArrayQueue{private int maxSize; private int front;private int rear; private int[] arr; public CircleArrayQueue(int arrMaxSize){maxSize = arrMaxSize; arr = new int[maxSize];front = 0;rear = 0; }public boolean isFull(){return (rear+1)%maxSize == front;}public boolean isEmpty(){return rear == front;}public void addQueue(int n){if (isFull()){System.out.println("队列已满,不能加入");return;}arr[rear] = n;rear = (rear+1) % maxSize;System.out.println("添加成功");}public int getQueue(){if (isEmpty()){throw new RuntimeException("队列为空,不能取数据");}int value = arr[front];front = (front+1) % maxSize;return value;}public void showQueue(){if (isEmpty()){System.out.println("队列为空");return;}for (int i = front; i < front + size();i++){System.out.println("arr["+ i%maxSize +"] = "+arr[i%maxSize]);}}public int size(){return (rear + maxSize - front) % maxSize;}public int headQueue(){if (isEmpty()){throw new RuntimeException("队列为空");}return arr[front];}
}