FIFO in C#
In our daily lives, we often encounter terms or acronyms, which are generally short forms for other larger meanings, and we are given these acronyms to make more efficient use of them.
FIFO is a term that stands for first in, first out. Let’s understand this with a clearer perspective. Suppose we are waiting at the coffee shop to place our order. In this case, the person who arrived earlier before us in the coffee shop and waited in the queue will get their order first and will be the one who gets to leave first. In the computing world, FIFO works similarly, keeping things in the same pattern and position as they came in. This unique arrangement and pattern helps them maintain order and ensure that the things or data are predictable and sane. In a programming world like ours, there always exist some sort of patterns and positions where the order of the tasks matters a lot, and in those situations, we use the basic methods of the FIFO implementation. It really helps all the developers to manage the data in which they were created or received.
Understanding the Concept
FIFO, as the name suggests, first one first out, is very similar to the queue data structure in this concept, we use a mere analogy where we simply process or work on the data that has arrived first. We work in the order of queue and process them in a given order. Please think of this method as very similar to standing in a queue at a railway ticket station where we have to maintain a line and ensure that the person who has arrived gets the ticket first. So, following this analogy, we process the rest of the data.
Approach
When we talk about the approach used while accessing or implementing FIFO, we generally use a series of steps in FIFO where we first add any element in the queue, which simply means that the elements will be added in the rear, and the elements will be removed from the front. The main operations which we will be performing in the FIFO analogy are: -
1. Enqueue
This is one of the main operations in the FIFO concept, where we have to add any given element at the queue's rear end and ensure that this element's pointer points out the newly added element.
2. Dequeue
This is also considered one of the main operations of the FIFO, where we have to remove any given element from the front end of the queue without actually disturbing the flow of the other elements in the queue.
3. Peek
We can also use another operation, known as the peek operation, that determines or examines the element at the front without having to remove it.
Applications
In this section of the tutorial, we will examine some of the applications and uses of the FIFO algorithm in real-world problems. Let's look at them one by one: -
a. Operating Systems
In operating systems and also in CPUs, we have often encountered the term known as process scheduling, in which processes are organized in the order of their arrival. The process that was initiated first will be executed first, and it will get the resources first as well.
b. Networking
As we all know, in networking, there's a term called network queuing. In this, the data travels through the networks and the routers, and the data or chunks of packets that we have received are generally sent over the network in the same order in which they were received.
c. Data Structures
FIFO generally serves as the basis when we talk about the queue data structure and want to implement them in real life. The FIFO remains crucial for tree traversal, recursion, job scheduling, and other important tasks.
d. Hardware Design
FIFO is a concept that is mainly applied in hardware designs to understand how the different components of the system work when they are coordinated together? It is basically done to see the functionalities and overall performance of the FIFO in real life.
e. Traffic Management
In the real world, FIFO is mostly implemented at traffic signals or toll booths to make the vehicles stand in a queue, and then they are also allowed to move in an orderly manner. If we don't follow this, there will be a massacre.
Examples
using System;
using System.Collections.Generic;
public class FIFOQueue<T>
{
private Queue<T> queue;
public FIFOQueue()
{
queue = new Queue<T>();
}
// Method to enqueue an item into the queue
public void Enqueue(T item)
{
queue.Enqueue(item);
}
// Method to dequeue an item from the queue
public T Dequeue()
{
if (queue.Count == 0)
{
throw new InvalidOperationException("The queue is empty.");
}
return queue.Dequeue();
}
// Method to peek at the first item in the queue without removing it
public T Peek()
{
if (queue.Count == 0)
{
throw new InvalidOperationException("The queue is empty.");
}
return queue.Peek();
}
// Method to check if the queue is empty
public bool IsEmpty()
{
return queue.Count == 0;
}
// Method to get the number of elements in the queue
public int Count()
{
return queue.Count;
}
}
class Program
{
static void Main(string[] args)
{
FIFOQueue<int> fifo = new FIFOQueue<int>();
// Enqueue some elements
fifo.Enqueue(243);
fifo.Enqueue(200);
fifo.Enqueue(346);
// Dequeue elements and print them
Console.WriteLine("Dequeued items:");
while (!fifo.IsEmpty())
{
Console.WriteLine(fifo.Dequeue());
}
}
}
Output:
![FIFO in C#/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p><strong>Example 2)</strong></p>
<!-- /wp:paragraph -->
<!-- wp:preformatted -->
<pre class=](https://static.tutorialandexample.com/csharp/fifo-in-csharp1.png)
public class Node<T>
{
public T Data { get; set; }
public Node<T> Next { get; set; }
public Node(T data)
{
Data = data;
Next = null;
}
}
public class FIFOQueue<T>
{
private Node<T> front;
private Node<T> rear;
public FIFOQueue()
{
front = null;
rear = null;
}
public void Enqueue(T data)
{
Node<T> newNode = new Node<T>(data);
if (rear == null)
{
front = newNode;
rear = newNode;
}
else
{
rear.Next = newNode;
rear = newNode;
}
}
public T Dequeue()
{
if (front == null)
{
throw new InvalidOperationException("The queue is empty.");
}
T data = front.Data;
front = front.Next;
if (front == null)
{
rear = null;
}
return data;
}
public T Peek()
{
if (front == null)
{
throw new InvalidOperationException("The queue is empty.");
}
return front.Data;
}
public bool IsEmpty()
{
return front == null;
}
public int Count()
{
int count = 0;
Node<T> current = front;
while (current != null)
{
count++;
current = current.Next;
}
return count;
}
}
class Program
{
static void Main(string[] args)
{
FIFOQueue<int> fifo = new FIFOQueue<int>();
// Enqueue some elements
fifo.Enqueue(5666);
fifo.Enqueue(00);
fifo.Enqueue(455);
// Dequeue elements and print them
Console.WriteLine("Dequeued items:");
while (!fifo.IsEmpty())
{
Console.WriteLine(fifo.Dequeue());
}
}
}
Output:
![FIFO in C#/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p><strong>Example 3)</strong></p>
<!-- /wp:paragraph -->
<!-- wp:preformatted -->
<pre class=](https://static.tutorialandexample.com/csharp/fifo-in-csharp2.png)
using System.Collections;
using System.Collections.Generic;
public class FIFOQueue<T> : IEnumerable<T>
{
private LinkedList<T> items;
public FIFOQueue()
{
items = new LinkedList<T>();
}
public int Count => items.Count;
public void Enqueue(T item)
{
items.AddLast(item);
}
public T Dequeue()
{
if (items.Count == 0)
{
throw new InvalidOperationException("The queue is empty.");
}
T item = items.First.Value;
items.RemoveFirst();
return item;
}
public T Peek()
{
if (items.Count == 0)
{
throw new InvalidOperationException("The queue is empty.");
}
return items.First.Value;
}
public bool Contains(T item)
{
return items.Contains(item);
}
public void Clear()
{
items.Clear();
}
public IEnumerator<T> GetEnumerator()
{
foreach (var item in items)
{
yield return item;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
class Program
{
static void Main(string[] args)
{
FIFOQueue<int> fifo = new FIFOQueue<int>();
// Enqueue some elements
fifo.Enqueue(10);
fifo.Enqueue(20);
fifo.Enqueue(30);
// Dequeue elements and print them
Console.WriteLine("Dequeued items:");
while (fifo.Count > 0)
{
Console.WriteLine(fifo.Dequeue());
}
// Check if queue is empty
Console.WriteLine("Queue is empty: " + (fifo.Count == 0));
// Enqueue more elements
fifo.Enqueue(40);
fifo.Enqueue(50);
// Peek at the first item
Console.WriteLine("Peeked item: " + fifo.Peek());
// Check if an item exists in the queue
Console.WriteLine("Does the queue contain 50? " + fifo.Contains(50));
// Iterate through the queue
Console.WriteLine("Iterating through the queue:");
foreach (var item in fifo)
{
Console.WriteLine(item);
}
// Clear the queue
fifo.Clear();
// Check if queue is empty after clearing
Console.WriteLine("Queue is empty after clearing: " + (fifo.Count == 0));
}
}
Output:
![FIFO in C#/>
<!-- /wp:html -->
<!-- wp:heading -->
<h2 class=](https://static.tutorialandexample.com/csharp/fifo-in-csharp3.png)
FIFO stands for first in, first out element, is a fundamental approach when it comes to dealing with elements in a system. When we try and follow the FIFO approach, we adhere to the fairness, predictable and efficient use of the method and the order-wise processing of the data. FIFO has made their place in every technological domain, proving its widespread significance, whether it is an operating system or any other technology such as networking or traffic control. Understanding the concept and features of FIFO not only helps us extend our knowledge but also improves the algorithm design in software development.