Working With Stack In C# don't

Introduction
 
 
 
A stack is a LIFO (last in first out) data structure. Think of stack as a collection of items where anything you insert in a stack will be placed at the top and if you need to remove something, it will be removed from the top. A stack of plates or a book stack are two common examples of a stack.
 
In this tutorial, we will learn how to use the Stack<> class in C#. We will see how to create a Stack and how to use its Push, Pop, and other methods.
The Stack<T> is a collection that is defined in the System.Collection.Generic namesoace where T specified the type of elements in the stack.
 
Constructors
 
The Stack<> class constructors look like the following:
 
 
 
Counting a stack items

The Count property of the Stack class returns the number of elements in a stack. The following code example creates three stacks using different methods and use the Count property to return the number of items in these stacks.
  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. }    

The output looks like the following:

Working On Stack Using C#
Adding items to a stack in C#

The Push() method is used to add (push) an elemen to the stack. The item is added to the top of the stack.

The following code example shows how to use the Push method to add items to a stack.
  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. }  
The output looks like the following:
 

Working On Stack Using C#

Removing items from a Stack in C#

The Pop() method is used to remove elements from a stack. The Pop() method removes the top most item from the stack.
The following code example uses the Pop() method three times to remove 3 elements.
 
  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. }    
The output looks like the following:
 
 
 

Getting items from the stack

The Peek() method returns the topmost element of a Stack<T> without removing it. The following code snippet read a stack items and displays its content.
 
  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. }    

The output looks like the following:

Working On Stack Using C#
Some More Methods
The Contain() method determines and returns true if an element is found in a stack. The following example uses the Contain method.
 
  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. }    
The Clear() method of Stack<T> removes all elements from a stack. The following code snippet uses the Clear() method and removes all items in a stack.
 
  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. }    
The output looks like the following:
 
Working On Stack Using C#
 
Summary
 
In this article I explained the concept of a stack and how to use the Stack<T> class to implement a stack in C#.
 
 
 

Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com