Trying to make a circular queue, but its not working right. Enqueue is just adding things to the end, every time.

my output is:
Numbers in Queue: 1 2 3
Numbers in Queue: 2 3
Numbers in Queue: 2 3 6

it should be:
Numbers in Queue: 1 2 3
Numbers in Queue: 2 3
Numbers in Queue: 6 2 3

any suggestions?

using System;

namespace CircularArrayQueue
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                QueueStuff queue = new QueueStuff();


                queue.Enqueue(1);
                queue.Enqueue(2);
                queue.Enqueue(3);
                
                queue.PrintQueue();

                queue.Dequeue();
                queue.PrintQueue();
                queue.Enqueue(6);
                queue.PrintQueue();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }


        }
    }
}
class QueueStuff
{
    int head = 0;
    int tail = 0;
    
    object[] Qlist = new object[5];

    public object Enqueue(object insert)
    {
        if (Full())
        {
            throw new Exception("Queue is full, cannot add more");
        }
        else
        {
            Qlist[tail] = insert;
            tail = count(tail);
            return true;
        }

       
    }

    public void Dequeue()
    {
        if (Empty())
        {
            throw new Exception("Queue is Empty, can not Dequeue");
        }
        else
        {
            object toDelete;
            toDelete = Qlist[head];
            Qlist[head] = null;

            head = count(head);
        }
    }

    public void PrintQueue()
    {
        Console.Write("Queue Contains: ");

        if (Empty())
        {
            Console.WriteLine("Queue is Empty.");
        }

        for (int i = 0; i < tail; i++)
        {
            Console.Write(Qlist[i]);
            Console.Write(" ");
        }
        Console.WriteLine("\n");
    }

    public bool Empty()
    {
        return (head == tail);
    }

    public int count(int x)
    {
        x++;
        return (x);
    }

    public bool Full()
    {      
        return (tail == 5);
    }

    public void empty()
    {
        tail = head = 0;
    }
}

Trying to make a circular queue, but its not working right. Enqueue is just adding things to the end, every time.

my output is:
Numbers in Queue: 1 2 3
Numbers in Queue: 2 3
Numbers in Queue: 2 3 6

it should be:
Numbers in Queue: 1 2 3
Numbers in Queue: 2 3
Numbers in Queue: 6 2 3

any suggestions?

That's the way your queue is supposed to work -- its capacity is 5, not 3.

My main suggestions have to do with coding style. Don't have the return type be object if you only return a bool; return void if you're going to return the same bool every time; don't name separate functions Empty and empty because that's retarded; use capital names for all your functions because that's C# style; use lowercase names for private variables because that's C# style; whatever naming scheme you use, be consistent because that's convenient; in functions, avoid declaring variables without initializing them if you have no reason to do so, it just makes the code take more brain capacity to read that way; don't write x++; return (x); , just write return x + 1; ; don't write tail == 5 , write tail == Qlist.Length , because your code is more resilient that way; name your function "IsEmpty" instead of "Empty" because that's more clear, and rename "empty" to "Clear" because that's more Clear; have your function "empty" make it so that every element of qList is null, because that's consistent with the way your Dequeue function behaves; don't assign to variables and then never use them, the way you do with toDelete.

This article has been dead for over six months. Start a new discussion instead.