Working on Stack Using C#

Introduction

Stack represents a First-in, Last-out process. The basic concept of a stack can be illustrated by thinking of your data as a stack of plates or books where you can only take the top item off the stack in order to remove things from it. Stack<T> is the class of System.Collection.Generic, where T Specifies the type of elements in the stack. In this article I explain the constructors, properties and methods of the Stack class.

Constructors

image11.jpg
Properties

Count - Gets the number of elements contained in the Stack<T>. For example:

 

namespace Stack

{

    class Program

    {

        static void Main(string[] args)

        {

            Stack<string> stack1 = new Stack<string>();

            string[] str = { "MCA", "BCA", "BBA", "MBA", "MTech" };

            Stack<string> stack2 = new Stack<string>(str);

            Stack<string> stack3 = new Stack<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());

        }

    }

}

 

Output

IMAGE1.jpg


Methods

 

Some of the most common and popular methods of stack are:

 

Push() - Push() method is used to push or insert an element in the stack, or we can say that this method is used to insert an object at the top of the Stack<T>. For example:

 

namespace Stack

{

    class Program

    {

        static void Main(string[] args)

        {

            Stack<string> stack1 = new Stack<string>();

            string[] str = { "MCA", "BCA", "BBA", "MBA", "MTech" };

            Stack<string> stack2 = new Stack<string>(str);

            Stack<string> stack3 = new Stack<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);

            }           

        }

    }

}

 

Output

image2.jpg

Pop() - Pop() method is used to remove the element from the stack or we can say that this method removes and returns the object at the top of the Stack<T>. For example:

namespace Stack

{

    class Program

    {

        static void Main(string[] args)

        {

            Stack<string> stack1 = new Stack<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);

            }

            //For remove/or pop the element pop() method is used

            stack1.Pop();

            stack1.Pop();

            stack1.Pop();

            Console.WriteLine("After removal/or pop the element the stack is as:");

            //the element that inserted in last is remove firstly.

            foreach (string s in stack1)

            {

                Console.WriteLine(s);

            }

        }

    }

}

 

 Output

IMAGE3.jpg

Peek() - This method returns the object at the top of the Stack<T> without removing it. For example:

namespace Stack

{

    class Program

    {

        static void Main(string[] args)

        {

            Stack<string> stack1 = new Stack<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);

            }

            //The first peek element is find by peek() method

            //peek method gives the element that located at the top of the stack

            Console.WriteLine("The peek element is:" + stack1.Peek());

            stack1.Pop();

            Console.WriteLine("The nest peek element is:" + stack1.Peek());

        }

    }

}

 

Output

IMAGE4.jpg

Some More Methods

Contain() - Determines whether an element is in the Stack<T>. For example:

namespace Stack

{

    class Program

    {

        static void Main(string[] args)

        {

            Stack<string> stack1 = new Stack<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"));

        }

    }

}

 

Output

 

IMAGE5.jpg

 

Clear() - Removes all objects from the Stack<T>. For example:

namespace Stack

{

    class Program

    {

        static void Main(string[] args)

        {

            Stack<string> stack1 = new Stack<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);

            }

            //clear() method remove/or clear all the elements from the stack

            stack1.Clear();

            Console.WriteLine("After apply the clear method the elements in the stack are:" + stack1.Count());

        }

    }

}

 

 Output

IMAGE6.jpg

Summary 

In this article I explained the concept of a stack and the various operations on a stack.