自定义线性及非线性存储的队列实现

简介: 在平时的编程中,队列可以应用于很多方面。 在生活中我们同样可以随处见到它的身影,比如我们排队,先排的人先得到服务,后进来的人后接受服务。这就是队列。 说白了,就是FIFO原则(First in First out, 先进先出)。

在平时的编程中,队列可以应用于很多方面。
在生活中我们同样可以随处见到它的身影,比如我们排队,先排的人先得到服务,后进来的人后接受服务。这就是队列。
说白了,就是FIFO原则(First in First out, 先进先出)。
队列的实现是基于存储结构不同而不同的,通常会有两种方式存储。线性存储或者非线行存储。

1、线性存储。是基于数组等长度固定的存储方式来实现的。我这里是先定义了一个接口,然后基于接口来实现的。
下面请看代码,

package simple;

interface queue {
    boolean isEmpty();

    void in(Object value);

    Object top();

    void out();

    boolean isFull();

    int getSize();
}

public class ArrayBase implements queue {
    private static final int MAXSIZE = 100;
    int front;
    int rear;
    int number;
    Object[] data;

    ArrayBase() {
        front = rear = -1;
        number = 0;
        data = new Object[MAXSIZE];
    }

    @Override
    public void in(Object value) {
        // TODO Auto-generated method stub
        if (this.isFull()) {
            System.out.println("Sorry, the queue is full!");
        } else {
            this.rear = (this.rear + 1) % MAXSIZE;
            number++;
            this.data[this.rear] = value;
        }
    }

    @Override
    public Object top() {
        // TODO Auto-generated method stub
        if (this.isEmpty()) {
            System.out.println("Sorry, the queue is empty!");
        }
        int p = (this.front + 1) % MAXSIZE;
        return data[p];
    }

    @Override
    public void out() {
        // TODO Auto-generated method stub
        if (this.isEmpty()) {
            System.out.println("Sorry, the queue is empty!");
        }
        this.front = (this.front - 1) % MAXSIZE;
        number--;

    }

    @Override
    public boolean isFull() {
        // TODO Auto-generated method stub
        if (this.rear == MAXSIZE - 1) {
            return true;
        }
        return false;
    }

    public boolean isEmpty() {
        if (number == 0) {
            return true;
        }
        return false;
    }

    @Override
    public int getSize() {
        // TODO Auto-generated method stub
        return this.number;
    }

}

接下来是测试类:

package simple;

public class MyQueueTest {

    public static void main(String []args){
        ArrayBase myQueue=new ArrayBase();
        System.out.println(((ArrayBase) myQueue).isEmpty());
        myQueue.in(false);
        myQueue.in(1);
        myQueue.in('T');
        myQueue.in("Biao");
        System.out.println("Now there are "+myQueue.getSize()+"  items!");
        System.out.println("Top Item is: "+ myQueue.top());
        myQueue.out();
        System.out.println("After the out operation,there are "
                            +myQueue.getSize()+" items in this queue!");
    }

}

然后就是我们的测试结果了:

true
Now there are 4  items!
Top Item is: false
After the out operation,there are 3 items in this queue!

2、当然也可以使用非线性的存储结构来实现队列,好处是可以避免“假溢出”的现象,充分的利用到空间。
下面是代码实现:

package simple;

interface queueListBase {
    boolean isEmpty();

    void in(Object value);

    Object top();

    Object out();

    int getSize();
}

class Node{
    Object data;
    Node next;
    Node(){
        data=null;
        next=null;
    }

}

public class ListBase implements queueListBase{

    int number;
    Node front;
    Node rear;

    public ListBase(){
        number=0;
        front=rear=null;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        if(this.front==this.rear){
            return true;
        }
        return false;
    }

    /*
     * (non-Javadoc)此方法应该尤其要注意的是对初始时的分析
     * 我就在此处忘记了front的存在,结果就会导致程序一直抱NullPointerException的错误!
     * @see simple.queueListBase#in(java.lang.Object)
     */
    @Override
    public void in(Object value) {
        // TODO Auto-generated method stub
        if (rear == null && front == null) {  
            rear = new Node();
            rear.data=value;
            front = rear;  
        } else {  
            Node node = new Node();
            node.data=value;
            rear.next = node;  
            rear = node;
            number++;
        }  
    }

    @Override
    public Object top() {
        // TODO Auto-generated method stub
        Node temp=new Node();
        temp.data=this.front.data;
        return temp;
    }

    @Override
    public Object out() {
        // TODO Auto-generated method stub
        Object temp=null;
        if(this.isEmpty()){
            System.out.println("Sorry,the queue is Empty!");
            return false;
        }else{
            temp=this.front.data;
            this.front=this.front.next;
            if(front.next==null){
                this.rear=this.front;
            }
            number--;
        }
        return temp;
    }

    @Override
    public int getSize() {
        // TODO Auto-generated method stub
        return number;
    }

}

下面是我的测试代码:

package simple;

public class MyQueueTest {

    public static void main(String []args){
//      ArrayBase myQueue=new ArrayBase();
        ListBase myQueue=new ListBase();
        System.out.println(((ListBase) myQueue).isEmpty());
        myQueue.in(false);
        myQueue.in(1);
        myQueue.in('T');
        myQueue.in("Biao");
        System.out.println(myQueue.out());
        System.out.println(myQueue.out());
        System.out.println(myQueue.out());
        System.out.println(myQueue.out());
        System.out.println("Now there are "+myQueue.getSize()+"  items!");
        System.out.println("Top Item is: "+ myQueue.top());
        myQueue.out();
        System.out.println("After the out operation,there are "
                            +myQueue.getSize()+" items in this queue!");
    }

}

下面是测试的结果:

true
false
1
T
Sorry,the queue is Empty!
false
Now there are 0  items!
Top Item is: simple.Node@659e0bfd
Sorry,the queue is Empty!
After the out operation,there are 0 items in this queue!

结果分析:
不难看出,在链式队列内存储值时,字符串类型并没有成功存储进去。但这并不代表队列无法存储字符串类型的数据,所以我们应该注意这一点!否则将会有可能导致今后软件的出错!下面是一个更加完整的非线性的队列的实现代码,也是我入队操作失败后得到纠正的一篇博客,与君共勉,共同进步吧!非线性队列的实现

目录
相关文章
数学界的线性和非线性的概念
数学界的线性和非线性的概念
|
机器学习/深度学习
普通卷积、分组卷积和深度分离卷积概念以及参数量计算
普通卷积、分组卷积和深度分离卷积概念以及参数量计算
1160 0
普通卷积、分组卷积和深度分离卷积概念以及参数量计算
|
4月前
|
机器学习/深度学习 算法 计算机视觉
YOLOv8改进-论文笔记】 AKConv(可改变核卷积):任意数量的参数和任意采样形状的即插即用的卷积
AKConv是一种可改变核卷积,旨在解决传统卷积的局限,包括固定大小的卷积窗口和卷积核尺寸。AKConv提供灵活的卷积核参数和采样形状,适应不同尺度特征。其创新点包括:1)支持任意大小和形状的卷积核;2)使用新算法确定初始采样位置;3)应用动态偏移调整采样位置;4)优化模型参数和计算效率。AKConv已应用于YOLOv8,提高网络性能。相关代码可在<https://github.com/CV-ZhangXin/AKConv>找到。
|
2月前
|
PyTorch 测试技术 算法框架/工具
【YOLOv8改进 - 卷积Conv】SPConv:去除特征图中的冗余,大幅减少参数数量 | 小目标
YOLO目标检测专栏探讨了模型优化,提出SPConv,一种新卷积操作,减少特征冗余,提升效率。SPConv将特征分为代表性和不确定部分,分别处理,再融合。实验显示,SPConv在速度和准确性上超越现有基准,减少FLOPs和参数。论文和PyTorch代码已公开。更多详情及实战案例见CSDN博客链接。
|
2月前
|
机器学习/深度学习 算法 计算机视觉
【YOLOv10改进 -卷积Conv】 AKConv(可改变核卷积):任意数量的参数和任意采样形状的即插即用的卷积
AKConv是一种可改变核卷积,旨在解决传统卷积的局限,包括固定大小的卷积窗口和卷积核尺寸。AKConv提供灵活的卷积核参数和采样形状,适应不同尺度特征。其创新点包括:1)支持任意大小和形状的卷积核;2)使用新算法确定初始采样位置;3)应用动态偏移调整采样位置;4)优化模型参数和计算效率。AKConv已应用于YOLOv8,提高网络性能。相关代码可在<https://github.com/CV-ZhangXin/AKConv>找到。
YOLOv8打印模型结构配置信息并查看网络模型详细参数:参数量、计算量(GFLOPS)
YOLOv8打印模型结构配置信息并查看网络模型详细参数:参数量、计算量(GFLOPS)
|
11月前
稀疏数组与队列
稀疏数组与队列
24 0
|
存储 算法 索引
稀疏数组和队列
稀疏数组和队列
39 0
|
存储 算法 定位技术
第 3 章 稀疏数组和队列
稀疏数组把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
62 0
|
机器学习/深度学习 算法 语音技术
自适应线性单元|学习笔记
快速学习自适应线性单元
163 0
自适应线性单元|学习笔记