一,数组模拟队列
规定头指针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 }
原文:https://www.cnblogs.com/steakliu/p/11418398.html