首页 > 其他 > 详细

数据结构之队列

时间:2019-08-27 15:57:41      阅读:75      评论:0      收藏:0      [点我收藏+]

一,数组模拟队列

  规定头指针front和尾指针rear都为-1,front=-1表示头指针指向队头的前一个位置,尾指针rear=-1表示指向队尾,这两句话要好好的理解,

  maxSize为队列的大小

  arr[ ]使用来存储数据的数组,大小由maxSize来定,

  判断队列是否为空:当队尾和队头合并则代表队列为空,rear == front

  判断队列是否已满:当队尾的索引等于队列长度-1,因为索引从0开始,所以队列的长度要-1才和队尾索引相等,rear == maxSize-1

    入队:先判断是否已满,不满的话尾指针后移一个位置(rear++),然后将元素放入数组

  出队:判断队列是否为空,不为空的话头指针后移(front++)

代码:

  

技术分享图片
  1 package Queue;
  2 
  3 import java.util.Scanner;
  4 
  5 public class ArrayQueue {
  6 
  7     public static void main(String[] args) {
  8         ArrayQueue arrayQueue = new ArrayQueue(6);
  9         Scanner scanner = new Scanner(System.in);
 10         char key = ‘ ‘;
 11         boolean loop = true;
 12         while (loop) {
 13             System.out.println("a 入队");
 14             System.out.println("r 出队");
 15             System.out.println("g 遍历队列");
 16             System.out.println("h 查看队头元素");
 17             key = scanner.next().charAt(0);//接收一个字符串
 18             switch (key) {
 19                 case ‘a‘:
 20                     System.out.println("输入一个数");
 21                     int value = scanner.nextInt();
 22                     arrayQueue.addQueue(value);
 23                     break;
 24                 case ‘r‘:
 25                     try {
 26                         int removeNum = arrayQueue.removeQueue();
 27                         System.out.println("取出的数据为:" + removeNum);
 28                     } catch (Exception e) {
 29                         System.out.println(e.getMessage());
 30                     }
 31                     break;
 32                 case ‘g‘:
 33                     try {
 34                         arrayQueue.getAllQueue();
 35                     } catch (Exception e) {
 36                         System.out.println(e.getMessage());
 37                     }
 38                     break;
 39                 case ‘h‘:
 40                     try {
 41                         int headNum = arrayQueue.headQueue();
 42                         System.out.println("队头为:"+headNum);
 43                     }catch (Exception e){
 44                         System.out.println(e.getMessage());
 45                     }
 46             }
 47         }
 48     }
 49 
 50     int maxSize;
 51     int rear;
 52     int front;
 53     int arr[];
 54 
 55     public ArrayQueue(int arrMaxSize){
 56         maxSize = arrMaxSize;
 57         rear = -1;
 58         front = -1;
 59         arr = new int[maxSize];
 60     }
 61 
 62     //判断队列是否为空
 63     public boolean isEmpty(){
 64         return rear == front;
 65     }
 66 
 67     //判断队列是否已满
 68     public boolean isFull(){
 69         return rear == maxSize-1;
 70     }
 71 
 72     //入队
 73     public void addQueue(int num){
 74         if (isFull()){
 75             System.out.println("队列满的");
 76             return;
 77         }
 78         rear++;
 79         arr[rear] = num;//入队
 80     }
 81 
 82     //出队(单个元素)
 83     public int removeQueue(){
 84         if (isEmpty()){
 85             throw new RuntimeException("队列没有元素");
 86         }
 87         front++;
 88         return arr[front];
 89     }
 90 
 91     //遍历队列所有元素
 92     public void getAllQueue(){
 93         if (isEmpty()){
 94             throw new RuntimeException("队列为空");
 95         }
 96         for (int i = 0 ; i <= arr.length ; i++){
 97             System.out.println("队列元素下标:"+i+"   "+"队列元素值:"+arr[i]);
 98         }
 99     }
100 
101     //查看队头元素
102     public int headQueue(){
103         if (isEmpty()){
104             throw new RuntimeException("队列为空");
105         }
106         return arr[front+1];
107     }
108 }
View Code

 

数据结构之队列

原文:https://www.cnblogs.com/steakliu/p/11418398.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!