05-环形队列

实现思路
1,调整front指向队列的第一个元素,front初始值=0
2,调整rear指向队列的最后一个元素的后一个位置,希望空出一个空间作为约定,rear的初始值=0
3,队满,条件: (rear+1) % maxSize = front ,则队满,队列最多可存 maxSize-1个数
4,队空,条件:rear == front空
5,队列中有效的数据的个数 (rear-front+maxSize) % maxSize
 
代码实现
  1 package com.datastack.datastack.queue;
  2 
  3 import java.util.Scanner;
  4 
  5 /*
  6  * 环形队列(数组实现)
  7  */
  8 public class CircleQueue {
  9     private int maxSize;//队列最大值
 10     private int front;//队首,指向队列首的前一个位置
 11     private int rear;//队尾,指向队列尾的序号
 12     private int[] arr;//存放队列数据的数组
 13     
 14     /**
 15      * 创建队列
 16      * @param maxSize
 17      */
 18     public CircleQueue(int maxSize){
 19         this.maxSize = maxSize;
 20         this.arr = new int[maxSize];
 21         this.front = 0;
 22         this.rear = 0;
 23     }
 24     
 25     /**
 26      * 判断队列是否已满
 27      * @return
 28      */
 29     public boolean isFull(){
 30         return (rear+1) % maxSize == front;
 31     }
 32     
 33     /**
 34      * 判断队列是否为空
 35      * @param args
 36      */
 37     public boolean isEmpty(){
 38         return rear == front;
 39     }
 40     
 41     /**
 42      * 添加数据到队列
 43      * @param args
 44      */
 45     public void addQueue(int n){
 46         //判断队列是否满
 47         if(isFull()){
 48             System.out.println("队列已满,不能加入数据。");
 49             return;
 50         }
 51         //直接将数据加入
 52         arr[rear] = n;
 53         //将rear后移,需考虑取模
 54         rear = (rear+1) % maxSize;
 55     }
 56     
 57     /**
 58      * 出队列
 59      * @param args
 60      */
 61     public int getQueue(){
 62         //判断队列是否为空
 63         if(isEmpty()){
 64             //通过抛出异常
 65             throw new RuntimeException("队列空,不能取数据");
 66         }
 67         //需要分析front是指向队列的第一个元素
 68         //1,先把front的值保存到变量
 69         //2,将front后移,去摸
 70         int res = this.arr[front];
 71         front = (front+1) % maxSize;
 72         return res;
 73     }
 74     
 75     /**
 76      * 显示队列数据
 77      * @param args
 78      */
 79     public void showQueque(){
 80         if(isEmpty()){
 81             System.out.println("队列为空。");
 82             return;
 83         }
 84         //从front开始遍历
 85         for(int i=front;i<front+size();i++){
 86             System.out.printf("arr[%d]=%d\t",i % maxSize,arr[i % maxSize]);
 87         }
 88     }
 89     
 90     /**
 91      * 求出当前队列有效个数
 92      */
 93     public int size(){
 94         return (rear-front+maxSize) % maxSize;
 95     }
 96     
 97     /**
 98      * 显示队头
 99      * @param args
100      */
101     public int headQueue(){
102         if(isEmpty()){
103             throw new RuntimeException("队列为空。");
104         }
105         return this.arr[front];
106     }
107     
108     public static void main(String[] args) {
109         //创建一个队列
110         CircleQueue arrQueue = new CircleQueue(3);
111         char key = ' ';//接收用户输入
112         Scanner scanner = new Scanner(System.in);
113         boolean loop = true;
114         while(loop){
115             System.out.println("s(show):显示队列");
116             System.out.println("e(exit):退出程序");
117             System.out.println("a(add):添加数据到队列");
118             System.out.println("g(get):从队列取出数据");
119             System.out.println("h(head):查看队列头的数据");
120             key = scanner.next().charAt(0);
121             switch (key) {
122             case 's'://显示队列值
123                 arrQueue.showQueque();
124                 break;
125             case 'a'://入队
126                 System.out.println("请输入一个数");
127                 int value = scanner.nextInt();
128                 arrQueue.addQueue(value);
129                 break;
130             case 'g'://出队
131                 try {
132                     int res = arrQueue.getQueue();
133                     System.out.println(res);
134                 } catch (Exception e) {
135                     System.out.println(e.getMessage());
136                 }
137                 break;
138             case 'h'://打印对首
139                 try {
140                     int res = arrQueue.headQueue();
141                     System.out.println(res);
142                 } catch (Exception e) {
143                     System.out.println(e.getMessage());
144                 }
145                 break;
146             case 'e'://退出程序
147                 scanner.close();
148                 loop = false;
149                 break;
150 
151             default:
152                 break;
153             }
154         }
155         System.out.println("程序退出");
156         
157     }
158 }

猜你喜欢

转载自www.cnblogs.com/linux777/p/11522662.html
今日推荐