Learn About Stack and Queue in a Nutshell

Hi Friends,

Now in this section, I am going to talk about Stack<T>. Basically, stack works on the principle of LIFO(Last in First out) means the element which gets inserted in the stack Last will come out first. Here, there are two fundamental principles involved 1)Push and 2)Pop. Push is to insert the element on the stack and Pop to remove the same.

Below, in the snippet i have explained all the basic operations involved with Stack.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using System.Collections.ObjectModel;  
  4. using System.Linq;  
  5. using System;  
  6.   
  7. namespace Collections  
  8. {  
  9.    internal class Stack  
  10.     {  
  11.         private static void Main(string[] args)  
  12.         {  
  13.             var monthsofYear = new Stack<string>();  
  14.             monthsofYear.Push("December");  
  15.             monthsofYear.Push("November");  
  16.             monthsofYear.Push("October");  
  17.             monthsofYear.Push("September");  
  18.             monthsofYear.Push("August");  
  19.             monthsofYear.Push("July");  
  20.             monthsofYear.Push("June");  
  21.             monthsofYear.Push("May");  
  22.             monthsofYear.Push("April");  
  23.             monthsofYear.Push("March");  
  24.             monthsofYear.Push("February");  
  25.             monthsofYear.Push("January");  
  26.   
  27.             //Peek returns top element without removing it  
  28.             string topElement = monthsofYear.Peek();  
  29.   
  30.             Console.WriteLine("Top element is: {0}",topElement);  
  31.             foreach (var month in monthsofYear)  
  32.             {  
  33.                 Console.WriteLine(month);  
  34.             }  
  35.   
  36.             //removed the top element  
  37.             string removeditem=monthsofYear.Pop();  
  38.             Console.WriteLine("Collection after removing: {0}", removeditem);  
  39.             foreach (var month in monthsofYear)  
  40.             {  
  41.                 Console.WriteLine(month);  
  42.             }  
  43.             Console.ReadLine();  
  44.         }  
  45.   
  46.           
  47.   
  48.     }  
  49.   
  50. }


Now, lets cover Queue. Queues are opposite of Stack. Queues work on the principle of FIFO(First in First out). Here, also two basic operations are involved to insert(known as Enqueue) and remove(known as Dequeue). Lets see the same in action.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using System.Collections.ObjectModel;  
  4. using System.Linq;  
  5. using System;  
  6.   
  7. namespace Collections  
  8. {  
  9.    internal class Queues  
  10.     {  
  11.         private static void Main(string[] args)  
  12.         {  
  13.             var monthsofYear = new Queue<string>();  
  14.             monthsofYear.Enqueue("January");  
  15.             monthsofYear.Enqueue("February");  
  16.             monthsofYear.Enqueue("March");  
  17.             monthsofYear.Enqueue("April");  
  18.             monthsofYear.Enqueue("May");  
  19.             monthsofYear.Enqueue("June");  
  20.             monthsofYear.Enqueue("July");  
  21.             monthsofYear.Enqueue("August");  
  22.             monthsofYear.Enqueue("September");  
  23.             monthsofYear.Enqueue("October");  
  24.             monthsofYear.Enqueue("November");  
  25.             monthsofYear.Enqueue("December");  
  26.               
  27.               
  28.             //Peek returns top element without removing it  
  29.             string topElement = monthsofYear.Peek();  
  30.   
  31.             Console.WriteLine("Top element is: {0}",topElement);  
  32.             foreach (var month in monthsofYear)  
  33.             {  
  34.                 Console.WriteLine(month);  
  35.             }  
  36.   
  37.             string removedItem=monthsofYear.Dequeue();  
  38.             Console.WriteLine("Collection after removing: {0}", removedItem);  
  39.   
  40.             foreach (var month in monthsofYear)  
  41.             {  
  42.                 Console.WriteLine(month);  
  43.             }  
  44.               
  45.             Console.ReadLine();  
  46.         }  
  47.   
  48.           
  49.   
  50.     }  
  51.   
  52. }


So, this was the stacks and queue. In the next section, will cover some other collection. Till then stay tuned and Happy Coding.

Thanks,

Rahul Sahay

Happy coding
Next Recommended Reading How to implement Queue in C#