0% found this document useful (0 votes)
5 views

Stack in C

Uploaded by

Venu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Stack in C

Uploaded by

Venu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

C# - Stack<T>

Stack is a special type of collection that stores elements in


LIFO style (Last In First Out). C# includes the generic
Stack<T> and non-generic Stack collection classes. It is
recommended to use the generic Stack<T> collection.

Stack is useful to store temporary data in LIFO style, and


you might want to delete an element after retrieving its
value.
Stack<T> Characteristics
• Stack<T> is Last In First Out collection.
• It comes under System.Collection.Generic namespace.
• Stack<T> can contain elements of the specified type. It
provides compile-time type checking and doesn't
perform boxing-unboxing because it is generic.
• Elements can be added using the Push() method.
Cannot use collection-initializer syntax.
• Elements can be retrieved using the Pop() and the
Peek() methods. It does not support an indexer.
Creating a Stack
You can create an object of the Stack<T> by specifying a type
parameter for the type of elements it can store. The following
example creates and adds elements in the Stack<T> using the Push()
method. Stack allows null (for reference types) and duplicate values.
Example: Create and Add Elements in Stack
Stack<int> myStack = new Stack<int>();
myStack.Push(1);
myStack.Push(2);
myStack.Push(3);
myStack.Push(4);

foreach (var item in myStack)


Console.Write(item + ",");
You can also create a Stack from an array, as shown below.

Example: Create and Add Elements in Stack


int[] arr = new int[]{ 1, 2, 3, 4};
Stack<int> myStack = new Stack<int>(arr);

foreach (var item in myStack)


Console.Write(item + ",");
Stack<T> Properties and Methods:
Property Usage
Count Returns the total count of elements
in the Stack.

Method Usage
Push(T) 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(T) Checks whether an item exists in the
stack or not.
Clear() Removes all items from the stack.
Pop()
The Pop() method returns the last element and removes it
from a stack. If a stack is empty, then it will throw the
InvalidOperationException. So, always check for the
number of elements in a stack before calling the Pop()
method.
Example: Access Stack using Pop()
Stack<int> stack1 = new Stack<int>();
stack1.Push(1);
stack1.Push(2);
stack1.Push(3);
stack1.Push(4);
Console.Write("Number of elements in Stack: {0}", stack1.Count);

while (stack1.Count > 0)


Console.Write(stack1.Pop() + ",");

Console.Write("Number of elements in Stack: {0}", stack1.Count);


Peek()
The Peek() method returns the lastly added value from the stack but
does not remove it. Calling the Peek() method on an empty stack will
throw the InvalidOperationException. So, always check for elements
in the stack before retrieving elements using the Peek() method.

Example: Retrieve Elements usign Peek()


Stack<int> stack1 = new Stack<int>();
stack1.Push(1);
stack1 .Push(2);
stack1.Push(3);
stack1.Push(4);
Console.Write("Number of elements in Stack: {0}", stack1.Count);

if(stack1.Count > 0)
{
Console.WriteLine(stack1.Peek());
Console.WriteLine(stack1.Peek());
}

Console.Write("Number of elements in Stack: {0}", stack1.Count);


Contains()
The Contains() method checks whether the specified element exists in
a Stack collection or not. It returns true if it exists, otherwise false.

Example: Contains()
Stack<int> stack1 = new Stack<int>();
stack1.Push(1);
stack1.Push(2);
stack1.Push(3);
stack1.Push(4);

stack1.Contains(2);
stack1.Contains(10);

You might also like