线性结构 —— 数组队列

一、介绍

?队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。

二、代码

?使用数组模拟队列,首先编写一个ArrayQueue类

class ArrayQueue {
    private int maxSize; // 最大容量
    private int front; // 指向队列头
    private int rear; // 指向队列尾
    private int[] queue; // 模拟队列

    public ArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        queue= new int[maxSize];
        front = -1; // 指向队列头部,指向队列头部前一个位置
        rear = -1; // 指向队列尾部,指向队列尾部的具体位置
    }
}

?添加队列类的相关方法

    // 判断队列是否已满
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    // 判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    // 添加元素
    pubic void addQueue(int n) {
        if(isFull()) {
            System.out.println("队列已满,无法添加新的元素!");
            return;
        }
        rear++;
        queue[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 < queue.length; i++) {
            System.out.printf("queue[%d]:%d\n", i , queue[i]);
        }
    }

    // 查看队列头
    public int headQueue() {
        if(isEmpty()) {
            throw new RunTimeException("队列为空,无法显示头信息");
        }
        return queue[front + 1];
    }        

三、测试

public class ArrayQueueDemo {

    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3); // 设定队列长度为3
        char key = ' '; // 记录用户的输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true; // 循环标识
        while(loop) {
            System.out.println("s:显示队列");
            System.out.println("e:退出队列");
            System.out.println("a:添加元素");
            System.out.println("g:获取元素");
            System.out.println("h:显示队列头数据");

            key = sacnner.next().charAt(0);
            switch(key) {
                case 's':
                    arrayQueue.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);
                        break;
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                case 'h':
                    try {
                        int res = queue.headQueue();
                        System.out.printf("队列头数据是:%d", res);
                        break;
                    } catch(Exception e) {
                        System.out.println(e.getMessage());
                    }
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
            }
        }
        System.out.println("程序退出!");
    }
}     

原文地址:https://www.cnblogs.com/gary97/p/12255242.html

时间: 2024-08-28 03:06:58

线性结构 —— 数组队列的相关文章

数据结构 线性结构(数组[列表] ,链表 单链表的增删改查**, 线性结构的应用 队列 栈[函数的调用**]),非线性结构 树

数据结构 参考:http://lupython.gitee.io/ 线性结构 就是能够用一根线串起来的数据结构 数组 (列表) 问:申请数组的前提条件是啥? a[12]?内存需要满足的条件? 答:内存必须有一块连续的内存空间 int a[7] : 声明一个数组,这个数组的数组名是 a, 数组的大小是 7, 数组元素的类型是整型. int a[7] = array(1,2,3,4,5,6,7) 问:如何申请内存? 答:C,C++语言,申请:mallco (28).释放:free(28) 问:int

线性结构-数组和链表的实现

连续存储(数组) 1.什么叫数组? 元素的类型相同,大小相等. 2.优缺点? 查询速度快,增加删除慢 #include <stdio.h> #include <malloc.h> #include <stdlib.h> //定义一个数据类型,名字 Arr,成员分别 struct Arr { int * pBase; //存储的是数组第一个元素的地址 int len; //数组的长度 int cnt; //当前数组有效元素的个数 int increment; //自动增长

线性结构之链队列实现

#include<stdio.h> #include<malloc.h> #include<stdlib.h> typedef struct node { int data; struct node *next; }NODE,*PNODE; typedef struct queue { PNODE front; PNODE rear; }QUEUE; void initQueue(QUEUE *); void enQueue(QUEUE *,int); void deQ

原始线性结构数组的实现以及操作

不多说直接上代码: #include <stdio.h> #include <malloc.h> #include <stdlib.h> struct Arr { int *pBase; int len; int cnt; }; void init_arr(struct Arr *pArr,int length);//初始化一个数组 void show_arr(struct Arr *pArr);//显示某一个数组 bool isEmpty(struct Arr *pA

线性存储结构--数组

#include<stdio.h> #include<stdlib.h> //定义了一个数据类型叫struct Arr 该数据类型包含 3个成员 struct Arr{ //12个字节 int *pBase;//存储数组第一个元素的地址 int len;//数组所能容纳的最大元素个数 int cnt;//当前数组有效元素的个数 }; //不用指针 用值得话 导致方法里存在局部变量 不能使主方法的改变 只能传地址 void Init_arr(struct Arr *p,int len

线性结构 —— 循环数组

一.介绍 ?上一章讲得数组队列无法重复使用,这一章我们使用环形数组实现队列. 二.代码 ?使用环形数组模拟队列,首先编写一个CircleArray class CircleArray { private int maxSize; // 表示数据最大容量 private int front; // 指向队列头 第一个元素 private int rear; // 指向队列最后元素的下一个位置,我们约定空出一个元素空间 private int[] queue; // 队列 // 创建队列构造器 pu

数据结构和算法-数据结构-线性结构-栈和队列

 ################################################## """ 三.线性结构 (1)栈 1.定义:栈是一个数据集合,可以理解为只能在一端进行插入或者删除操作的列表. 2.栈的特点:后进先出(last-in,first-out),简称LTFO表 这种数据结构的特点: 就是像是杯子或者是弹夹,电梯, 存储的时候从底部开始,读取的时候从顶部开始,具备这种特点就是栈 就是后进先出, 存储的时候就可以从顺序表或者链表就可以实现, 只让从一

线性结构和非线性结构

线性结构和非线性结构同属于数据结构中的逻辑结构类型 线性结构是指该结构中的节点之间存在一对一的关系.其特点是开始节点和终端节点都是唯一的,除了开始节点和终端节点外,其余节点都有且仅有一个直接前驱,有且仅有一个直接后继.此类型的存储结构有:顺序表(数组).链表.堆栈结构.队列结构等 非线性结构又包括集合.树形结构.图形结构或网状结构,特点是数据元素之间存在一个对多个或多个对多个的关系,其中集合是一种关系极为松散的结构.

JAVA之数组队列

package xxj.datastructure0810; import java.util.Random; public class DataStructure { /** * @param args */ public static void main(String[] args) { String [] array = {"1","2","3"}; MyArrayList<String> mal = new MyArrayLi