码迷,mamicode.com
首页 > 编程语言 > 详细

java 数据结构 用数组实现队列

时间:2019-10-05 10:18:59      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:com   closed   ann   图片   tin   hide   util   显示   for   

技术图片

 

 

 

代码内容

技术图片
  1 package com.structure;
  2 
  3 import java.util.Scanner;
  4 
  5 /**
  6  * @auther::9527
  7  * @Description: 数组模拟队列
  8  * @program: jstl2
  9  * @create: 2019-10-05 08:58
 10  */
 11 public class ArrayQueueDemo {
 12     public static void main(String[] args) {
 13         Scanner scanner = new Scanner(System.in);
 14         //测试
 15         ArrayQueue queue = new ArrayQueue(3);
 16         char key = ‘ ‘; //接受用户输入
 17         boolean loop = true;  //循环终止判断器
 18         while (loop) {
 19             System.out.println("s(show):显示队列");
 20             System.out.println("(e(exit)):退出程序");
 21             System.out.println("a(add):添加数据到队列");
 22             System.out.println("g(get):从队列取出数据");
 23             System.out.println("h(head):查看队列头部的数据");
 24             System.out.println("按提示输入......");
 25             key = scanner.next().charAt(0);  //接收数据
 26             switch (key) {
 27                 case ‘s‘:
 28                     queue.showQueue();
 29                     break;
 30                 case ‘a‘:
 31                     System.out.println("请输入一个数");
 32                     int value = scanner.nextInt();
 33                     queue.addQueue(value);
 34                     break;
 35                 case ‘g‘:
 36                     try {
 37                         int res = queue.getQueue();
 38                         System.out.printf("取出的数是%d\n", res);
 39                     } catch (Exception e) {
 40                         System.out.println(e.getMessage());
 41                     }
 42                     break;
 43                 case ‘h‘:
 44                     try {
 45                         int res = queue.headQueue();
 46                         System.out.printf("队列头部的数是%d\n", res);
 47                     } catch (Exception e) {
 48                         System.out.println(e.getMessage());
 49                     }
 50                     break;
 51                 case ‘e‘:
 52                     scanner.close();
 53                     loop = false;
 54                     break;
 55                 default:
 56                     System.out.println("...输入不合法,请重新输入...");
 57                     break;
 58             }
 59         }
 60         System.out.println("程序退出....");
 61     }
 62 }
 63 
 64 //使用数组模拟队列--编写一个ArrayQueue
 65 class ArrayQueue {
 66     private int maxSize; //数组的最大容量
 67     private int front;  //队列头
 68     private int rear; //队列尾
 69     private int[] arr; //存放数据的数组,模拟队列.
 70 
 71     //创建队列的构造器
 72     public ArrayQueue(int arrMaxSize) {
 73         maxSize = arrMaxSize;
 74         arr = new int[maxSize];
 75         front = -1;
 76         rear = -1;
 77     }
 78 
 79     //判断队列是否已经满了
 80     public boolean isFull() {
 81         return rear == maxSize - 1;
 82     }
 83 
 84     //判断队列是否为空
 85     public boolean isEmpty() {
 86         return rear == front;
 87     }
 88 
 89     //添加数据到队列
 90     public void addQueue(int n) {
 91         //判断队列是否已满
 92         if (isFull()) {
 93             System.out.println("队列已满,不能添加");
 94             return;
 95         }
 96         rear++; //指针后移
 97         arr[rear] = n;
 98     }
 99 
100     //数据出队列
101     public int getQueue() {
102         //判断队列是否为空
103         if (isEmpty()) {
104             throw new RuntimeException("队列为空,不能取数据");
105         }
106         front++; //头部指针后移
107         return arr[front];
108     }
109 
110     //显示队列的所有数据
111     public void showQueue() {
112         if (isEmpty()) {
113             System.out.println("队列为空,没有数据");
114             return;
115         }
116         for (int i = 0; i < arr.length; i++) {
117             System.out.printf("arr[%d]=%d\n", i, arr[i]);
118         }
119     }
120 
121     //显示队列的头部数据,这个不是取出数据
122     public int headQueue() {
123         //判断是否为空
124         if (isEmpty()) {
125             System.out.println("队列为空,没有数据....");
126             throw new RuntimeException("队列为空,没有数据....");
127         }
128         return arr[front + 1];
129     }
130 }
ArrayQueue---用数组实现队列

 

java 数据结构 用数组实现队列

标签:com   closed   ann   图片   tin   hide   util   显示   for   

原文地址:https://www.cnblogs.com/twuxian/p/11624001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!