Monday, 4 May 2015

How to use Queue in c# . NET ?

It represents a first-in, first out collection of object. It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue, and when you remove an item, it is called deque.Objects stored in a Queue are inserted at one end and removed from the other.
We can Enqueue (add) items in Queue and we can Dequeue (remove from Queue ) or we can Peek (that is we will get the reference of first item ) item from Queue

Operation on Queue:-


















Property of Queue:-






Source code example:-

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleAppDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            stackOperations classObj = new stackOperations();
            while (true)
            {
                // Console.Clear();
                Console.WriteLine("\n");
                Console.WriteLine("1. Add an element");
                Console.WriteLine("2. See the First element.");
                Console.WriteLine("3. Remove First element.");
                Console.WriteLine("4. Display Queue elements.");
                Console.WriteLine("5. Exit");
                Console.Write("Select your choice: ");
                int choice = Convert.ToInt32(Console.ReadLine());
                switch (choice)
                {
                    case 1:
                        classObj.Enqueue();
                        break;

                    case 2: classObj.Peek();
                        break;

                    case 3: classObj.Dequeue();
                        break;
                    case 4: classObj.Display();
                        break;

                    case 5: System.Environment.Exit(1);
                        break;
                }
                Console.ReadKey();
            }
        }
    }


    class stackOperations
    {
        string item;
        Queue qObj = new Queue();

        public bool isEmpty()
        {
            if (qObj.Count == 0) return true;

            return false;
        }

        public void Enqueue()
        {
            Console.Write("\nEnter a Item\n");
            item = Console.ReadLine();
            qObj.Enqueue(item);
            Console.WriteLine("\nItem Added successfully!");

        }

        public void Dequeue()
        {
            if (isEmpty())
            {
                Console.WriteLine("Queue is empty!");
            }
            else
            {
                qObj.Dequeue();
                Console.WriteLine("Removed Successfuly");
            }
        }
        public void Peek()
        {
            if (isEmpty())
            {
                Console.WriteLine("Queue is empty!");
            }
            else
            {
                Console.Write("First Item is: {0}", qObj.Peek());
            }
        }

        public void Display()
        {
            if (isEmpty())
            {
                Console.WriteLine("Queue is empty!");
            }
            else
            {
                Console.Write("\nItems in Queue are: \n");
                foreach (string item in qObj)
                {
                    Console.Write(item);
                    Console.Write("\n");
                }
            }
        }

    }
}


Output:-

Listing 1





































Listing 2





































Listing 3





































Listing 4

No comments:

Post a Comment