C queue example LEARNOVITA

Queue Collection in C# Tutorial | A Definitive Guide

Last updated on 16th Aug 2022, Blog, Tutorials

About author

Manobala ((C# Automation Tester )

Manobala is a C# Automation Tester and he has tons of experience in the areas of HTTP, SOAP/REST, VisualStudio, TFS, etc., CI/CD, functional, regression, and .NET technology stack. he spends most of his time researching technology, and startups.

(5.0) | 19678 Ratings 2091

C# Queue with Examples

In c#Queue is useful for representing a gathering of objects that saves elements in FIFO (First In, First Out) style, i.e

The element that is added first will come out first.

In a queue, elements are inserted from one end and removed from the other end.

Queues are useful when want to access elements from the collection in same order that is saved, And can store multiple null and duplicate values in a queue based on requirements.

Using Enqueue() and Dequeue() methods, can add or delete the element from the queue.

Here, the Enqueue() method is useful to add elements at end of the queue,

And the Dequeue() method is useful to remove elements from the queue.

Below is the pictorial representation of the queue process flow in the c# programming language.

C# Queue Declaration

Queue In C#

c# will support both generic and non-generic kinds of queues.

Non -generic queue collections by using System.Collections namespace.

The collection is the class.

To explain a queue, you need to declare an instance of the queue class before performing any operations like add, delete, etc. as shown below.

Queue que = new Queue();

If you observe the above queue declaration, generate a new queue (que) with an instance of a queue class without specifying any size.

C# Queue Properties

The following are the commonly used properties of a queue in the c# programming language.

    PropertyDescription
    Count It will return the total number of elements in queue
    IsSynchronized It is used to get a value to denote that access to the queue is synchronized (thread-safe) or not.

C# Queue Methods

    MethodDescription
    Enqueue It is used to add the elements at the end of the queue.
    Dequeue It will remove and return an item from the starting of the queue.
    Clear It will remove all elements from the queue.
    Clone It will generate a shallow copy of the queue.
    Contains It is used to find whether an element exists in a queue or not.
    Peek It is used to get the first element from a queue.
    TrimToSize It is used to set the capacity of the queue to an actual number of elements in the queue.

C# Queue Example

ill use a non-generic type of queue to add elements of various data types to the queue using Enqueue() method.

Below is the example of adding and accessing elements of the queue in c#.

  • using System;
  • using System.Collections;
  • namespace Tutlane
  • {
  • class Program
  • {
  • static void Main(string[] args)
  • {
  • // Create and initialize a queue
  • Queue que = new Queue();
  • que.Enqueue(“Welcome”);
  • que.Enqueue(“Tutlane”);
  • que.Enqueue(20.5f);
  • que.Enqueue(10);
  • que.Enqueue(100);
  • Console.WriteLine(“******Queue Example******”);
  • Console.WriteLine(“Number of Elements in Queue: {0}”, que.Count);
  • Console.WriteLine(“******Queue Elements******”);
  • // Access Queue Elements
  • foreach (var item in que)
  • {
  • Console.WriteLine(item);
  • }
  • Console.ReadLine();
  • }
  • }
  • }

If we observe the above example, we generated a new queue (que).

We added various data type elements to the queue (que) using Enqueue() method, and used a for each loop to iterate through a queue to get elements from it.

C# Dequeue() Method to Access Queue Elements

The queue Dequeue() method will always remove and return a first element of the queue.

C# Peek() Method to Access Queue Elements

The queue Peek() method will always return the first element of the queue.

C# Queue Contains() Method

In c#, by using the queue Contains() method, can check if an element exists in a queue or not.

If the element is discovered in the queue, it will return true; otherwise, it will return false.

C# – Queue Class:

It represents a first-in, first out collection of the object.

It is used when needing a first-in, first-out access to the items.

When adding an item in the list, it is called enqueue, and when removing an item, it is called deque.

Methods and Properties of the Queue Class

The below table lists some of the commonly used properties of the Queue class

    Sr.No Property & Description
    1 Count Gets the number of the elements contained in the Queue.

The below table lists some of the commonly used methods of the Queue class −

    Sr.No Property & Description
    1 public virtual void Clear(); Removes all the elements from the Queue.
    2 public virtual bool Contains(object obj); Find whether an element is in the Queue.
    3 public virtual object Dequeue(); Removes and returns an 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 the new array.
    6 public virtual void TrimToSize(); Sets the capacity to the actual number of elements in the Queue.

Types of Queues:

Types of Queues

Simple Queue:

It is also known as a linear queue is the most basic version of a queue.

Insertion of an element that is. The Enqueue operation takes place at the rear end and removal of an element ,that is the Dequeue operation takes place at the front end.

Circular Queue:

In a circular queue, the element of a queue acts as a circular ring.

The working of a circular queue is the same as the linear queue except for the fact that the last element is connected to the first element.

Its advantage is that the memory is utilized in a good way.

This is due to the fact that if there is an empty space, i.e. if no element is present at a specific point in the queue, an element may simply be inserted at that place.

Priority Queue:

This queue is a very special type of queue.

Its specialty is that it assembles the elements in a queue based on some priority.

The priority can be something where the element with the highest value has the priority so it makes a queue with decreasing order of values.

The priority can also be such that the element with the lowest value gets the highest priority so in turn it makes a queue with increasing order of values.

Dequeue:

Dequeue is also known as the Double Ended Queue.

Double ended, it means that an element can be inserted or removed from both the ends of the queue unlike the other queues in which it can be done only from the one end.

Because of this property it does not obey the First In First Out property.

Implementation of Queue:

Sequential allocation:

  • An array can be used to implement a queue.
  • It can organize a limited number of the elements.

Linked list allocation:

  • A queue can be implemented by using a linked list.
  • It can organize an unlimited number of the elements.

Applications of Queue:

Multi programming:

  • Multiprogramming is when multiple programs are running in the main memory.
  • It is needed to organize these multiple programs and these multiple programs are organized as queues.

Network:

  • In a network, a queue is used in devices like a router or a switch.
  • Another application of a queue is a mail queue which is a directory that saves data and controls files for mail messages.

Job Scheduling:

  • The computer has a task to execute a specific number of jobs that are scheduled to be executed one after another.
  • These jobs are assigned to the processor one by one which is organized by using a queue.

Shared resources:

  • Queues are used as waiting lists for the single shared resource.

Real-time application of Queue:

  • ATM Booth Line
  • Ticket Counter Line
  • Key press sequence on keyboard
  • CPU task scheduling
  • Waiting time of every customer at call centers.

Advantages of Queue:

Advantages Of Queue
  • It is simple and effective to handle bigger amounts of data.
  • Because it adheres to the first in first out principle, operations like insertion and deletion may be carried out without difficulty.
  • When several customers utilize a single service, queues are helpful.
  • When communicating data across processes, queues communicate quickly.
  • The implementation of the other data structures may make use of queues.
  • Disadvantages of Queue:

    • The operations like insertion and deletion of elements from the middle are time consuming. Limited Space.
    • In a classical queue, a new element can only be inserted when the existing elements are deleted from a queue.
    • Searching for an element takes O(N) the time.
    • Maximum size of the queue must be defined prior.

    Are you looking training with Right Jobs?

    Contact Us

    Popular Courses