How to Use Stack Class For LIFO Operation in C#

Introduction

In this article I am describing how to use the Stack class for LIFO operations on its collection of items. The Stack class is the class providing a Last-In-First-Out (LIFO) collection of items. In LIFO the item added last is always at the top of the Stack and also the first one to be removed. A Stack provides a powerful and simple Last-In-First-Out data structure. Stack is a generic type. The last element added (with Push) to the stack is the first one removed (with Pop). Stack is one of the common data structures used in various mathematical functions like Towers of Hanoi, finding the Fibonacci Sequence and the factorial of a number, to name a few. The most important operations on a Stack Data Structure are:
Push() : Insert an element onto the Stack.
Pop()  : Remove the topmost element from the Stack.

For this, follow these steps:
Step1: Open the Visual Studio 2010, click on the New Project.
Step2: Now click on the Visual C#; a list will open which shows different categories.
Step3: First click on the Windows and then click on the console application; you will see a page in which you have to do the coding.
Step4: After coding, build the solution and press F5 for the output.
The following example shows how we can use the Stack class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace St
{
    class
Cl
    {
        static void Main()
        {
            Stack days = new Stack();
            days.Push("Sunday");
            days.Push("Monday");
            days.Push("Tuesday");
            days.Push("Wednesday");
            days.Push("Thursaday");
            days.Push("Friday");
            days.Push("Saturday");

            if (days.Count == 7)
            {
               MessageBox.Show(days.Pop().ToString());
            }
           
else
            {
               MessageBox.Show("Saturday does not exist");
          }
      }
    }
}

The output of the above program will be:

  st.png

For this program we have to add the System.Collections namespace. The System.Collections namespace provides many classes, methods and properties to interact with the varying data structures that are supported by it. System.Collections is also used for initializing a Collection. We use the System.Collections.Generic namespace to take a numeric value and alphabet value together. Here System.Windows.Forms is used to show the message by using MessageBox.Show(). In this we also use an if,else condition for performing the Stack operation.
We can also perform a Stack operation by creating a Stack object. The following example shows how we perform a Stack operation using an object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace st
{
    class
Test
    {
        static void Main()
        {
            Stack stackObject = new Stack();
            stackObject.Push("MCN");
            stackObject.Push("HCL");
            stackObject.Push("IBM");
            while (stackObject.Count > 0)
                Console.WriteLine(stackObject.Pop());
            Console.ReadLine();
        }
    }
}

The output of the above program will be:

st21.png

The Push() method is responsible for storing items in the Stack and the Pop() method removes them one at a time from the top of the Stack.

 

Up Next
    Ebook Download
    View all
    Learn
    View all