Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C#
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!
Posted

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!
 
Share this answer
 
Comments
CPallini 24-Nov-10 15:41pm    
Why didn't you check for overrun?
OriginalGriff 24-Nov-10 15:42pm    
Because I didn't want to do ALL his homework for him! Leave some work that shows he understands what I gave him...
Tanacia 24-Nov-10 16:16pm    
Dont worry, I have a WHOLE Lot more homework to do. Just trying to check this off the list. Im tickled you think Ima guy! lol :)
Tanacia 24-Nov-10 16:22pm    
Actually, I just finished Queue class which I had no problem with. Funny thing is My MyQueue looked almost exactly like this one! Makes me happy.
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.
 
Share this answer
 
Comments
OriginalGriff 24-Nov-10 15:37pm    
John, that's a bit retrograde - like recommending an ArrayList instead of a List<t>. Flexible, but not type safe, plus you have to cast it before you can use it...
OriginalGriff 24-Nov-10 15:41pm    
I don't believe it. I've tried to put the damn greater than and less than back twice now, two different ways. I guess I'll have to live with it - you know what I mean, anyway...
Tanacia 24-Nov-10 16:18pm    
Not a stupid question...I didnt do it that way because my instructor wanted it this way. I thought I would change some things and he had me change it back. BUT thanks.
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.
 
Share this answer
 
Comments
Tanacia 24-Nov-10 16:20pm    
Rock on dmageiras! That did it.
Im curious about the other bug in my program! What and where is it? I love feedback!
dmageiras 24-Nov-10 16:31pm    
Code does not dequeue correctly: if you enqueue (1,2,3,4), by dequeuing you get (1,0,2,3,4,-1,-1,...). I guess there is something wrong with your enqueue method, especially when you enqueue an int for the first time and you adjust your head, tail indices.
Tanacia 24-Nov-10 16:35pm    
I noticed that too. After looking at it I was able to get it working correctly. Thanks!
Tanacia 24-Nov-10 16:42pm    
Sorry, instead of incrementing tail i incremented newtail in the 2nd if statement for queueArray. That took care of the issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900