articles

Home / DeveloperSection / Articles / Stack Class in C#

Stack Class in C#

Anupam Mishra5004 01-Jan-2016

The Stack class represents a Last-in- First –Out (LIFO) stack of objects. It is mainly two operation push and pop i.e. we can push (insert) items into the stack and pop (retrieve) items into the stack. Stack is implemented a circular buffer. It is follows the (Last-in-first-out) strategy. We can push the items into a stack and get into a reverse order. Stack is returned as last elements as a first when you try to retrieve the items into the stack. As elements are added to a stack, the capacity is automatically increased as required through reallocation. 

Stack Constructor:

1.  Stack ():initializes a new instance of the stack class i.e. empty and has the default initial capacity.

2.  Stack (ICollection): Initializes a new instances of the stack class that contains elements copied.

3.    Stack (Int 32): initializes a new instances of the stack class i.e. empty and has the specified initial capacity or the default initial capacity, whichever is greater. 

Stack Properties:
1.      Count: Gets the number of elements contained in the stack.
2.      IsSynchronized: Gets a value indicating whether access to the stack is
synchronized (thread  safe).
Stack Methods:

  Some of the important methods of stack class are:

1.      Clear (): Removes all objects from the stack.

2.      Clone (): Create a shallow copy of the stack.

3.      Contains (Object): Determines whether an elements is in the stack.

4.      Peek (): Returns the object at the top of the stack without removing it.

5.      Pop (): Removes and returns the object at the top of the stack.

6.      Push (Object): Inserts an object at the top of the stack.

Advantage of Stack:

1.   As elements are added to the stack, the capacity of a stack is automatically increased as required through reallocation.

2.   Stack accepts null as a valid value and allows duplicate elements.

3.   Also applicable thread safety of the stack class.

For example, we create an instance of stack class with initial capacity by using its default constructor and Inserting (Push) three values into the stack. After inserting elements we have retrieve (Pop) the elements.

using System;
using System.Collections;
namespace StackEx
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
 
            Stack st = newStack();
       // Inserting(Pop) values into the stack
            st.Push("Hi");
            st.Push("Hello");
            st.Push("Everyone");
            Console.WriteLine(@"Let us try to use stack in the program ");
            Console.WriteLine();
            Console.WriteLine("Count: {0}",st.Count);
            Console.WriteLine();
            Console.WriteLine("Values:");
            Console.WriteLine();
            PrintValues(st);
            Console.ReadKey();
        }
         // For retrieving(Pop) values from the stack
        publicstaticvoid PrintValues(IEnumerable MyCollection)
        {
            foreach (Object obj in MyCollection)
            {
                Console.Write("{0}",obj);
                Console.WriteLine();
            }
        }
    }
}
 


Output:


Stack Class in C#

 


Updated 07-Sep-2019

Leave Comment

Comments

Liked By