C# Coding of Data Structures for Stack, Queue and Circular Queue

 C# Coding of Data Structures for Stack, Queue and Circular Queue

Stack

A Stack is an ordered collection of items into which new items may be inserted and from which item may deleted one end called the top of the stack.

namespace DataStructure

{

    public struct Stack<T>

    {

        public T Top;

        public T[] Items;

    }

    public class StackStructure

    {

        public void Demo()

        {

            var items = Convert.ToInt16(Console.ReadLine());

            for (int i = 0; i < items; i++)

            {

                Console.Write("Enter the " + i.ToString() + " Value :");

                Push(Convert.ToInt16(Console.ReadLine()));

            }

            for (int i = 0; i < items; i++)

            {

                Console.WriteLine("poped the " + i.ToString() + " Value :" + Pop());

                Console.ReadLine();

            }

            Console.ReadLine();

        }

        public StackStructure(int value)

        {

            stack.Top = -1;

            stack.Items = new int[value];

        }

        Stack<int> stack = new Stack<int>();

        public int Pop()

        {

            stack.Top--;

            if (stack.Items.Length <= 0)

            {

                Console.WriteLine("The Stack is Empty");

            }

            return stack.Items[stack.Top + 1];

        }

        public void Push(int item)

        {

            stack.Top++;

            stack.Items[stack.Top] = item;

        }

    }

}

Queue: It is similar to stack but inserted in one end and remove from other end

namespace DataStructure

{

    public struct Queue

    {

        public int Front;

        public int Rear;

        public int[] items;

    }

    public class QueueStructure

    {

        Queue queue = new Queue();

        public QueueStructure(int value)

        {

            queue.items = new int[value];

        }

        public void Demo()

        {

            queue.Front = 0;

            queue.Rear = -1;

            var items = Convert.ToInt16(Console.ReadLine());

            for (int i = 0; i < queue.items.Length; i++)

            {

                Console.Write("Enter the " + i.ToString() + " Value :");

                Insert(Convert.ToInt16(Console.ReadLine()));

            }

            Display();

            Delete();

            Display();

            Delete();

            Display();

            Delete();

            Display();

            Console.ReadLine();

        }

        public void Display()

        {

            Console.WriteLine("The items are");

            if (queue.Rear < queue.Front)

            {

                Console.WriteLine("Queue is Empty");

            }

            else

            {

                for (int i = queue.Front; i <= queue.Rear; i++)

                {

                    Console.WriteLine(queue.items[i]);

                }

            }

        }

 

        public int Delete()

        {

 

            if (queue.Front > queue.Rear)

                Console.WriteLine("Queue is empty");

            else

                queue.Front++;

            return -1;

        }

 

        public void Insert(int value)

        {

            queue.Rear++;

            queue.items[queue.Rear] = value;

        }

    }

 

Circular Queue: As same as the queue but in circular structure.

 

namespace DataStructure

{

    public struct CircularQueue

    {

        public int Front;

        public int Rear;

        public int[] items;

    }

 

    public class CircularQueueStructure

    {

        CircularQueue queue = new CircularQueue();

        public CircularQueueStructure(int value)

        {

            queue.items = new int[value];

        }

 

        public void Demo()

        {

            queue.Front = -1;

            queue.Rear = -1;

            var items = Convert.ToInt16(Console.ReadLine());

            for (int i = 0; i <= queue.items.Length; i++)

            {

                Console.Write("Enter the " + i.ToString() + " Value :");

                Insert(i);

            }

 

            Display();

            Delete();

            Insert(4);

            Display();

            Insert(4);

            Delete();

            Delete();

            Insert(4);

            Display();

            Insert(5);

            Display();

            Insert(1);

            Console.ReadLine();

        }

 

        public void Display()

        {

            Console.WriteLine("The items are");

            {

                for (int i = 0; i < queue.items.Length; i++)

                {

                    Console.WriteLine(queue.items[i]);

                }

            }

        }

 

        public int Delete()

        {

            queue.Front++;

            return -1;

        }

 

        public void Insert(int value)

        {

            if (((queue.Rear + 1) % 5) == queue.Front)

                Console.WriteLine("queue is full");

            else

            {

                queue.Rear++;

                queue.Rear = (queue.Rear) % 5;

                queue.items[queue.Rear] = value;

            }

            if (queue.Front == -1)

                queue.Front = 0;

        }

    }

}

Ebook Download
View all
Learn
View all