赞
踩
博文主要是自己学习的笔记,供自己以后复习使用,
参考的主要教程是B站的
尚硅谷数据结构和算法
1)队列是一个有序列表,可以用数组或者链表来实现
2)遵循先进先出的原则:先存入队列的数据,要先取出。
用数组模拟队列的示意图:
初始化:rear=front=-1,都指向队列的前一个元素
入队:rear++
出队:front++
判空:rear==front
判满:rear = maxSize - 1
class ArrayQueue { private int maxSize;//最大容量 private int front;//队列头 private int rear;//队列尾 private int[] arr;//存放数组 //创建队列的构造器 public ArrayQueue(int maxSize) { this.maxSize = maxSize; 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 data) { if (isFull()) { System.out.println("队列已满"); } else { rear++; arr[rear] = data; } } //获取队列的数据,出队列 public int getQueue() { if (isEmpty()) { //通过抛出异常 throw new RuntimeException("队列为空"); } else { 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]; } }
测试代码
public class ArrayQueueDemo { public static void main(String[] args) { //测试 ArrayQueue arrayQueue = new ArrayQueue(3); char key = ' '; Scanner scanner = new Scanner(System.in); boolean loop = true; while (loop) { System.out.println("s(show): 显示队列"); System.out.println("e(exit): 退出程序"); System.out.println("a(add): 添加数据到队列"); System.out.println("g(get): 从队列取数据"); System.out.println("h(head): 查看队列头的数据"); key = scanner.next().charAt(0); //接受一个字符 switch (key) { case 's': arrayQueue.showQueue(); break; case 'a': System.out.println("请输入数据"); int data = scanner.nextInt(); arrayQueue.addQueue(data); break; case 'g': try { int res = arrayQueue.getQueue(); System.out.printf("取出的数据是%d\n", res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = arrayQueue.headQueue(); System.out.printf("队列头的数据是%d\n", res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'e': scanner.close(); loop = false; break; default: break; } } } }
上述代码存在问题:
队列不管是存还是取都是++操作,因此上述队列只能用一次
**优化手段:**改为环形的队列,利用取模的操作实现。
初始化:rear=front=0,front指向当前元素,rear指向当前元素的后一个位置,空出一个空间作为约定
入队:rear = (rear+1)%maxSize
出队:front= (front+1)%maxSize
判空:rear==front
判满:(rear+1)%maxSize = front
有效数据个数(rear - front + maxSize) % maxSize
JAVA代码实现
class CircleArray { private int maxSize; private int front; private int rear; private int[] arr; public CircleArray(int maxSize) { this.maxSize = maxSize; rear = 0; front = 0; arr = new int[maxSize]; } //判断队列是否已满 public boolean isFull() { return (rear + 1) % maxSize == front; } //判断队列是否为空 public boolean isEmpty() { return rear == front; } //添加数据到队列 public void addQueue(int data) { if (isFull()) { System.out.println("队列已满"); } else { arr[rear] = data; rear = (rear + 1) % maxSize; } } //获取队列的数据,出队列 public int getQueue() { if (isEmpty()) { //通过抛出异常 throw new RuntimeException("队列为空"); } else { int temp = arr[front]; front = (front + 1) % maxSize; return temp; } } //显示队列的所有数据 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 headQueue() { if (isEmpty()) { throw new RuntimeException("队列为空~~~"); } return arr[front]; } //显示队列的头数据,注意不是取数据 public int size() { return (rear - front + maxSize) % maxSize; } }
测试代码
public class CircleArrayQueueDemo { public static void main(String[] args) { //测试 CircleArray arrayQueue = new CircleArray(4); char key = ' '; Scanner scanner = new Scanner(System.in); boolean loop = true; while (loop) { System.out.println("s(show): 显示队列"); System.out.println("e(exit): 退出程序"); System.out.println("a(add): 添加数据到队列"); System.out.println("g(get): 从队列取数据"); System.out.println("h(head): 查看队列头的数据"); key = scanner.next().charAt(0); //接受一个字符 switch (key) { case 's': arrayQueue.showQueue(); break; case 'a': System.out.println("请输入数据"); int data = scanner.nextInt(); arrayQueue.addQueue(data); break; case 'g': try { int res = arrayQueue.getQueue(); System.out.printf("取出的数据是%d\n", res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'h': try { int res = arrayQueue.headQueue(); System.out.printf("队列头的数据是%d\n", res); } catch (Exception e) { System.out.println(e.getMessage()); } break; case 'e': scanner.close(); loop = false; break; default: break; } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。