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:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Stack myStack = new 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" );
Console.Write( "Total number of elements in the Stack are : " );
Console.WriteLine(myStack.Count);
Console.WriteLine( "Element at the top is : " + myStack.Peek());
Console.WriteLine( "Element at the top is : " + myStack.Peek());
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:
using System;
using System.Collections;
class GFG {
public static void Main()
{
Stack myStack = new Stack();
myStack.Push( "Chandigarh" );
myStack.Push( "Delhi" );
myStack.Push( "Noida" );
myStack.Push( "Himachal" );
myStack.Push( "Punjab" );
myStack.Push( "Jammu" );
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 :
using System;
using System.Collections;
class GFG {
public static void Main()
{
Stack myStack = new 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" );
Console.Write( "Total number of elements in the Stack are : " );
Console.WriteLine(myStack.Count);
myStack.Clear();
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 :
using System;
using System.Collections;
class GFG {
public static void Main()
{
Stack myStack = new Stack();
myStack.Push( "Geeks" );
myStack.Push( "Geeks Classes" );
myStack.Push( "Noida" );
myStack.Push( "Data Structures" );
myStack.Push( "GeeksforGeeks" );
Console.WriteLine(myStack.Contains( "GeeksforGeeks" ));
}
}
|
Reference: