Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Queue in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 58
    Comment on it

    It is the first in first out collection of any object. It is used when you want FIFO type access of elements.

     

    When you insert item in the list it is called enqueue, when you remove item from the list it is called dequeue.

     

    Properties of Queue:

    Property Description
    Count Gets the number of elements contained in the Queue.

     

    Methods of Queue:

    Sr.No. Methods
    1 public virtual void Clear();

    Removes all elements from the Queue.

    2 public virtual bool Contains(object obj);

    Determines whether an element is in the Queue.

    3 public virtual object Dequeue();

    Removes and returns the object at the beginning of the Queue.

    4 public virtual void Enqueue(object obj);

    Adds an object to the end of the Queue.

    5 public virtual object[] ToArray();

    Copies the Queue to a new array.

    6 public virtual void TrimToSize();

    Sets the capacity to the actual number of elements in the Queue.

     

     

    Queue contains list of elements and we can insert and delete element from anywhere we want but we can access element in FIFO order.

     

     class Program
       {
          static void Main(string[] args)
          {
             Queue q = new Queue();
             
             q.Enqueue('Aman');
             q.Enqueue('Manoj');
             q.Enqueue('Gaurav');
             q.Enqueue('Suresh');
             
             Console.WriteLine("Current queue: ");
             foreach (char c in q) Console.Write(c + " ");
             
             Console.WriteLine();
             q.Enqueue('V');
             q.Enqueue('H');
             Console.WriteLine("Current queue: ");
             foreach (char c in q) Console.Write(c + " ");
             
             Console.WriteLine();
             Console.WriteLine("Removing some values ");
             char ch = (char)q.Dequeue();
             Console.WriteLine("The removed value: {0}", ch);
             ch = (char)q.Dequeue();
             Console.WriteLine("The removed value: {0}", ch);
             
             Console.ReadKey();
          }
       }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: