队列类/ IsEmpty 方法

发布于 2024-10-31 17:44:50 字数 1687 浏览 1 评论 0原文

到目前为止,这一切(至少我希望如此哈哈)是

class Queue
{

    private int front = 0; //Creates front and back int holders and an array
    private int back = -1;
    private int[] anArray;

    public Queue(int size) // constructor to get the Queue size 
    {
        anArray = new int[size];
    }

    public bool IsFull
    {
        get // gets to see if the Queue is full ( I assume I did this right, It's full if the back of the queue is equal to -1 of an array)
        {
            return back == anArray.Length - 1;
        }
    }

    public bool IsEmpty
    {
        get // It's empty if the back is -1; I think this is where I messed up, I think that when it checks to see if it's empty it also doesn't check if it's empty when I have dequeued the other numbers (or write them off). Would I make something like "Return front == anArray.Length -1;" ? That would check to see when the front (The part being written to console first) hits the end of the array?
        {
            return back == -1;
        }
    }

    public void Enqueue(int valueEnqueue)
    { // Method to Enqueue the variables into the array

        if (IsFull)
        {
            //do nothing
        }
        else
        {
            back = back + 1;
            anArray[back] = valueEnqueue;

        }
    }

    public int Dequeue()
    { // Method to dequeue the variables put in
        if (IsEmpty)
        {
            //do nothing
            return 1010101010;
        }
        else
        {
            int dequeue = anArray[front];
            front = front + 1;
            return dequeue;
        }
    }

所以我猜我的问题是什么,遵守正常的队列思维(先进先出)我如何让它停止?我不断收到索引超出范围错误。

So far all this does (at least I hope so lol) is

class Queue
{

    private int front = 0; //Creates front and back int holders and an array
    private int back = -1;
    private int[] anArray;

    public Queue(int size) // constructor to get the Queue size 
    {
        anArray = new int[size];
    }

    public bool IsFull
    {
        get // gets to see if the Queue is full ( I assume I did this right, It's full if the back of the queue is equal to -1 of an array)
        {
            return back == anArray.Length - 1;
        }
    }

    public bool IsEmpty
    {
        get // It's empty if the back is -1; I think this is where I messed up, I think that when it checks to see if it's empty it also doesn't check if it's empty when I have dequeued the other numbers (or write them off). Would I make something like "Return front == anArray.Length -1;" ? That would check to see when the front (The part being written to console first) hits the end of the array?
        {
            return back == -1;
        }
    }

    public void Enqueue(int valueEnqueue)
    { // Method to Enqueue the variables into the array

        if (IsFull)
        {
            //do nothing
        }
        else
        {
            back = back + 1;
            anArray[back] = valueEnqueue;

        }
    }

    public int Dequeue()
    { // Method to dequeue the variables put in
        if (IsEmpty)
        {
            //do nothing
            return 1010101010;
        }
        else
        {
            int dequeue = anArray[front];
            front = front + 1;
            return dequeue;
        }
    }

So I guess what my question is, abiding by the normal Queue thinking (First in First out) how do I get it to stop? I keep getting an index out of range error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洒一地阳光 2024-11-07 17:44:50

你想重新发明轮子吗?

为什么不使用:system.collections.queue

http://msdn.microsoft.com/en-us/library /system.collections.queue.aspx

如果您只是想这样做,请在 system.collections.queue 上尝试 Reflector 并查看里面有什么。

Are you trying to reinvent the wheel?

Why not use: system.collections.queue?

http://msdn.microsoft.com/en-us/library/system.collections.queue.aspx

And if your just want to do so, Try Reflector on system.collections.queue and see what's inside.

撕心裂肺的伤痛 2024-11-07 17:44:50

乍一看,我怀疑您的 Dequeue 函数出现 IndexOutOfRange 异常,该函数对 front 变量没有限制,它只是在每次调用时不断递增,最终会超过数组长度。

队列结构通常被实现为循环缓冲区。请查看此处以了解可能有助于您实施的更多详细信息。

http://en.wikipedia.org/wiki/Circular_buffer

At first glance I suspect you are getting an IndexOutOfRange exception on your Dequeue function which has no limit on the front variable, it just keeps incrementing on each call and will eventually exceed the array length.

A queue structure is normally implemented as a circular buffer. Take a look here for more details that might help you with your implementation.

http://en.wikipedia.org/wiki/Circular_buffer

往事风中埋 2024-11-07 17:44:50

您有一个有限的数组,并且期望有无限的容量。数组不是最好的容器,您应该使用其他东西来实现队列,例如列表容器。

Enqueue(item) { list.Add(item); }
Dequeue() 
{ 
  if( !IsEmpty ) 
  {
    var item = list[0];
    list.Remove(item);
    return item;
  }
  return null;
}
IsFull{ get { return false; } }
IsEmpty{ get { return list.Count == 0; }

You have a finite array and are expecting infinite capacity. An array isn't the best container for this, you should use something else to implement your queue, like the List container.

Enqueue(item) { list.Add(item); }
Dequeue() 
{ 
  if( !IsEmpty ) 
  {
    var item = list[0];
    list.Remove(item);
    return item;
  }
  return null;
}
IsFull{ get { return false; } }
IsEmpty{ get { return list.Count == 0; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文