C# Stack
C# includes a special type of collection which stores elements in LIFO style (Last In First Out).
Stack allows null value and also duplicate values. It provides a Push() method to add a value and Pop() or Peek() methods to retrieve values.
Use the Push() method to add elements into Stack.
The Pop() method returns and removes elements from the top of the Stack. Calling the Pop() method on the empty Stack will throw an exception.
The Peek() method always returns top most element in the Stack.
Properties
Count- Return the total count of element in the stack.
Method
- Push: Inserts an item at the top of the stack.
- Peek: Returns the top item from the stack.
- Pop: Removes and returns items from the top of the stack.
- Contains: Checks whether an item exists in the stack or not.
- Clear: Removes all items from the stack.
1. Push: The Push() method adds values into the Stack. It allows value of any datatype
Example:
static void Main(string[] args)
{
Stack _stack = new Stack();
_stack.Push(1);
_stack.Push(2);
_stack.Push(3);
foreach (var item in _stack)
Console.WriteLine(item);
Console.ReadLine();
}
2.Peek: The Peek() method returns the last (top-most) value from the stack. Calling Peek() method on empty stack will throw InvalidOperationException. So always check for elements in the stack before retrieving elements using the Peek() method.
The below program return 4 as an output.
Example
class CollectionExamples
{
static void Main(string[] args)
{
Stack _stack = new Stack();
_stack.Push(1);
_stack.Push(2);
_stack.Push(3);
_stack.Push(4);
Console.WriteLine(_stack.Peek());
Console.ReadLine();
}
}
3.Pop(): You can also retrieve the value using the Pop() method. The Pop() method removes and returns the value that was added last to the Stack. The Pop() method call on an empty stack will raise an InvalidOperationException. So always check for number of elements in stack must be greater than 0 before calling Pop() method.
Below program retrurn 3,2 and 1 as an output.
Example
class CollectionExamples
{
static void Main(string[] args)
{
Stack _stack = new Stack();
_stack.Push(1);
_stack.Push(2);
_stack.Push(3);
_stack.Push(4);
_stack.Pop();
foreach (var item in _stack)
Console.WriteLine("items {0}", item);
Console.ReadLine();
}
}
4.Contains():The Contains() method checks whether the specified item exists in a Stack collection or not. It returns true if it exists; otherwise it returns false.
Below program return true as an output.
class CollectionExamples
{
static void Main(string[] args)
{
Stack _stack = new Stack();
_stack.Push(1);
_stack.Push(2);
_stack.Push(3);
_stack.Push(4);
Console.WriteLine("Is Exist : {0}", _stack.Contains(1));
Console.ReadLine();
}
}
5.Clear() :The Clear() method removes all the values from the stack.
class CollectionExamples
{
static void Main(string[] args)
{
Stack _stack = new Stack();
_stack.Push(1);
_stack.Push(2);
_stack.Push(3);
_stack.Push(4);
_stack.clear();
Console.WriteLine("Total {0}",_stack.count);
Console.ReadLine();
}
No comments:
Post a Comment
Please let me know if you have any feedback or doubts.