实施队列 [英] Implementing Queues

查看:68
本文介绍了实施队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我把这个程序放在一起就可以了...大声笑
我的输出全为0.我无法弄清楚如何放入我的东西
队列之外的队列.我添加了一个for循环,没有那么多行.
我陷入困境,我需要另一双眼睛才能看到我可能会丢失的东西.

这是main的代码

Hello All,

I put this program together and it kinda works...lol
My output is all 0''s. I cant figure out how to get what i put into
the queue out of the queue. I add a for loop to not have so many lines.
Im stuck and I need another set of eyes to see what I may me missing.

here is the code for main

public static void Main(string[] args)
       {
           MyQueue q = new MyQueue();
           int v = 0;

           Console.Write("MyQueue Items: ");
           for (int i = 0; i < 6; i++)
           {
               v = i * 5;
               q.enqueue(v);
               Console.Write(v + "\t");
           }

           Console.WriteLine("\n");
           v = q.dequeue();
           Console.WriteLine("Dequeue: "+ v);
           v = q.dequeue();
           Console.WriteLine("Dequeue: {0}",v);
           v = q.dequeue();
           Console.WriteLine("Dequeue: {0}",v);
           v = q.dequeue();
           Console.WriteLine("Dequeue: {0}",v);
           v = q.dequeue();
           Console.WriteLine("Dequeue: {0}",v);
           v = q.dequeue();
           Console.WriteLine("Dequeue: {0}",v);
           v = q.dequeue();
           Console.WriteLine("Dequeue: {0}",v);
           v = q.dequeue();
           Console.WriteLine("Dequeue: {0}",v);

       }



这是我的实现类




Here is My implementation class


public void enqueue(int newtail)
        {
            int value = 0;
            if (tail >= maxQueueSize)
            
                newtail = tail - head;
                adjustQueue();
                head = 0;
                tail = newtail;
            }
            if (newtail >= maxQueueSize)
            {
                
                maxQueueSize = maxQueueSize + 25;
                newArraySize(maxQueueSize);
                queueArray[tail] = value;
                tail++;
            }
            
            else
                queueArray[tail] = value;
            tail++;
        }
        
        public int dequeue()
        {
            int returnvalue = -1;
            
            if (head >= maxQueueSize)
            {
                returnvalue = -1;
                Console.WriteLine("Error...Queue is Empty, Cannot Dequeue!");
            }
            if (head >= tail)
            {
                returnvalue = -1;
                Console.WriteLine("Queue is Empty, Cannot Dequeue!");
                head++;
            }
            else
                returnvalue = queueArray[head];
            head++;
            return returnvalue;
        }
<pre>
        private void newArraySize(int newsize)
        {
           
            Array.Resize(ref queueArray, newsize);
        }
       
        public void adjustQueue()
        {
            
            int newhead = 0;
            int i = head;
            while (i < tail)
            {
                queueArray[newhead++] = queueArray[head++];
                i++;
            }
            head = 0;
        }




对代码转储感到抱歉!




sorry about the code dump!

推荐答案

自从我不得不编写队列处理以来已经很长时间了(特别是因为它们内置于.NET中),但是我记得最主要的事情是:

1)有一个输入"指示器和一个输出"指示器.
2)当输入" ==输出"时,队列为空.高级版本可以算数,但不是必须的.
3)否则,请将输入和输出分开.

It''s been a long time since I had to write queue handling, (especially since they are built into .NET) but the main things I remember made it a lot easier were:

1) Have an "input" indicator, and and "output" indicator.
2) When "input" == "output", the queue is empty. Advanced version can have a count, but it isn''t necessary.
3) Otherwise, keep input and output separate.

public class MyQueue<T>
    {
    private T[] data;
    private int input = 0;
    private int output = 0;

    public MyQueue(int size)
        {
        data = new T[size];
        }
    public void Enqueue(T item)
        {
        data[input++] = item;
        if (input >= data.Length)
            {
            input = 0;
            }
        }
    public T Dequeue()
        {
        if (input == output)
            {
            throw new ApplicationException("Buffer underrun");
            }
        T result = data[output++];
        if (output >= data.Length)
            {
            output = 0;
            }
        return result;
        }
    public bool IsEmpty()
        {
        return input == output;
        }
    }

请注意,这不会检查溢出!

Notice this doesn''t check for overrun!


这可能是一个愚蠢的问题,请问为什么您不只是使用框架中内置的Queue类呢?

此外,您的队列是特定于类型的.如果您使用object而不是int来存储项目,它将更加灵活.
This may be a stupid question, byut why don''t you just use the Queue class built into the framework?

Further, your queue is type-specific. If you use object instead of int to store the items, it would be a lot more flexible.


尽管我完全同意OriginalGriff和John Simmons的方法,但我还是尝试找出实现的问题:enqueue方法有一个小问题,要入队的值始终为0.这就是为什么总是出队0的原因.

一个快速的解决方案是像这样更改排队方法的第一行:int value = newtail;.如果进行此更改,则您的代码应该可以正常工作.您的头和尾巴索引还有另一个bug,但这是另一个主题.
Although I totally agree with OriginalGriff''s and John Simmons'' approaches, I tried to figure out what is wrong with your implementation: there is a small problem with your enqueue method, where value to be enqueued is always 0. That''s why you always dequeue 0''s.

One quick solution is to change the first line of your enqueue method like this: int value = newtail;. If you make this change your code should work. There is also another bug with your head and tail indices but this is another subject.


这篇关于实施队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