Stack and Queue in a Nutshell

Here, I will talk about Stack<T>. Basically, Stack works on the principle of Last In First Out (LIFO). That means that of the elements inserted into the stack, the element inserted last will come out first. Here, there are two fundamental principles involved, 1) Push and 2) Pop. Push is to insert the element onto the stack and Pop to remove it.

In the following 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. }  
Now, let's cover Queue. Queues are the opposite of Stack. Queues work on the principle of First In First Out (FIFO). Here, two basic operations are involved to insert (known as Enqueue) and remove (known as Dequeue). Let's see that 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. }  
So, this was stacks and queue. In the next section, we will cover some other collections. Until then stay tuned and Happy Coding.