Queue And Stack Collection In C#

Queue and stack are two common implementations when creating linked lists. A queue uses first-in-first-out algorithm. The stack uses last-in-first-out algorithm. Both are generic collections in C# and .NET. In the code examples of this article, we will learn about and how to work with Queue and Stack collection classes of .NET 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.
  1. class Employee  
  2. {  
  3.    public int Id { getset; }  
  4.    public string Name { getset; }  

In the same class file, create another class “MainProgram” and create a Main method.
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {
  5.     }  

In this Main method create three objects of the Employee class.
  1. static void Main(string[] args)  
  2. {  
  3.     Employee employeeOne = new Employee()  
  4.     {  
  5.         Id = 1,  
  6.         Name = "Aiden"  
  7.     };  
  8.   
  9.     Employee employeeTwo = new Employee()   
  10.     {  
  11.         Id = 2,   
  12.         Name = "Jack"  
  13.     };  
  14.   
  15.     Employee employeeThree = new Employee()  
  16.     {  
  17.         Id = 3,  
  18.         Name = "Tom Clancy"  
  19.     };  
  20.   

To create a List of Employees, we say:
  1. List<Employee> employeeList = new List<Employee>(); 
Where Employee is the type.

Just like that, to create an Employee queue collection object, we say:
  1. 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.
  1. Queue<Employee> employeeQueue = new Queue<Employee>();  
  2. employeeQueue.Enqueue(employeeOne);  
  3. employeeQueue.Enqueue(employeeTwo);  
  4. 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.
  1. int itemsBeforeDequeue = employeeQueue.Count;  
  2. Console.WriteLine("Items before Dequeue {0}",itemsBeforeDequeue);  
  3.   
  4. Employee empOne = employeeQueue.Dequeue();  
  5. Console.WriteLine(empOne.Id+" "+empOne.Name);  
  6.   
  7. int itemsAfterDequeue = employeeQueue.Count;  
  8. 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.
  1. Employee empTwo = employeeQueue.Dequeue();  
  2. Console.WriteLine(empTwo.Id + " " + empTwo.Name);  
  3.   
  4. int itemsAfterFirstDequeue = employeeQueue.Count;  
  5. 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.
  1. foreach(Employee emp in employeeQueue)  
  2. {  
  3.     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.
  1. int itemBeforePeek = employeeQueue.Count();  
  2. Console.WriteLine("Items before Peek {0}", employeeQueue.Count);  
  3.   
  4. Employee employeePeek = employeeQueue.Peek();  
  5. Console.WriteLine(employeePeek.Name);  
  6.   
  7. int itemAfterPeek = employeeQueue.Count();  
  8. 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.
  1. static void Main(string[] args)  
  2. {  
  3.     Employee employeeOne = new Employee()  
  4.     {  
  5.         Id = 1,  
  6.         Name = "Aiden"  
  7.     };  
  8.   
  9.     Employee employeeTwo = new Employee()  
  10.     {  
  11.         Id = 2,  
  12.         Name = "Jack"  
  13.     };  
  14.   
  15.     Employee employeeThree = new Employee()  
  16.     {  
  17.         Id = 3,  
  18.         Name = "Tom Clancy"  
  19.     };  
  20.   
  21.     Queue<Employee> employeeQueue = new Queue<Employee>();  
  22.     employeeQueue.Enqueue(employeeOne);  
  23.     employeeQueue.Enqueue(employeeTwo);  
  24.     employeeQueue.Enqueue(employeeThree);  
  25.  
  26.     #region Dequeue method      
  27.     int itemsBeforeDequeue = employeeQueue.Count;  
  28.     Console.WriteLine("Items before Dequeue {0}", itemsBeforeDequeue);  
  29.   
  30.     Employee empOne = employeeQueue.Dequeue();  
  31.     Console.WriteLine(empOne.Id + " " + empOne.Name);  
  32.   
  33.     int itemsAfterDequeue = employeeQueue.Count;  
  34.     Console.WriteLine("Items after first Dequeue {0}", itemsAfterDequeue);  
  35.   
  36.     Employee empTwo = employeeQueue.Dequeue();  
  37.     Console.WriteLine(empTwo.Id + " " + empTwo.Name);  
  38.   
  39.     int itemsAfterFirstDequeue = employeeQueue.Count;  
  40.     Console.WriteLine("Items after second Dequeue {0}", itemsAfterFirstDequeue);  
  41.  
  42.     #endregion  
  43.  
  44.     #region foreach loop      
  45.     foreach (Employee emp in employeeQueue)  
  46.     {  
  47.         Console.WriteLine(emp.Id + " " + emp.Name);  
  48.     }  
  49.  
  50.     #endregion  
  51.  
  52.     #region peek method      
  53.     int itemBeforePeek = employeeQueue.Count();  
  54.     Console.WriteLine("Items before Peek {0}", employeeQueue.Count);  
  55.   
  56.     Employee employeePeek = employeeQueue.Peek();  
  57.     Console.WriteLine(employeePeek.Name);  
  58.   
  59.     int itemAfterPeek = employeeQueue.Count();  
  60.     Console.WriteLine("Items After Peek {0}", employeeQueue.Count);  
  61.     #endregion  
  62. }  
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.
  1. class Employee  
  2. {  
  3.     public int Id { getset; }  
  4.     public string Name { getset; }  
  5. }  
  6.   
  7. //In the same class file, create another class “MainProgram” and create a Main method .      
  8.   
  9. class Program  
  10. {  
  11.     static void Main(string[] args)  
  12.     {  
  13.   
  14.     }  
  15. }  
  16.   
  17. //In this Main method create three different objects of Employee class.     
  18.   
  19. static void Main(string[] args)  
  20. {  
  21.     Employee employeeOne = new Employee()  
  22.     {  
  23.         Id = 1,  
  24.         Name = "Aiden"  
  25.     };  
  26.   
  27.     Employee employeeTwo = new Employee()  
  28.     {  
  29.         Id = 2,  
  30.         Name = "Jack"  
  31.     };  
  32.   
  33.     Employee employeeThree = new Employee()  
  34.     {  
  35.         Id = 3,  
  36.         Name = "Tom Clancy"  
  37.     };  
  38.   
  39. }    
To create an Employee stack collection object, we say:
  1. 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.
  1. Stack<Employee> EmployeeStack = new Stack<Employee>();  
  2. EmployeeStack.Push(employeeOne);  
  3. EmployeeStack.Push(employeeTwo);  
  4. 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.
  1. int itemsBeforeStack = EmployeeStack.Count;  
  2. Console.WriteLine("Items before Dequeue {0}", itemsBeforeStack);  
  3.   
  4. Employee empOne = EmployeeStack.Pop();  
  5. Console.WriteLine(empOne.Id + " " + empOne.Name);  
  6.   
  7. int itemsAfterStack = EmployeeStack.Count;  
  8. 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.
  1. foreach(Employee employee in EmployeeStack) {  
  2.    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.
  1. int itemBeforePeek = EmployeeStack.Count();  
  2. Console.WriteLine("Items before Peek {0}", EmployeeStack.Count);  
  3.   
  4. Employee employeePeek = EmployeeStack.Peek();  
  5. Console.WriteLine(employeePeek.Name);  
  6.   
  7. int itemAfterPeek = EmployeeStack.Count();  
  8. Console.WriteLine("Items After Peek {0}", EmployeeStack.Count); 
Run the application.
 
Queue And Stack Collection Classes In C# 

Code snippet for the Main method.
  1. static void Main(string[] args) {  
  2.    Employee employeeOne = new Employee() {  
  3.       Id = 1,  
  4.       Name = "Aiden"  
  5. };  
  6.   
  7. Employee employeeTwo = new Employee() {  
  8.    Id = 2,  
  9.    Name = "Jack"  
  10. };  
  11.   
  12. Employee employeeThree = new Employee() {  
  13.    Id = 3,  
  14.    Name = "Tom Clancy"  
  15. };  
  16.   
  17. Stack<Employee> EmployeeStack = new Stack<Employee>();  
  18. EmployeeStack.Push(employeeOne);  
  19. EmployeeStack.Push(employeeTwo);  
  20. EmployeeStack.Push(employeeThree);  
  21.  
  22. #region Pop Method  
  23. int itemsBeforeStack = EmployeeStack.Count;  
  24. Console.WriteLine("Items before Dequeue {0}", itemsBeforeStack);  
  25.   
  26. Employee empOne = EmployeeStack.Pop();  
  27. Console.WriteLine(empOne.Id + " " + empOne.Name);  
  28.   
  29. int itemsAfterStack = EmployeeStack.Count;  
  30. Console.WriteLine("Items after Dequeue {0}", itemsAfterStack);  
  31. #endregion  
  32.  
  33.  
  34. #region Foreach loop  
  35. foreach(Employee employee in EmployeeStack) {  
  36.    Console.WriteLine(employee.Id + " " + employee.Name);  
  37. }  
  38. #endregion  
  39.  
  40.  
  41. #region Peek Method  
  42. int itemBeforePeek = EmployeeStack.Count();  
  43. Console.WriteLine("Items before Peek {0}", EmployeeStack.Count);  
  44.   
  45. Employee employeePeek = EmployeeStack.Peek();  
  46. Console.WriteLine(employeePeek.Name);  
  47.   
  48. int itemAfterPeek = EmployeeStack.Count();  
  49. Console.WriteLine("Items After Peek {0}", EmployeeStack.Count);  
  50.  
  51. #endregion  
  52.   

Summary

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

I hope you like it.
Thank you.