当前位置: 首页 > news >正文

地名网站建设费用百度站长工具怎么关闭

地名网站建设费用,百度站长工具怎么关闭,怎么给新公司做网站,免费商城小程序队列 队列介绍 有序列表,可以用数组或者链表实现。遵循先进先出原则。 数组实现队列 public class ArrayQueue {public static void main(String[] args) {ArrayQueue queue new ArrayQueue(3);// 接收用户输入char key ;Scanner sc new Scanner(System.in);…

队列

队列介绍

有序列表,可以用数组或者链表实现。遵循先进先出原则。

数组实现队列

public class ArrayQueue {public static void main(String[] args) {ArrayQueue queue = new ArrayQueue(3);// 接收用户输入char key =' ';Scanner sc = 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 = sc.next().charAt(0);switch (key) {case 's':queue.showQueue();break;case 'a':System.out.println("输出一个数:");queue.addQueue(sc.nextInt());break;case 'g':try {int res = queue.getQueue();System.out.printf("取出的数据是:%d\n", res);} catch (Exception e) {System.out.printf(e.getMessage());}break;case 'h':try {int res = queue.head();System.out.printf("头数据是:%d\n", res);} catch (Exception e) {System.out.printf(e.getMessage());}break;case 'e':sc.close();loop = false;break;default:break;}System.out.println("程序退出~~");}}// 数组的最大容量private int maxSize;// 队头private int front;// 队尾private int rear;// 存放数据的数组private int[] arr;// 初始化队列public ArrayQueue(int maxSize) {this.maxSize = maxSize;this.arr = new int[this.maxSize];// 让front 指向队列头的前一个位置(队列头数据的前一个位置)this.front = -1;// 让rear 指向队尾的数据(队列的最后一个数据)this.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("队列满,不能加数据~");return;}rear++;arr[rear] = data;}public int getQueue() {if (isEmpty()) {throw new RuntimeException("队列空,无法取出数据~");}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 head() {if (isEmpty()) {throw new RuntimeException("队列空,没有头数据~");}return arr[front + 1];}
}

通过该方法测试
在这里插入图片描述在这里插入图片描述
添加了:10,20,30
show:arr[0]=10,arr[1]=20,arr[2]=30
在这里插入图片描述
取出10后,头数据是20。这时候show一下,可以看到队列为:arr[0]=10,arr[1]=20,arr[2]=30
在这里插入图片描述
也就是我们通过g获取到了数据,看到队列数据还是一样。

为了可以重新使用已经出队的数据所占用的空间,考虑使用环形队列。

数组环形队列

在这里插入图片描述

public class CircleArrayQueue {public static void main(String[] args) {CircleArrayQueue queue = new CircleArrayQueue(3);// 接收用户输入char key =' ';Scanner sc = 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 = sc.next().charAt(0);switch (key) {case 's':queue.showQueue();break;case 'a':System.out.println("输出一个数:");queue.addQueue(sc.nextInt());break;case 'g':try {int res = queue.getQueue();System.out.printf("取出的数据是:%d\n", res);} catch (Exception e) {System.out.printf(e.getMessage());}break;case 'h':try {int res = queue.head();System.out.printf("头数据是:%d\n", res);} catch (Exception e) {System.out.printf(e.getMessage());}break;case 'e':sc.close();loop = false;break;default:break;}System.out.println("程序退出~~");}}// 数组的最大容量private int maxSize;// 让front 指向队列的第一个元素// front = 0;// 让rear 指向队列的最后一个元素的后一个位置,因为希望空出一个空间作为约定// rear = 0;private int front;private int rear;// 存放数据的数组private int[] arr;// 初始化队列public CircleArrayQueue(int maxSize) {this.maxSize = maxSize;this.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("队列满,不能加数据~");return;}arr[rear] = data;// rear 后移,因为是环形,考虑取模rear = (rear + 1) % maxSize;}public int getQueue() {if (isEmpty()) {throw new RuntimeException("队列空,无法取出数据~");}// 因为front 是队列第一个元素,也会后移,考虑取模,以免越界// 1.先保存front 指向的值到valueint value = arr[front];// 2.front 后移,并返回valuefront = (front + 1) % maxSize;return value;}public void showQueue() {if (isEmpty()) {System.out.println("队列空,没有数据~");return;}// 因为是环形队列,需要考虑从front 开始遍历,遍历队列的有效数据个数for (int i = front; i < front + size(); i++) {System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);}}// 当前队列的有效数据个数public int size() {return (rear + maxSize - front) % maxSize;}public int head() {if (isEmpty()) {throw new RuntimeException("队列空,没有头数据~");}return arr[front];}
}

测试:
添加数据10,20,30,show一下,添加40提示队列满
在这里插入图片描述
在这里插入图片描述
取出arr[0] 后,show一下,可以看到队列还剩两个数据
在这里插入图片描述
这时,又可以在arr[0] 的位置插入新数据。
在这里插入图片描述
在这里插入图片描述
这样,就可以利用好了数组空间。

最近学习的数据结构与算法主要都是根据尚硅谷的学习:
https://www.bilibili.com/video/BV1E4411H73v/?p=18&vd_source=8e23c01ff3b88e176530ca2e0f4f18fe

可以看看我的个人博客:
网站:https://www.fuzm.wang / https://liwangc.gitee.io
—————————————————————————

作为初学者,很多知识都没有掌握,见谅,如有错误请指出,以期进步,感谢!。后续有新的学习,继续补充上来。

http://www.ritt.cn/news/10626.html

相关文章:

  • 企石网站仿做策划营销
  • 公司该建哪种网站做网站优化的公司
  • 做网站的office平台关键词查找工具
  • ps做网站原形什么平台可以发广告引流
  • 网站个别页面做seo西安做网站
  • 域名如何备案seo系统源码
  • 集团公司网站方案什么是百度快照
  • 做网站需要用什么技术网络推广方式
  • 网站模板找超速云建站app下载推广平台
  • 深圳网站开发建设服务公司网页设计用什么软件
  • linux服务器安装网站定制企业网站建设制作
  • 提供中小企业网站建设品牌网络营销成功案例
  • 重庆网站推广营销代理优化设计英语
  • 企业做网站有发展么只要做好关键词优化
  • 滕州做网站哪家好平台推广方式有哪些
  • 做5g网站空间容量要多少钱湖南发展最新消息公告
  • 阿里云账号密码发给网站开发百度竞价冷门产品
  • cms做静态网站什么网站都能进的浏览器
  • dz整站网站建设排名优化方法
  • wordpress 如何添加备案号seo深度优化公司
  • 锦州做网站的公司新网seo关键词优化教程
  • 上海建站宝盒网络营销课程培训机构
  • 石家庄网站设计制作seo人员招聘
  • 邢台网络公司做网站百度网盘app下载安装官方免费下载
  • 大型电子商务网站需要配服务器广东seo推广公司
  • 孝义做网站营销方式和营销策略
  • 佛山专业网站建设哪家好手机制作网页用什么软件
  • 做玩网站怎么上传搜狗权重查询
  • 网站建设公司效益怎么样网站推广关键词排名优化
  • 二维码付款跳转网页制作教程优化方案丛书官网