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.
- namespace Stack {
- classProgram {
- staticvoid Main(string[] args) {
- Stack < string > stack1 = newStack < string > ();
- string[] str = {
- "MCA",
- "BCA",
- "BBA",
- "MBA",
- "MTech"
- };
- Stack < string > stack2 = newStack < string > (str);
- Stack < string > stack3 = newStack < string > (10);
- Console.WriteLine("The elements in the stack1 are:" + stack1.Count());
- Console.WriteLine("The elements in the stack2 are:" + stack2.Count());
- Console.WriteLine("The elements in the stack3 are:" + stack3.Count());
- }
- }
- }
The output looks like the following:
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.
- namespace Stack {
- classProgram {
- staticvoid Main(string[] args) {
- Stack < string > stack1 = newStack < string > ();
- string[] str = {
- "MCA",
- "BCA",
- "BBA",
- "MBA",
- "MTech"
- };
- Stack < string > stack2 = newStack < string > (str);
- Stack < string > stack3 = newStack < string > (10);
- stack1.Push("************");
- stack1.Push("MCA");
- stack1.Push("MBA");
- stack1.Push("BCA");
- stack1.Push("BBA");
- stack1.Push("***********");
- stack1.Push("**Courses**");
- stack1.Push("***********");
- Console.WriteLine("The elements in the stack1 are as:");
- foreach(string s in stack1) {
- Console.WriteLine(s);
- }
- Console.WriteLine("The elements in the stack2 are as:");
- foreach(string s in stack2) {
- Console.WriteLine(s);
- }
- stack3.Push("one");
- stack3.Push("Two");
- Console.WriteLine("The elements in the stack3 are as:");
- foreach(string s in stack3) {
- Console.WriteLine(s);
- }
- }
- }
- }
The output looks like the following:
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.
- namespace Stack {
- classProgram {
- staticvoid Main(string[] args) {
- Stack < string > stack1 = newStack < string > ();
- stack1.Push("************");
- stack1.Push("MCA");
- stack1.Push("MBA");
- stack1.Push("BCA");
- stack1.Push("BBA");
- stack1.Push("***********");
- stack1.Push("**Courses**");
- stack1.Push("***********");
- Console.WriteLine("The elements in the stack1 are as:");
- foreach(string s in stack1) {
- Console.WriteLine(s);
- }
-
- stack1.Pop();
- stack1.Pop();
- stack1.Pop();
- Console.WriteLine("After removal/or pop the element the stack is as:");
-
- foreach(string s in stack1) {
- Console.WriteLine(s);
- }
- }
- }
- }
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.
- namespace Stack {
- classProgram {
- staticvoid Main(string[] args) {
- Stack < string > stack1 = newStack < string > ();
- stack1.Push("************");
- stack1.Push("MCA");
- stack1.Push("MBA");
- stack1.Push("BCA");
- stack1.Push("BBA");
- stack1.Push("***********");
- stack1.Push("**Courses**");
- stack1.Push("***********");
- Console.WriteLine("The elements in the stack1 are as:");
- foreach(string s in stack1) {
- Console.WriteLine(s);
- }
-
-
- Console.WriteLine("The peek element is:" + stack1.Peek());
- stack1.Pop();
- Console.WriteLine("The nest peek element is:" + stack1.Peek());
- }
- }
- }
The output looks like the following:
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.
- namespace Stack {
- classProgram {
- staticvoid Main(string[] args) {
- Stack < string > stack1 = newStack < string > ();
- stack1.Push("************");
- stack1.Push("MCA");
- stack1.Push("MBA");
- stack1.Push("BCA");
- stack1.Push("BBA");
- stack1.Push("***********");
- stack1.Push("**Courses**");
- stack1.Push("***********");
- Console.WriteLine("The elements in the stack1 are as:");
- foreach(string s in stack1) {
- Console.WriteLine(s);
- }
- Console.WriteLine("The element MCA contain in the stack " + stack1.Contains("MCA"));
- Console.WriteLine("The element BCA contain in the stack " + stack1.Contains("BCA"));
- Console.WriteLine("The element MTech contain in the stack " + stack1.Contains("MTech"));
- }
- }
- }
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.
- namespace Stack {
- classProgram {
- staticvoid Main(string[] args) {
- Stack < string > stack1 = newStack < string > ();
- stack1.Push("************");
- stack1.Push("MCA");
- stack1.Push("MBA");
- stack1.Push("BCA");
- stack1.Push("BBA");
- stack1.Push("***********");
- stack1.Push("**Courses**");
- stack1.Push("***********");
- Console.WriteLine("The element in the stack are:" + stack1.Count());
- Console.WriteLine("The elements in the stack1 are as:");
- foreach(string s in stack1) {
- Console.WriteLine(s);
- }
-
- stack1.Clear();
- Console.WriteLine("After apply the clear method the elements in the stack are:" + stack1.Count());
- }
- }
- }
The output looks like the following:
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#.