---
description: First In First Out (FIFO)
---

# Queue

Elements are always added to the back and removed from the front.&#x20;

* **Example**: A line of people waiting for a bus. The person who is at the beginning of the line is the first one to enter the bus.

{% embed url="https://1.bp.blogspot.com/-N-v_FiIdQXM/XlkFCQQYtPI/AAAAAAAAHR0/zxkuX6WfQS8Y8Mkoj1nHZDWtMOD3MjsUwCLcBGAsYHQ/s1600/0_E33E-AjyAUTFjVmM.gif" %}



* **Enqueue -** If the queue is not full, this function adds an element to the back of the queue, else it prints “**OverFlow**”.
* **Dequeue -** If the queue is not empty, this function removes the element from the front of the queue, else it prints “**UnderFlow**”.

<details>

<summary>Queue : Example</summary>

* Non-Generic Queue

```csharp
using System;
using System.Collections;

public class NonGenericQueueExample {
    public static void Main() {
        Console.WriteLine("Non-Generic Queue Example\n");
        var queue = new Queue();
        queue.Enqueue("Hello,");
        queue.Enqueue("World!");
	Console.WriteLine("Items in Non-Generic Queue:");
	foreach (var item in queue) {
	    Console.WriteLine(item);
	}
    }
}
```

* Generic Queue

```csharp
using System;
using System.Collections.Generic;

public class GenericQueueExample {
    public static void Main() {
    	Console.WriteLine("Generic Queue Example\n");
	var gQueue = new Queue<string>();
	gQueue.Enqueue("One");
	gQueue.Enqueue("Two");
	gQueue.Enqueue("Three");
	gQueue.Enqueue("Four");
	gQueue.Enqueue("Five");
	Console.WriteLine("Items in Generic Queue:");
	foreach (var item in gQueue) {
		Console.WriteLine("- " + item);
	}

	Console.WriteLine("Count: " + gQueue.Count);
	Console.WriteLine("Contains(): " + gQueue.Contains("One"));
	Console.WriteLine("Dequeue: " + gQueue.Dequeue());
	Console.WriteLine("Peek(): " + gQueue.Peek());
	Console.WriteLine("Copy of gQueue");
	var cgQueue = new Queue<string>(gQueue.ToArray());
	foreach (var item in cgQueue) {
		Console.WriteLine("- " + item);
	}

	Console.WriteLine("Copy of gQueue");
	string[] array = new string[gQueue.Count * 2];
	gQueue.CopyTo(array, gQueue.Count);
	var cgQueue2 = new Queue<string>(array);
	foreach (var cItem in cgQueue2)	{
		Console.WriteLine("- " + cItem + " ");
	}
	Console.WriteLine("Clear(): ");
	cgQueue.Clear();
	Console.WriteLine("Count: " + cgQueue.Count);
    }
}
```

</details>

## Double-ended queue

* In a double-ended queue, characters can be inserted and deleted from both the front and back of the queue.

## Circular queue

* A circular queue is an improvement over the standard queue structure. In a standard queue, when an element is deleted, the vacant space is not reutilized. However, in a circular queue, vacant spaces are reutilized.
* While inserting elements, when you reach the end of an array and you need to insert another element, you must insert that element at the beginning (given that the first element has been deleted and the space is vacant).
