How to Use Stack Class For LIFO Operation in C#

Introduction to Stack In C#

In this article, I explain how to use the C# Stack class for Last-In-First-Out (LIFO) operations on a collection. The Stack class provides functionality of a Last-In-First-Out (LIFO) collection. In LIFO, items added last is always at the top of the Stack and also is 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 to be 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 a new element to the Stack.
  • Pop(): Remove the topmost element from the Stack.

Let's create a simple C# Console application using in Visual Studio.

How to 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("Thursday");
            days.Push("Friday");
            days.Push("Saturday");

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

Output

output

The Stack class is a generic class and defined in the The System.Collections.Generic namespace. Make sure to import this namespace in your project using the following.

using System.Collections.Generic;

In the above code, the 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.

The above code uses the Push method to add items to a stack. The code also uses the Pop method to remove items from the stack. 

We can also perform a Stack operation by creating a Stack object. The following example shows how we perform a Stack operations 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();
        }
    }
}

Output

output

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

Want to learn more about Stack in C#, here is a complete tutorial: Working with Stack In C#


Similar Articles