When it comes to data structures, one of the frequently used data structure is Stack. It represents Last in First out (LIFO) concept. Just to give a brief idea about the LIFO, it is a concept that used to remove last elements that added to stack first. Meanwhile, it is not possible to remove elements from the middle of the stack. For an example, LIFO behaves just like removing books from the top of a pile of books.

Overview of Stack Data Structure

In the meantime, stacks behave in the same way in C# as the other languages. From the technical point of view, adding elements to the stack is called “Push” operation and removing elements from the stack is called “Pop” operation. Let’s take a look at how to initialize the stack in C#. Before the initialization, it is must to import library which contains Stack. It is the library available in the System class called “Collections”.

        using System.Collections;

        class StackExample
        {
            public static void main()
            {
                Stack _stack = new Stack();
            }
        }

Add elements to a Stack

With the latest version of the Collection class, we don’t need to specify the data type of the stack. From our implementation, the system identifies the correct form of the data type. Now, let’s add some elements to the stack and remove some of the items from it. As we have initialized a stack in the above C# code, let’s use that stack to add some elements.

stackObject.Push("Intro");
_stack.Push("to");
_stack.Push("C#");
_stack.Push("Stack");

In the above example, I have added 4 elements to the stack. Let’s take a look at how these elements are arranged in the stack from the below diagram.

Remove elements from a Stack

From the definition Push() method should have one parameter which is the element to be added to the stack. In the Pop() method, we cannot define any parameter as by default calling Pop() will remove the topmost element in the stack. So from the above example, if we want to remove element “to” before element “C#” and “Stack”, it is not possible. It is a must to remove the topmost elements before you can remove the element 2. If we call the Pop() method once it will remove topmost elements as shown below.

So far we have added elements and removed elements from the Stack. Not like other data structures, we have to adhere to the structure of the Stack. There are certain properties and methods available in the Stack class to check the number of elements currently present at the stack and to check the existence of a particular element in the Stack. Let’s take a look at, how to implement those methods and properties using C# code.

Some useful properties and methods in the stack class

Count

Since we have removed two elements from the Stack, only two elements present at the Stack. When we want to know the number of elements present we use the following property;

 int i = _stack.Count;
 Console.Write(i.ToString());

Contains

When we want to check the availability of a certain element, we use Contain() method which takes one argument. If the element is present in the Stack it results in Boolean value “True” else results in “False”.

 Boolean contain = _stack.Contains("To");
 Console.Write(contain.ToString());

 

Print

Finally, let’s get to the point, how can we print our values in the Stack using C#. It’s the same as the normal print statements used to print elements, hence this is a collection of elements, and we’ll iterate the Stack using a foreach loop and print each element.

 foreach (Object _stackElement in _stack)
 {
    Console.WriteLine(_stackElement.ToString());
 }

 

Related Articles

Exception Handling in C#

Create Your First C# Program

C# while Loop

Last modified: February 17, 2019

Comments

Write a Reply or Comment

Your email address will not be published.