当前位置: 首页 > 编程日记 > 正文

数据结构--数组队列的实现

数据结构--数组模拟队列

    • 1. 说明
    • 2. 实现代码
      • 1. 数组队列类
      • 2.数组队列测试类
      • 3.代码运行结果
    • 3.完整代码

1. 说明

  1. 队列是一个有序列表,可以用数组或者链表来实现。

  2. 遵循先入先出(FIFO)的原则,即先存入列的数据,会被先取出,后存入的会被后取出,实际中的排队就是最好的说明。

在这里插入图片描述

  1. 索引( 初始值front = 0 rear=0)在插入新数据时,在rear位置插入,rear++, front保持不动 ,删除数据的时候,first++

  2. 判断条件

    maxSize为数组的最大容量

    • 队列空:front == rear

    • 队列满:rear = maxSize -1

2. 实现代码

1. 数组队列类

//使用数组模拟队列,编写一个ArrayQueue类
class ArrayQueue {//表示数组的最大容量private final int maxSize;//队列头private int front;//队列尾private int rear;//该数据用于存放数据,模拟队列private final 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 n) {if (isFull()) {System.out.println("队列满,不能加入数据!");return;}rear++;arr[rear] = n;}//    获取队列的数据,取出队列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 headQueue() {
//        判断if (isEmpty()) {throw new RuntimeException("队列为空,无数据");}return arr[front + 1];}}

2.数组队列测试类

/*** @author Manix* 数组模拟队列*/
public class ArrayQueueDemo {public static void main(String[] args) {ArrayQueue queue = 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':queue.showQueue();break;case 'a':System.out.println("输出一个数");int value = scanner.nextInt();queue.addQueue(value);break;case 'g':try {int res = queue.getQueue();System.out.printf("取出的数据是%d\n", res);} catch (Exception e) {System.out.println(e.getMessage());}break;case 'h':try {int res = queue.headQueue();System.out.printf("队列头的数据是%d\n", res);} catch (Exception e) {System.out.println(e.getMessage());}break;case 'e':loop = false;scanner.close();break;default:break;}}System.out.println("----程序退出----");}
}

3.代码运行结果

s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输入一个数
10
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输入一个数
20
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输入一个数
30
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
a
输入一个数
40
队列满,不能加入数据!
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
arr[0]=10
arr[1]=20
arr[2]=30
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
取出的数据是10
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
取出的数据是20
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
g
取出的数据是30
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据
s
队列为空,没有数据
s(show): 显示队列
e(exit): 退出程序
a(add): 添加数据到队列
g(get): 从队列取出数据
h(head): 查看队列头的数据

3.完整代码

package com.starking.queue;import java.util.Scanner;/*** @author Manix* 数组模拟队列*/
public class ArrayQueueDemo {public static void main(String[] args) {ArrayQueue queue = 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':queue.showQueue();break;case 'a':System.out.println("输入一个数");int value = scanner.nextInt();queue.addQueue(value);break;case 'g':try {int res = queue.getQueue();System.out.printf("取出的数据是%d\n", res);} catch (Exception e) {System.out.println(e.getMessage());}break;case 'h':try {int res = queue.headQueue();System.out.printf("队列头的数据是%d\n", res);} catch (Exception e) {System.out.println(e.getMessage());}break;case 'e':loop = false;scanner.close();break;default:break;}}System.out.println("----程序退出----");}
}//使用数组模拟队列,编写一个ArrayQueue类
class ArrayQueue {//表示数组的最大容量private final int maxSize;//队列头private int front;//队列尾private int rear;//该数据用于存放数据,模拟队列private final 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 n) {if (isFull()) {System.out.println("队列满,不能加入数据!");return;}rear++;arr[rear] = n;}//    获取队列的数据,取出队列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 headQueue() {
//        判断if (isEmpty()) {throw new RuntimeException("队列为空,无数据");}return arr[front + 1];}}

相关文章:

DIV+CSS一行两列布局

实现效果&#xff1a; main 我是包在外面的div col1 我是第一列col2 我是第二列clear-float;我用来清除浮动&#xff08;清除float&#xff09;以下是说明&#xff1a;CSS代码&#xff1a;.main{width:800px;/* 总的宽度 */ background:red; } .main .col1{ float:left;/* 这个…

编程上标和下标使用方法

1.问题&#xff1a;写代码要求显示平方、立方、化学符号等等完全写不出来&#xff0c;Word写出来复制出来也不管用 2.办法&#xff1a;Unicode下标和上标 3.举例&#xff1a;string.Format("{0} km\xB2"&#xff0c;1000&#xff09;&#xff0c;单位是平方千米&…

上周新闻回顾:微软补丁个个紧急 奥运网络百花齐放

也许是美国不是黄金周的原因&#xff0c;五一刚过&#xff0c;直接来自国外的新产品发布等IT新闻就源源不断涌来&#xff0c;倒是国内的新闻发布不是非常多。不过&#xff0c;微软的5月安全补丁如期发布&#xff0c;还是值得大家关注的。此外&#xff0c;关于2008年奥运会网络建…

rest-framework之解析器

rest-framework之解析器 本文目录 一 解析器的作用二 全局使用解析器三 局部使用解析器四 源码分析回到目录一 解析器的作用 根据请求头 content-type 选择对应的解析器对请求体内容进行处理。 有application/json&#xff0c;x-www-form-urlencoded&#xff0c;form-data等格式…

httpd常用配置

author&#xff1a;JevonWei版权声明&#xff1a;原创作品 检查配置文件时&#xff0c;如下提示&#xff0c;则因为没有server的服务名称导致&#xff0c;故设置网站的服务server名称&#xff0c;若没有设置web服务名&#xff0c;主默认解析系统主机名(添加主机名解析) [rootda…

[导入]C#中实现Socket端口复用

一、什么是端口复用&#xff1a;   因为在winsock的实现中&#xff0c;对于服务器的绑定是可以多重绑定的&#xff0c;在确定多重绑定使用谁的时候&#xff0c;根据一条原则是谁的指定最明确则将包递交给谁&#xff0c;而且没有权限之分。这种多重绑定便称之为端口复用。 二、…

数据结构学习系列文章合集

数据结构学习系列文章目录前言1.稀疏数组和队列稀疏数组和二位数组的转换数组队列的实现环形队列的介绍与实现2.链表单链表的增、删、改、查总结前言 学习数据结构记录&#xff0c;作为自己的笔记&#xff0c;同时也可以方便大家一起交流和学习 1.稀疏数组和队列 稀疏数组和二…

支付宝Payto接口的c#.net实现

它现在这种支付方式比较多象网银在线等使用的方法都是url验证&#xff0c;就是通过url参数和一个这些url参数的md5编码来确认这个连接的正确性&#xff0c;支付宝在你购买成功后跳转自定义连接的时候会传2次过来&#xff0c;第一次是数据底层请求&#xff0c;第二次是web请求&a…

【Visual Studio 扩展工具】如何在ComponentOneFlexGrid树中显示RadioButton

概述 在ComponentOne Enterprise .NET控件集中&#xff0c;FlexGrid表格控件是用户使用频率最高的控件之一。它是一个功能强大的数据管理工具&#xff0c;轻盈且灵动&#xff0c;以分层的形式展示数据&#xff08;数据呈现更加直观&#xff09;。 FlexGrid 简介 FlexGrid 是业界…

如何 SQL Server 2005 实例之间传输登录和密码

INTRODUCTION 本文介绍如何不同服务器上的 Microsoft SQL Server 2005 实例之间传输登录和密码。 本文, 服务器 A 和服务器 B 是不同的服务器。 此外, 服务器 A 和 B 服务器都运行 SQL Server 2005。 将数据库从服务器 A 上的 SQLServer 实例移到 B, 服务器上的 SQLServer 实例…

IDEA配置GitHub报错GitHub Invalid authentication data.404 Not Found-Not Found

登录账户GitHub Invalid authentication data.404 Not Found-Not Found报错及解决办法1 登录自己的github账号--》头像---》settting2 Developer settings3 Personal access tokens4 回到IDEA中&#xff0c;粘贴上自己的token就可以了想要把自己的代码上传到GitHub中&#xff0…

console.log 简写

console.log 简写 平常代码调试总会用到console.log&#xff0c;但是每次写这么长也是很麻烦&#xff0c;就想着存一个简介一点的变量&#xff1b; 然后就随手写了下面代码&#xff1b; var a 10;var log console.log;log(a);调用的时候发现火狐浏览器报错了&#xff0c;仔细…

为何我的BLOG不能DIY?

今天想把MODULE调整一下&#xff0c;居然搞不定。估计是服务器又出问题了........不知道51CTO有没有备份我们的博克呀&#xff1f;

Java中的自动装箱和拆箱

自动装箱和拆箱自动装箱和拆箱自动装箱&#xff1a;拆箱1. 为什么要有包装类(或封装类&#xff09;2. 基本数据类型与对应的包装类&#xff1a;3. 类型间的转换4. 何时发生自动装箱和拆箱赋值、数值运算时方法调用时&#xff1a;自动装箱、拆箱中的坑自动装箱和拆箱 目的&…

【Java】身份证号码验证

代码引用自&#xff1a;https://gitee.com/appleat/codes/ynrtqujv0wfgesm8ia9b547 1 package xxx;2 3 /**4 * Created by wdj on 2017/6/21.5 */6 7 import java.text.ParseException;8 import java.text.SimpleDateFormat;9 import java.util.Calendar;10 import java.util…

linux history记录格式修改

#保存一万条命令记录 sed -i s/^HISTSIZE1000/HISTSIZE10000/g /etc/profile#在/etc/profile的文件尾部添加如下行数配置信息 ######jiagu history xianshi######### USER_IPwho -u am i 2>/dev/null | awk {print $NF} | sed -e s/[()]//g if [ "$USER_IP" &quo…

从EAI到SOA

写在前面SOA现在越发闹腾的厉害了&#xff0c;各种宣传越来越多&#xff0c;都把SOA吹上天&#xff1b;到底SOA是什么&#xff0c;有啥神奇之处&#xff0c;真的想宣传说的那么好吗&#xff1f;看了种种文章&#xff0c;只是越发混沌。罢了&#xff0c;俺做技术的&#xff0c;商…

用C#实现FTP搜索引擎

晚辈最近用C#写了一个教育网FTP搜索引擎&#xff0c;希望能得到高手的指点。 网址&#xff1a;http://soso.ccnu.com.cn http://it.ccnu.edu.cn/soso 部分代码&#xff1a; using System;using softplib;using System.Threading;using System.Collections;using System.Ne…

IDEA配置GitHub和Gitee

IDEA配置GitHub和GiteeIDEA配置GitHub和GiteeGit准备IDEA内配置Git配置GitHub1. IDEA的Settings-->Version Control ---> GitHub2. 登录账户GitHub Invalid authentication data.404 Not Found-Not Found报错及解决办法2.1 登录自己的github账号--》头像---》settting2.2…

MATLAB 2014a (8.3) Compiler Runtime (MCR)

在安装的时候可以 ./install -H 界面化安装到自己目录下 MATLAB 2014a (8.3) Runtime Compiler (MCR) Errors when trying to launch deployed (using deploy tool) application in Ubuntu 13.04. Right after installation of MCR if one runs the deployed application follo…

[Quiz]竞赛题目 Word Trace

一、竞赛题目Problem Statement You are given a String[] grid representing a rectangular grid of letters. You are also given a String find, a word you are to find within the grid. The starting point may be anywhere in the grid. The path may move up, down, le…

c#总结最近的几项重要代码

java的代码就不说了&#xff0c;毕竟不是我的主业。 1.c#数据库连接池Hikari. (1)动态加载各类数据库驱动 &#xff08;2&#xff09;支持简单配置文件 &#xff08;3&#xff09;支持按照名称多数据库调用 &#xff08;4&#xff09;使用简洁 单数据库使用&#xff1a; Hikari…

动态模板列更新数据分页的例子

前台&#xff1a;<% Page language"c#" Codebehind"WebForm30.aspx.cs" AutoEventWireup"false" Inherits"csdn.WebForm30" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML>…

[您有新的未分配科技点]可,可,可持久化!?------0-1Trie和可持久化Trie普及版讲解...

这一次&#xff0c;我们来了解普通Trie树的变种&#xff1a;0-1Trie以及在其基础上产生的可持久化Trie&#xff08;其实&#xff0c;普通的Trie也可以可持久化&#xff0c;只是不太常见&#xff09; 先简单介绍一下0-1Trie&#xff1a;一个0-1Trie节点只有两个子节点&#xff0…

SQL查询1064报错 [ERR] 1064 - You have an error in your SQL syntax; check the manual.......

MySQL建表出现1064问题问题 SQL语句 DROP DATABASE IF EXISTS bookstore; DROP DATABASE bookstore; USE bookstore; CREATE TABLE t_user (id INT PRIMARY KEY auto_increment,username VARCHAR ( 20 ) NOT NULL UNIQUE,password VARCHAR ( 32 ) NOT NULL,email VARCHAR ( …

移动端丨-webkit-overflow-scrolling:touch属性导致页面卡住

起因 起因-webkit-overflow-scrolling问题解决方案&#xff1a; 方案一方案二思考为什么会出现这个问题总结故事的起因是&#xff0c;在一个多列表的页面上&#xff0c;页面在iOS11&#xff0c;跟iOS10中会发生页面卡住&#xff0c;不能进行滚动。 然后就怀疑是自己的样式写的出…

瑞星杀毒软件所有监控已禁用!

瑞星杀毒软件所有监控已禁用! 我的瑞星杀毒软件所有监控已禁用!在右下脚有个红色的小伞,可以升级,但是监控怎么都开启不了。 解决办法是&#xff1a;启动主程序&#xff0c;点“工具列表”&#xff0c;选择“瑞星监控中心”&#xff0c;点“运行”&#xff0c;在弹出的窗口…

Typora输出表情 Typora_Smile

文章目录小表情还挺好看的SmileNatureObjectsPlacesSymbols小表情还挺好看的 Smile &#x1f604; :smile:&#x1f606; :laughing:&#x1f60a; :blush:&#x1f603; :smiley:☺️ :relaxed:&#x1f60f; :smirk:&#x1f60d; :heart_eyes:&#x1f618; :kissing_hear…

COOKIE操作

import scrapyclass CookiedemoSpider(scrapy.Spider):name cookiedemo# allowed_domains [www.douban.com]start_urls [https://www.douban.com/accounts/login/]def parse(self, response):# 登录成功后对页面数据进行存储fp open("main.html", "w",…

01--安装Activiti流程设计器eclipse插件

Activiti1 安装流程设计器eclipse插件   Name:Activiti BPMN 2.0 designer&#xff08;随便起个名字&#xff09;   Location: http://activiti.org/designer/update/ 安装完成后勾选(不勾选不生成bpmn文件) 转载于:https://www.cnblogs.com/miye/p/7283468.html