Queue And Stack Collection In C#

Generic Queue Collection Class

A Queue is a First in First Out (FIFO) collection class in the System.Collections.Generic namespace.

Let's have a look at the following example.

Create a new console application and add a class “Employee” with two auto-implemented properties.

class Employee  
{  
   public int Id { get; set; }  
   public string Name { get; set; }  
}

In the same class file, create another class “MainProgram” and create a Main method.

class Program  
{  
    static void Main(string[] args)  
    {
    }  
} 

In this Main method create three objects of the Employee class.

static void Main(string[] args)  
{  
    Employee employeeOne = new Employee()  
    {  
        Id = 1,  
        Name = "Aiden"  
    };  
  
    Employee employeeTwo = new Employee()   
    {  
        Id = 2,   
        Name = "Jack"  
    };  
  
    Employee employeeThree = new Employee()  
    {  
        Id = 3,  
        Name = "Tom Clancy"  
    };
} 

To create a List of Employees, we say:

List<Employee> employeeList = new List<Employee>(); 

Where Employee is the type.

Just like that, to create an Employee queue collection object, we say:

Queue<Employee> employeeQueue = new Queue<Employee>(); 

Enqueue Method

To add items to a list we use the Add method and to add items to a queue, we use the Enqueue method.

Queue And Stack Collection Classes In C#

This Enqueue method adds objects to the end of the collection queue one after another. Look at the type of item this enqueue parameter expects, an item/object of type Employee.

Queue<Employee> employeeQueue = new Queue<Employee>();  
employeeQueue.Enqueue(employeeOne);  
employeeQueue.Enqueue(employeeTwo);  
employeeQueue.Enqueue(employeeThree); 

Dequeue Method

To add an item, we use the Enqueue method and to return an item we use Dequeue method.

 Queue And Stack Collection Classes In C#

But this dequeue method does not just return an item, it removes the item from the queue and returns it back at the beginning.

int itemsBeforeDequeue = employeeQueue.Count;  
Console.WriteLine("Items before Dequeue {0}",itemsBeforeDequeue);  
	  
Employee empOne = employeeQueue.Dequeue();  
Console.WriteLine(empOne.Id+" "+empOne.Name);  
	  
int itemsAfterDequeue = employeeQueue.Count;  
Console.WriteLine("Items after Dequeue {0}",itemsAfterDequeue); 

Run the application.

Queue And Stack Collection Classes In C# 

In the output, before we invoke the Dequeue method, the total items was 3 but after the Dequeue method, the total items is 2. Just like that if we again invoke the Dequeue method it will return the second employee object back because now the second employee object is in the first position of the queue.

Employee empTwo = employeeQueue.Dequeue();  
Console.WriteLine(empTwo.Id + " " + empTwo.Name);  
	  
int itemsAfterFirstDequeue = employeeQueue.Count;  
Console.WriteLine("Items after second Dequeue {0}", itemsAfterFirstDequeue); 

Run the application.

Queue And Stack Collection Classes In C# 


So, now there is just one item left in the queue collection.

Foreach loop

To retrieve all the items from a list collection, we use a foreach loop and just like that to retrieve all the items from a queue collection, we can use a foreach loop.

NOTE

This foreach loop just loops thru each item in a collection, but it will not remove or dequeue any of the items/objects.

foreach(Employee emp in employeeQueue)  
{  
    Console.WriteLine(emp.Id + " "+emp.Name);  
} 

Queue And Stack Collection Classes In C# 

Peek

To remove and return an item at the beginning we use the Dequeue method but if we want to retrieve or return an item at the beginning without removing it, we can use the Peek method.

int itemBeforePeek = employeeQueue.Count();  
Console.WriteLine("Items before Peek {0}", employeeQueue.Count);  
	  
Employee employeePeek = employeeQueue.Peek();  
Console.WriteLine(employeePeek.Name);  
	  
int itemAfterPeek = employeeQueue.Count();  
Console.WriteLine("Items After Peek {0}", employeeQueue.Count); 

Run the application.

Queue And Stack Collection Classes In C# 

The following is a code snippet for the Main method.

static void Main(string[] args)  
{  
    Employee employeeOne = new Employee()  
    {  
        Id = 1,  
        Name = "Aiden"  
    };  
  
    Employee employeeTwo = new Employee()  
    {  
        Id = 2,  
        Name = "Jack"  
    };  
  
    Employee employeeThree = new Employee()  
    {  
        Id = 3,  
        Name = "Tom Clancy"  
    };  
  
    Queue<Employee> employeeQueue = new Queue<Employee>();  
    employeeQueue.Enqueue(employeeOne);  
    employeeQueue.Enqueue(employeeTwo);  
    employeeQueue.Enqueue(employeeThree);  
 
    #region Dequeue method      
    int itemsBeforeDequeue = employeeQueue.Count;  
    Console.WriteLine("Items before Dequeue {0}", itemsBeforeDequeue);  
  
    Employee empOne = employeeQueue.Dequeue();  
    Console.WriteLine(empOne.Id + " " + empOne.Name);  
  
    int itemsAfterDequeue = employeeQueue.Count;  
    Console.WriteLine("Items after first Dequeue {0}", itemsAfterDequeue);  
  
    Employee empTwo = employeeQueue.Dequeue();  
    Console.WriteLine(empTwo.Id + " " + empTwo.Name);  
  
    int itemsAfterFirstDequeue = employeeQueue.Count;  
    Console.WriteLine("Items after second Dequeue {0}", itemsAfterFirstDequeue);  
 
    #endregion  
 
    #region foreach loop      
    foreach (Employee emp in employeeQueue)  
    {  
        Console.WriteLine(emp.Id + " " + emp.Name);  
    }  
 
    #endregion  
 
    #region peek method      
    int itemBeforePeek = employeeQueue.Count();  
    Console.WriteLine("Items before Peek {0}", employeeQueue.Count);  
  
    Employee employeePeek = employeeQueue.Peek();  
    Console.WriteLine(employeePeek.Name);  
  
    int itemAfterPeek = employeeQueue.Count();  
    Console.WriteLine("Items After Peek {0}", employeeQueue.Count);  
    #endregion  
}

