Advertisement



< Prev
Next >



C# Stack Class





Stack is a non-generic collection class which is used to maintain a last-in, first-out(LIFO) collection of objects i.e. the last element to enter the Stack is the first one to be removed. In other words, Stack can be thought of as a pile of dishes, a new dish is always added to the top of the pile and the first dish to be removed is from the top as well.

Note: Stack. It implements the ICollection, IEnumerable, and ICloneable interfaces.







Important features of Stack







Constructors of Stack


Constructor Description
Stack()
This constructor creates an empty instance of the Stack class by using the default initial capacity.

The capacity of an 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 initializes created an Stack that is empty and has the specified initial capacity.

Stack(ICollection)
This constructor creates a Stack initialized with the elements of an ICollection.






Properties of Stack:


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 the Stack.






Some commonly used methods of Stack class


Methods Description
Object Pop()
This method removes and returns the object at the top of the Stack.

Object Push(Object ob)
This method inserts an object at the top of the Stack.

Object Peek()
This method returns the object at the top of the 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 the Stack.

IEnumerator GetEnumerator()
This method returns an IEnumerator to enumerate the entire Stack.

Object[ ] ToArray()
This method returns an Object array containing all the elements of a Stack.




Advertisement




An example of Stack


In this program we have created a Stack to hold objects and we will add and remove objects from this Stack using its methods described above in the table. Let's see the code now -:

//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);
}
}


Output is :


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


Program Analysis






Converting a Stack to an Object array


In the upcoming program, we will call the ToArray(), which returns an Object array containing all the elements of the invoked Stack.

//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);
}
}


Output is :


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




Program Analysis






Please share this article -




< Prev
Next >
< C# SortedList
C# Queue >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement