Hi,

I need to develop a C# console application which implements stack as a circular queue. I've written the code to use stack as a simple queue.

using System;

public class Stack
{
    private int[] theArray;
    private int topOfStack;

	public Stack()
	{
        theArray = new int[10];
        topOfStack = -1;
	}

    public bool isEmpty()
    {
        return topOfStack == -1;
    }

	public void push(int data)
	{
        if (topOfStack < 10)
        {
            topOfStack++;
            theArray[topOfStack] = data;
        }
	}

	public int pop()
	{
        if (!isEmpty())
            return theArray[topOfStack--];
        else
            return -1;
	}

    public void display(int flag)
    {
        if(flag == 0) //display outStack
            for (int i = topOfStack; i >= 0; i--)
                System.Console.Write(theArray[i] + "\t");

        if (flag == 1) //display inStack
            for (int i = 0; i <= topOfStack; i++)
                System.Console.Write(theArray[i] + "\t");
    }
}

public class StackQueue
{
	public static void Main(string[] args)
	{
		int choice;
		Stack inStack = new Stack();
        Stack outStack = new Stack();
		while(true)
		{
			System.Console.WriteLine("\n1. Add\n2. Delete\n3. View\n4. Exit");
			System.Console.Write("\nEnter your choice: ");
			choice = int.Parse(System.Console.ReadLine());
			switch(choice)
			{
				case 1: System.Console.Write("\nEnter element to add: ");
					    int element = int.Parse(System.Console.ReadLine());
					    inStack.push(element);
					    break;

				case 2: if(outStack.isEmpty())
                            while(!inStack.isEmpty())
                                outStack.push(inStack.pop());
                        if ((element = outStack.pop()) != -1)
                            System.Console.WriteLine("\nElement deleted: " + element);
                        else
                            System.Console.WriteLine("\nQueue is empty");
					    break;

                case 3: if (outStack.isEmpty() && inStack.isEmpty())
                            System.Console.WriteLine("Queue is empty");
                        if (!outStack.isEmpty())
                            outStack.display(0);
                        if (!inStack.isEmpty())
                            inStack.display(1);
					    break;

				case 4: System.Environment.Exit(0);
					    break;

				default: System.Console.WriteLine("Wrong Choice Entered\n");
			    		 break;
			}
		}
	}
}

I'm not able to devise algorithm for using stack as circular queue? Can anyone guide me to develop algorithm for using stack as a circular queue? The program may contain push(), pop() methods with topOfStack as a int variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.