08 Aug C# Stack
Stack Collections come under System.Collection.Generic namespace. It stores in the LIFO form i.e. Last In First Out. Add elements using the Push() method, whereas remove using Pop().
There are two types of Stacks in C#: Generic and Non-Generic. The Generic Stack class should be preferred over Non-Generic.
Create a Stack and Push elements
To create a Stack in C#, with string elements, use <string>. Set the type in angular braces as shown below:
1 2 3 |
Stack<string> stack = new Stack<string>(); |
Let us see an example of creating a stack and pushing/adding elements. To add elements i.e., insert the object at the top of the stack, use the Push() method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a stack Stack<string> stack = new Stack<string>(); // Push/ Add 5 elements to the Stack stack.Push("Sachin"); stack.Push("Amit"); stack.Push("Virat"); stack.Push("Rohit"); stack.Push("Rahul"); // Display the elements Console.WriteLine("Display the stack elements...\n"); foreach (string s in stack) { Console.WriteLine(s); } } } } |
Output
1 2 3 4 5 6 7 8 9 |
Display the stack elements... Rahul Rohit Virat Amit Sachin |
Pop an element
To retrieve and remove an element from the top of the stack, use the Pop() method. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a stack Stack<string> stack = new Stack<string>(); // Push/ Add 5 elements to the Stack stack.Push("Sachin"); stack.Push("Amit"); stack.Push("Virat"); stack.Push("Rohit"); stack.Push("Rahul"); // Display the stack elements Console.WriteLine("Display the stack elements..."); foreach (string s in stack) { Console.WriteLine(s); } // Pop en element i.e. the tomost element gets removed Console.WriteLine("\nPop an element = \n"+stack.Pop()); // Display the updated stack elements Console.WriteLine("\nDisplay the updated stack elements..."); foreach (string s in stack) { Console.WriteLine(s); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Display the stack elements... Rahul Rohit Virat Amit Sachin Pop an element = Rahul Display the updated stack elements... Rohit Virat Amit Sachin |
Peek an element
To return the element at the top of the stack, without removing it, use the Peek() method. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a stack Stack<string> stack = new Stack<string>(); // Push/ Add 5 elements to the Stack stack.Push("Sachin"); stack.Push("Amit"); stack.Push("Virat"); stack.Push("Rohit"); stack.Push("Rahul"); // Display the stack elements Console.WriteLine("Display the stack elements..."); foreach (string s in stack) { Console.WriteLine(s); } // Peek an element i.e., return the topmost element Console.WriteLine("\nPeek an element = \n"+stack.Peek()); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Display the stack elements... Rahul Rohit Virat Amit Sachin Peek an element = Rahul |
Count the number of elements
The Count property is used in C# to count the number of elements. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a stack Stack<string> stack = new Stack<string>(); // Push/ Add 5 elements to the Stack stack.Push("Sachin"); stack.Push("Amit"); stack.Push("Virat"); stack.Push("Rohit"); stack.Push("Rahul"); // Display the stack elements Console.WriteLine("Display the stack elements..."); foreach (string s in stack) { Console.WriteLine(s); } // Count of stack elements Console.WriteLine("\nStack elements = \n"+stack.Count); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Display the stack elements... Rahul Rohit Virat Amit Sachin Stack elements = 5 |
Remove all the elements
The Clear() method is used to remove all the stack elements. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
using System; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a stack Stack<string> stack = new Stack<string>(); // Push/ Add 5 elements to the Stack stack.Push("Sachin"); stack.Push("Amit"); stack.Push("Virat"); stack.Push("Rohit"); stack.Push("Rahul"); // Display the stack elements Console.WriteLine("Display the stack elements..."); foreach (string s in stack) { Console.WriteLine(s); } // Count of stack elements Console.WriteLine("\nStack elements = \n"+stack.Count); // Remove all the elements stack.Clear(); // Count of stack elements after removing all the elements Console.WriteLine("\nStack elements (updated) = \n"+stack.Count); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Display the stack elements... Rahul Rohit Virat Amit Sachin Stack elements = 5 Stack elements (updated) = 0 |
Read More
No Comments