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.
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System;
-
- namespace Collections
- {
- internal class Stack
- {
- private static void Main(string[] args)
- {
- var monthsofYear = new Stack<string>();
- monthsofYear.Push("December");
- monthsofYear.Push("November");
- monthsofYear.Push("October");
- monthsofYear.Push("September");
- monthsofYear.Push("August");
- monthsofYear.Push("July");
- monthsofYear.Push("June");
- monthsofYear.Push("May");
- monthsofYear.Push("April");
- monthsofYear.Push("March");
- monthsofYear.Push("February");
- monthsofYear.Push("January");
-
-
- string topElement = monthsofYear.Peek();
-
- Console.WriteLine("Top element is: {0}",topElement);
- foreach (var month in monthsofYear)
- {
- Console.WriteLine(month);
- }
-
-
- string removeditem=monthsofYear.Pop();
- Console.WriteLine("Collection after removing: {0}", removeditem);
- foreach (var month in monthsofYear)
- {
- Console.WriteLine(month);
- }
- Console.ReadLine();
- }
-
-
-
- }
-
- }
![]()
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.
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System;
-
- namespace Collections
- {
- internal class Queues
- {
- private static void Main(string[] args)
- {
- var monthsofYear = new Queue<string>();
- monthsofYear.Enqueue("January");
- monthsofYear.Enqueue("February");
- monthsofYear.Enqueue("March");
- monthsofYear.Enqueue("April");
- monthsofYear.Enqueue("May");
- monthsofYear.Enqueue("June");
- monthsofYear.Enqueue("July");
- monthsofYear.Enqueue("August");
- monthsofYear.Enqueue("September");
- monthsofYear.Enqueue("October");
- monthsofYear.Enqueue("November");
- monthsofYear.Enqueue("December");
-
-
-
- string topElement = monthsofYear.Peek();
-
- Console.WriteLine("Top element is: {0}",topElement);
- foreach (var month in monthsofYear)
- {
- Console.WriteLine(month);
- }
-
- string removedItem=monthsofYear.Dequeue();
- Console.WriteLine("Collection after removing: {0}", removedItem);
-
- foreach (var month in monthsofYear)
- {
- Console.WriteLine(month);
- }
-
- Console.ReadLine();
- }
-
-
-
- }
-
- }
![]()
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