Generic Stack Collection Class

A stack is a Last In First Out (LIFO) collection class present in the System.Collection.Generic namespace.

Let's have a look at the following example.

Create a new console application and add a class “Employee” with two auto-implemented properties.

class Employee  
{  
    public int Id { get; set; }  
    public string Name { get; set; }  
}  
  
//In the same class file, create another class “MainProgram” and create a Main method .      
  
class Program  
{  
    static void Main(string[] args)  
    {  
  
    }  
}  
  
//In this Main method create three different objects of Employee class.     
  
static void Main(string[] args)  
{  
    Employee employeeOne = new Employee()  
    {  
        Id = 1,  
        Name = "Aiden"  
    };  
  
    Employee employeeTwo = new Employee()  
    {  
        Id = 2,  
        Name = "Jack"  
    };  
  
    Employee employeeThree = new Employee()  
    {  
        Id = 3,  
        Name = "Tom Clancy"  
    }; 
}

To create an Employee stack collection object, we say:

Stack<Employee> EmployeeStack = new Stack<Employee>(); 

Push Method

To add items to a stack, we use the Push method.

Queue And Stack Collection Classes In C#

This Push method adds objects to the top of the collection, in other words one over another. Look at the type of item this Push parameter expects, an item/object of type Employee.

Stack<Employee> EmployeeStack = new Stack<Employee>();  
EmployeeStack.Push(employeeOne);  
EmployeeStack.Push(employeeTwo);  
EmployeeStack.Push(employeeThree); 

Pop Method

To add an item, we use the Push method and to return an item we use the Pop method.

Queue And Stack Collection Classes In C# 

But this Pop method does not just return an item. It removes the item from the stack collection and returns it back at the top.

int itemsBeforeStack = EmployeeStack.Count;  
Console.WriteLine("Items before Dequeue {0}", itemsBeforeStack);  
	  
Employee empOne = EmployeeStack.Pop();  
Console.WriteLine(empOne.Id + " " + empOne.Name);  
	  
int itemsAfterStack = EmployeeStack.Count;  
Console.WriteLine("Items after Dequeue {0}", itemsAfterStack); 

Run the application.

Queue And Stack Collection Classes In C# 

In the output, before we invoked the Pop method, the total item was 3 but after the Pop method, the total item is 2 and the last item is removed and returned first, in other words Last In First Out.

Foreach loop

To retrieve all the items from a list collection, we use a foreach loop and just like that to retrieve all the items from a stack collection, we can use a foreach loop.

NOTE

This foreach loop just loops thru each item in a collection, but it will not remove any of the items/objects.

foreach(Employee employee in EmployeeStack) {  
   Console.WriteLine(employee.Id + " "+employee.Name);  
} 

Queue And Stack Collection Classes In C# 

Peek

To remove and return an item at the beginning we use the Pop method but if we want to retrieve or return an item at the beginning without removing it, we can use the Peek method.

int itemBeforePeek = EmployeeStack.Count();  
Console.WriteLine("Items before Peek {0}", EmployeeStack.Count);  
	  
Employee employeePeek = EmployeeStack.Peek();  
Console.WriteLine(employeePeek.Name);  
	  
int itemAfterPeek = EmployeeStack.Count();  
Console.WriteLine("Items After Peek {0}", EmployeeStack.Count); 

Run the application.

Queue And Stack Collection Classes In C# 

Code snippet for the Main method.

static void Main(string[] args) {  
   Employee employeeOne = new Employee() {  
      Id = 1,  
      Name = "Aiden"  
};  
  
Employee employeeTwo = new Employee() {  
   Id = 2,  
   Name = "Jack"  
};  
  
Employee employeeThree = new Employee() {  
   Id = 3,  
   Name = "Tom Clancy"  
};  
  
Stack<Employee> EmployeeStack = new Stack<Employee>();  
EmployeeStack.Push(employeeOne);  
EmployeeStack.Push(employeeTwo);  
EmployeeStack.Push(employeeThree);  
 
#region Pop Method  
int itemsBeforeStack = EmployeeStack.Count;  
Console.WriteLine("Items before Dequeue {0}", itemsBeforeStack);  
  
Employee empOne = EmployeeStack.Pop();  
Console.WriteLine(empOne.Id + " " + empOne.Name);  
  
int itemsAfterStack = EmployeeStack.Count;  
Console.WriteLine("Items after Dequeue {0}", itemsAfterStack);  
#endregion  
 
 
#region Foreach loop  
foreach(Employee employee in EmployeeStack) {  
   Console.WriteLine(employee.Id + " " + employee.Name);  
}  
#endregion  
 
 
#region Peek Method  
int itemBeforePeek = EmployeeStack.Count();  
Console.WriteLine("Items before Peek {0}", EmployeeStack.Count);  
  
Employee employeePeek = EmployeeStack.Peek();  
Console.WriteLine(employeePeek.Name);  
  
int itemAfterPeek = EmployeeStack.Count();  
Console.WriteLine("Items After Peek {0}", EmployeeStack.Count);  
 
#endregion  
  
}

Summary

In this article, we learned how to work with the Queue and Stack collection classes.

I hope you like it.
Thank you.


Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles