本文介绍了Java - 使用数组的有界队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求创建一个有条件的队列类,其条件如下:

I was asked to create a bounded queue class to which the conditions were the following:

仅使用基本类型,实现有界队列来存储整数。应针对算法运行时,内存使用和内存吞吐量优化数据结构。不应导入和/或使用外部库。解决方案应该在一个提供以下功能的类中提供:

Using only primitive types, implement a bounded queue to store integers. The data structure should be optimized for algorithmic runtime, memory usage, and memory throughput. No external libraries should be imported and/or used. The solution should be delivered in one class that provides the following functions:


  • 构造函数 - 类应该提供一个对象创建方法,它采用整数设置队列的大小。

  • enqueue - 如果队列未满,则函数应该取整数并将其存储在队列中。该函数应该正确处理队列已满的情况。

  • dequeue - 如果当前存储在队列中,函数应该返回一个整数。该函数应该正确处理队列为空的情况。

我写了这个课但是我想通过有人请求帮助测试它以查看它是否正常工作。我写了一个小的主要类来测试它,一切似乎都在工作,但我想在提交它之前看看另一双眼睛。它是为了实习。提前谢谢。

I wrote this class but I wanted to ask for help by having someone test it as well to see if it works properly. I wrote a small main class to test it and everything seems to be working but I want another pair of eyes to look at it before I submit it. Its for an internship. Thank you in advance.

public class Queue<INT>
{


    int size;
    int spacesLeft;
    int place= 0;
    int[] Q;


    public Queue(int size)
    {
        this.size = size;
        spacesLeft = size;
        Q = new int[size];
    }

    //enqueue - function should take an integer and store it in the queue if the queue isn't full.
    //The function should properly handle the case where the queue is already full
    public void enque(int newNumber) throws Exception
    {
        if(place <= size)
        {
            Q[place] = newNumber;
            place++;
            spacesLeft--;

        }
        else
            throw new Exception();
    }


    //dequeue - function should return an integer if one is currently stored in the queue.
    //The function should properly handle the case where the queue is empty.
    public int deque() throws Exception
    {
        int dequeNum;


        if(spacesLeft == size)
            throw new Exception();
        else
        {
            dequeNum = Q[0];
            spacesLeft++;
        }

        int[] tempAry = new int[size];
        for (int i=0; i < Q.length; i++)
        {
            if(i < size-1)
            {
                tempAry[i] = Q[i+1]; // put in destination
            }

        }

        Q = tempAry;

        for(int i = 0; i < Q.length; i++)
        {
            System.out.println("value in Q"+Q[i]);

        }


        return dequeNum;


    }
}


推荐答案

这是根据您的规范实现的。

Here is the implementation that as per your specifications.

以下是相同的源代码。

import java.util.Arrays;

public class Queue {

    private int enqueueIndex;// Separate index to ensure enqueue happens at the end
    private int dequeueIndex;// Separate index to ensure dequeue happens at the
                            // start
    private int[] items;
    private int count;
    // Lazy to add javadocs please provide
    public Queue(int size) {
        enqueueIndex = 0;
        dequeueIndex = 0;
        items = new int[size];
    }
    // Lazy to add javadocs please provide
    public void enqueue(int newNumber) {
        if (count == items.length)
            throw new IllegalStateException();
        items[enqueueIndex] = newNumber;
        enqueueIndex = ++enqueueIndex == items.length ? 0 : enqueueIndex;
        ++count;
    }
    // Lazy to add javadocs please provide
    public int dequeue() {
        if (count == 0)
            throw new IllegalStateException();
        int item = items[dequeueIndex];
        items[dequeueIndex] = 0;
        dequeueIndex = ++dequeueIndex == items.length ? 0 : dequeueIndex;
        --count;
        return item;
    }

    @Override
    public String toString() {
        return Arrays.toString(items);
    }
}

这篇关于Java - 使用数组的有界队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 05:42