Working On Stack Using C#

In this article I explain how to create a Stack<> in C# and how to perform the various operations on a Stack.

Introduction

Stack represents a First-in, Last-out process. The basic concept of a stack can be illustrated by thinking of your data as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. Stack<T> is the class of System.Collection.Generic, where T Specifies the type of elements in the stack. In this article I explain the constructors, properties and methods of the Stack class.
 
Constructors

Working On Stack Using C#
Properties

Count -
Gets the number of elements contained in the Stack<T>. For example:

  1. namespace Stack {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Stack < string > stack1 = newStack < string > ();  
  5.             string[] str = {  
  6.                 "MCA",  
  7.                 "BCA",  
  8.                 "BBA",  
  9.                 "MBA",  
  10.                 "MTech"  
  11.             };  
  12.             Stack < string > stack2 = newStack < string > (str);  
  13.             Stack < string > stack3 = newStack < string > (10);  
  14.             Console.WriteLine("The elements in the stack1 are:" + stack1.Count());  
  15.             Console.WriteLine("The elements in the stack2 are:" + stack2.Count());  
  16.             Console.WriteLine("The elements in the stack3 are:" + stack3.Count());  
  17.         }  
  18.     }  
  19. }  

Output

Working On Stack Using C#
 
Methods

Some of the most common and popular methods of stack are:

Push() - Push() method is used to push or insert an element in the stack, or we can say that this method is used to insert an object at the top of the Stack<T>. For example:

  1. namespace Stack {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Stack < string > stack1 = newStack < string > ();  
  5.             string[] str = {  
  6.                 "MCA",  
  7.                 "BCA",  
  8.                 "BBA",  
  9.                 "MBA",  
  10.                 "MTech"  
  11.             };  
  12.             Stack < string > stack2 = newStack < string > (str);  
  13.             Stack < string > stack3 = newStack < string > (10);  
  14.             stack1.Push("************");  
  15.             stack1.Push("MCA");  
  16.             stack1.Push("MBA");  
  17.             stack1.Push("BCA");  
  18.             stack1.Push("BBA");  
  19.             stack1.Push("***********");  
  20.             stack1.Push("**Courses**");  
  21.             stack1.Push("***********");  
  22.             Console.WriteLine("The elements in the stack1 are as:");  
  23.             foreach(string s in stack1) {  
  24.                 Console.WriteLine(s);  
  25.             }  
  26.             Console.WriteLine("The elements in the stack2 are as:");  
  27.             foreach(string s in stack2) {  
  28.                 Console.WriteLine(s);  
  29.             }  
  30.             stack3.Push("one");  
  31.             stack3.Push("Two");  
  32.             Console.WriteLine("The elements in the stack3 are as:");  
  33.             foreach(string s in stack3) {  
  34.                 Console.WriteLine(s);  
  35.             }  
  36.         }  
  37.     }  
  38. }  

Output

Working On Stack Using C#

Pop() - Pop() method is used to remove the element from the stack or we can say that this method removes and returns the object at the top of the Stack<T>. For example:

  1. namespace Stack {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Stack < string > stack1 = newStack < string > ();  
  5.             stack1.Push("************");  
  6.             stack1.Push("MCA");  
  7.             stack1.Push("MBA");  
  8.             stack1.Push("BCA");  
  9.             stack1.Push("BBA");  
  10.             stack1.Push("***********");  
  11.             stack1.Push("**Courses**");  
  12.             stack1.Push("***********");  
  13.             Console.WriteLine("The elements in the stack1 are as:");  
  14.             foreach(string s in stack1) {  
  15.                 Console.WriteLine(s);  
  16.             }  
  17.             //For remove/or pop the element pop() method is used  
  18.             stack1.Pop();  
  19.             stack1.Pop();  
  20.             stack1.Pop();  
  21.             Console.WriteLine("After removal/or pop the element the stack is as:");  
  22.             //the element that inserted in last is remove firstly.  
  23.             foreach(string s in stack1) {  
  24.                 Console.WriteLine(s);  
  25.             }  
  26.         }  
  27.     }  
  28. }  

Output

Working On Stack Using C#

Peek() - This method returns the object at the top of the Stack<T> without removing it. For example:

  1. namespace Stack {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Stack < string > stack1 = newStack < string > ();  
  5.             stack1.Push("************");  
  6.             stack1.Push("MCA");  
  7.             stack1.Push("MBA");  
  8.             stack1.Push("BCA");  
  9.             stack1.Push("BBA");  
  10.             stack1.Push("***********");  
  11.             stack1.Push("**Courses**");  
  12.             stack1.Push("***********");  
  13.             Console.WriteLine("The elements in the stack1 are as:");  
  14.             foreach(string s in stack1) {  
  15.                 Console.WriteLine(s);  
  16.             }  
  17.             //The first peek element is find by peek() method  
  18.             //peek method gives the element that located at the top of the stack  
  19.             Console.WriteLine("The peek element is:" + stack1.Peek());  
  20.             stack1.Pop();  
  21.             Console.WriteLine("The nest peek element is:" + stack1.Peek());  
  22.         }  
  23.     }  
  24. }  

Output

Working On Stack Using C#
 
Some More Methods
 
Contain() - Determines whether an element is in the Stack<T>. For example:

  1. namespace Stack {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Stack < string > stack1 = newStack < string > ();  
  5.             stack1.Push("************");  
  6.             stack1.Push("MCA");  
  7.             stack1.Push("MBA");  
  8.             stack1.Push("BCA");  
  9.             stack1.Push("BBA");  
  10.             stack1.Push("***********");  
  11.             stack1.Push("**Courses**");  
  12.             stack1.Push("***********");  
  13.             Console.WriteLine("The elements in the stack1 are as:");  
  14.             foreach(string s in stack1) {  
  15.                 Console.WriteLine(s);  
  16.             }  
  17.             Console.WriteLine("The element MCA contain in the stack " + stack1.Contains("MCA"));  
  18.             Console.WriteLine("The element BCA contain in the stack " + stack1.Contains("BCA"));  
  19.             Console.WriteLine("The element MTech contain in the stack " + stack1.Contains("MTech"));  
  20.         }  
  21.     }  
  22. }  

Output

Working On Stack Using C#

Clear() - Removes all objects from the Stack<T>. For example:

  1. namespace Stack {  
  2.     classProgram {  
  3.         staticvoid Main(string[] args) {  
  4.             Stack < string > stack1 = newStack < string > ();  
  5.             stack1.Push("************");  
  6.             stack1.Push("MCA");  
  7.             stack1.Push("MBA");  
  8.             stack1.Push("BCA");  
  9.             stack1.Push("BBA");  
  10.             stack1.Push("***********");  
  11.             stack1.Push("**Courses**");  
  12.             stack1.Push("***********");  
  13.             Console.WriteLine("The element in the stack are:" + stack1.Count());  
  14.             Console.WriteLine("The elements in the stack1 are as:");  
  15.             foreach(string s in stack1) {  
  16.                 Console.WriteLine(s);  
  17.             }  
  18.             //clear() method remove/or clear all the elements from the stack  
  19.             stack1.Clear();  
  20.             Console.WriteLine("After apply the clear method the elements in the stack are:" + stack1.Count());  
  21.         }  
  22.     }  
  23. }  

 Output

Working On Stack Using C#
 
Summary
 
In this article I explained the concept of a stack and the various operations on a stack.