Tutorialspoint.dev

C# | Stack Class

Stack represents a last-in, first out collection of object. It is used when you need a last-in, first-out access to items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item. This class comes under System.Collections namespace.

Characteristics of Stack Class:

  • The capacity of a Stack is the number of elements the Stack can hold. As elements are added to a Stack, the capacity is automatically increased as required through reallocation.
  • If Count is less than the capacity of the stack, Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Push becomes an O(n) operation, where n is Count. Pop is an O(1) operation.
  • Stack accepts null as a valid value and allows duplicate elements.

Constructors

Constructor Description
Stack() Initializes a new instance of the Stack class that is empty and has the default initial capacity.
Stack(ICollection) Initializes a new instance of the Stack class that contains elements copied from the specified collection and has the same initial capacity as the number of elements copied.
Stack(Int32) Initializes a new instance of the Stack class that is empty and has the specified initial capacity or the default initial capacity, whichever is greater.

Example:



// C# code to create a Stack
using System;
using System.Collections;
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Stack
        Stack myStack = new Stack();
  
        // Inserting the elements into the Stack
        myStack.Push("1st Element");
        myStack.Push("2nd Element");
        myStack.Push("3rd Element");
        myStack.Push("4th Element");
        myStack.Push("5th Element");
        myStack.Push("6th Element");
  
        // Displaying the count of elements
        // contained in the Stack
        Console.Write("Total number of elements in the Stack are : ");
  
        Console.WriteLine(myStack.Count);
  
        // Displaying the top element of Stack
        // without removing it from the Stack
        Console.WriteLine("Element at the top is : " + myStack.Peek());
  
        // Displaying the top element of Stack
        // without removing it from the Stack
        Console.WriteLine("Element at the top is : " + myStack.Peek());
  
        // Displaying the count of elements
        // contained in the Stack
        Console.Write("Total number of elements in the Stack are : ");
  
        Console.WriteLine(myStack.Count);
    }
}

Output:

Total number of elements in the Stack are : 6
Element at the top is : 6th Element
Element at the top is : 6th Element
Total number of elements in the Stack are : 6

Properties

Property Description
Count Gets the number of elements contained in the Stack.
IsSynchronized Gets a value indicating whether access to the Stack is synchronized (thread safe).
SyncRoot Gets an object that can be used to synchronize access to the Stack.

Example:

// C# code to Get the number of
// elements contained in the Stack
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Stack
        Stack myStack = new Stack();
  
        // Inserting the elements into the Stack
        myStack.Push("Chandigarh");
        myStack.Push("Delhi");
        myStack.Push("Noida");
        myStack.Push("Himachal");
        myStack.Push("Punjab");
        myStack.Push("Jammu");
  
        // Displaying the count of elements
        // contained in the Stack
        Console.Write("Total number of elements in the Stack are : ");
  
        Console.WriteLine(myStack.Count);
    }
}

Output:

Total number of elements in the Stack are : 6

Methods

Method Description
Clear() Removes all objects from the Stack.
Clone() Creates a shallow copy of the Stack.
Contains(Object) Determines whether an element is in the Stack.
CopyTo(Array, Int32) Copies the Stack to an existing one-dimensional Array, starting at the specified array index.
Equals(Object) Determines whether the specified object is equal to the current object.
GetEnumerator() Returns an IEnumerator for the Stack.
GetHashCode() Serves as the default hash function.
GetType() Gets the Type of the current instance.
MemberwiseClone() Creates a shallow copy of the current Object.
Peek() Returns the object at the top of the Stack without removing it.
Pop() Removes and returns the object at the top of the Stack.
Push(Object) Inserts an object at the top of the Stack.
Synchronized(Stack) Returns a synchronized (thread safe) wrapper for the Stack.
ToArray() Copies the Stack to a new array.
ToString() Returns a string that represents the current object.

Example :

// C# code to Remove all
// objects from the Stack
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Stack 
        Stack myStack = new Stack();
  
        // Inserting the elements into the Stack
        myStack.Push("1st Element");
        myStack.Push("2nd Element");
        myStack.Push("3rd Element");
        myStack.Push("4th Element");
        myStack.Push("5th Element");
        myStack.Push("6th Element");
  
        // Displaying the count of elements
        // contained in the Stack before
        // removing all the elements
        Console.Write("Total number of elements in the Stack are : ");
  
        Console.WriteLine(myStack.Count);
  
        // Removing all elements from Stack
        myStack.Clear();
  
        // Displaying the count of elements
        // contained in the Stack after
        // removing all the elements
        Console.Write("Total number of elements in the Stack are : ");
  
        Console.WriteLine(myStack.Count);
    }
}

Output:

Total number of elements in the Stack are : 6
Total number of elements in the Stack are : 0

Example :

// C# code to Check if a Stack
// contains an element
using System;
using System.Collections;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a Stack of strings
        Stack myStack = new Stack();
  
        // Inserting the elements into the Stack
        myStack.Push("Geeks");
        myStack.Push("Geeks Classes");
        myStack.Push("Noida");
        myStack.Push("Data Structures");
        myStack.Push("GeeksforGeeks");
  
        // Checking whether the element is
        // present in the Stack or not
        // The function returns True if the
        // element is present in the Stack, else
        // returns False
        Console.WriteLine(myStack.Contains("GeeksforGeeks"));
    }
}

Output:

True

Reference:



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter