Advertisement
Constructor | Description |
---|---|
Stack() | This constructor creates an empty instance of the Stack class by using the default initial capacity. The capacity of a Stack is the number of elements it can hold, as the new elements are added to the Stack, its capacity is automatically increased. |
Stack(int capacity) | This constructor creates a Stack that is empty and has specified its initial capacity. |
Stack(ICollection) | This constructor creates a Stack initialized with the elements of an ICollection. |
Properties | Description |
---|---|
Count | This property gives total number of elements in a Stack. |
IsSynchronzied | This property determines whether a Stack is thread-safe or not. |
SyncRoot | Thie property gets an object that can be used to synchronize access to a Stack. |
Methods | Description |
---|---|
Object Pop() | This method removes and returns the object at the top of a Stack. |
Object Push(Object ob) | This method inserts an object at the top of a Stack. |
Object Peek() | This method returns an object at the top of a Stack without removing it. |
bool Contains(Object o) | This method checks if Stack contains a specific Object. |
void Clear() | This method removes all of the elements from a Stack. |
IEnumerator GetEnumerator() | This method returns an IEnumerator to enumerate an entire Stack. |
Object[ ] ToArray() | This method returns an Object array containing all the elements of a Stack. |
Advertisement
//C# Example of a Stack
using System;
using System.Collections;
class Stack1
{
public static void Main(String[] ar)
{
//Creating a Stack
Stack st= new Stack();
//Calling the Push() method to push elements into the Stack
//New element is always added to the top of the Stack
st.Push(10);
st.Push(23);
st.Push(16);
st.Push(5);
st.Push(29);
//Printing the contents of Stack
Console.WriteLine("The contents of Stack: ");
foreach(int element in st)
Console.WriteLine(element);
//Calling the Pop() method to remove the top element of the Stack
Console.WriteLine("Popping out the top element = "+ st.Pop());
Console.WriteLine("Popping out the next top element = "+ st.Pop());
//Calling the Peek() method which only gets the top element of the Stack, without removing it
Console.WriteLine("Peeking at the current top element = "+ st.Peek());
//Printing the updated contents of Stack
Console.WriteLine("Updated contents of Stack: ");
foreach(int element in st)
Console.WriteLine(element);
}
}
The contents of Stack:
29
5
16
23
10
Popping out the top element = 29
Popping out the next top element = 5
Peeking at the current top element = 16
Updated contents of Stack:
16
23
10
//C# Example of a Stack
using System;
using System.Collections;
class Stack1
{
public static void Main(String[] ar)
{
//Creating a Stack
Stack st= new Stack();
//Calling the Push() method to push elements into the Stack
//New element is always added to the top of the Stack
st.Push(10);
st.Push(23);
st.Push(16);
st.Push(5);
st.Push(29);
//Printing the contents of Stack
Console.WriteLine("The contents of Stack: ");
foreach(int element in st)
Console.WriteLine(element);
//Calling the ToArray() method of Stack
Object[] obArr = st.ToArray();
//Printing the contents of Object array
Console.WriteLine("The contents of Object array initialized from the elements of Stack: ");
foreach(Object element in obArr)
Console.WriteLine(element);
}
}
The contents of Stack:
29
5
16
23
10
The contents of Object array initialized from the elements of Stack:
29
5
16
23
10
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement