队列queue
队列是一个 有序列表,可以用 数组或链表来实现,遵循 先入先出 的原则。
1. 单向队列
public boolean isFull();
判断队列是否满
public boolean isEmpty();
判断队列是否为空
public void addQueue(int n);
添加数据到队列
public int getQueue();
从队列中删除数据
public void showQueue();
显示队列的所有数据
public int headQueue();
显示队列的头数据,只是显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| public 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; } 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 < arr.length; i++) { System.out.printf("arr[%d]=%d\n",i, arr[i]); } } public int headQueue() { if(isEmpty()) { throw new RuntimeException("队列为空,没有数据"); } return arr[front+1]; } }
|
1.1 测试单向队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class ArrayQueueDemo {
public static void main(String[] args) { ArrayQueue queue = new ArrayQueue(4); queue.addQueue(1); queue.addQueue(2); queue.addQueue(3); queue.addQueue(4);
queue.showQueue(); int front = queue.getQueue(); System.out.println("取出的数据:"+front); int head = queue.headQueue(); System.out.println("head的数据:"+head); }
}
|
2. 环形队列
front
指向队列的第一个元素 默认初始值 front
= 0
rear
指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定 默认初始值 rear
= 0
public boolean isFull();
判断队列是否满
(rear+1)%maxSize == front
public boolean isEmpty();
判断队列是否为空
public void addQueue(int n);
添加数据到队列
rear=(rear+1)%maxSize;
后移一位
public int getQueue();
从队列中删除数据
public void showQueue();
显示队列的所有数据
public int headQueue();
显示队列的头数据,只是显示
public int size();
得到队列中有效数据的个数
(rear-front+maxSize)%maxSize
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| public class CircleArrayQueue{ private int maxSize; private int front; private int rear; private int[] arr; public CircleArrayQueue(int arrMaxSize) { maxSize = arrMaxSize; arr = new int[maxSize]; } public boolean isFull() { return (rear+1)%maxSize == front; } public boolean isEmpty() { return rear == front; } public void addQueue(int num) { if(isFull()) { System.out.println("队列满不能添加数据!"); return; } arr[rear]=num; rear=(rear+1)%maxSize; } public int getQueue() { if(isEmpty()) { throw new RuntimeException("队列空不能取数据!"); }
int result = arr[front]; front = (front+1)%maxSize; return result; } public void showQueue() { if(isEmpty()) { System.out.println("队列为空!"); return; } 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-front+maxSize)%maxSize; } public int headQueue() { if(isEmpty()) { throw new RuntimeException("队列为空!没有head数据"); } return arr[front]; } }
|
2.1 测试循环队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| public class CircleArrayQueueDemo {
public static void main(String[] args) { System.out.println("测试数据模拟循环队列!"); CircleArrayQueue queue = new CircleArrayQueue(4); Scanner scanner = new Scanner(System.in); boolean flag =true; while(flag) {
String key = scanner.nextLine(); switch (key) { case "s": queue.showQueue(); break; case "a": System.out.println("请输入一个数"); int value = scanner.nextInt(); queue.addQueue(value); break; case "g": try { int result = queue.getQueue(); System.out.printf("取出的数据是:%d\n",result); } catch (Exception e) { System.out.println(e.getStackTrace()); } break; case "e": scanner.close(); flag=false; break; default: break; } } System.out.println("测试程序退出!"); }
}
|