Overview of Implement Stack Algorithm in C#.Net

Overview of Implement Stack Algorithm in C#.Net

Today, We want to share with you Overview of Implement Stack Algorithm in C#.Net.
In this post we will show you Stack in C#.Net with example, hear for How to use C# Stack Class we will give you demo and example for implement.In this post, we will learn about C# program to implement stack using array with an example.

Introduction: Stack in C#.Net with example

In this postStack Algorithm in C#.Net we will learn about Stack in C#.Net with example.Stack follows a First-in, Last-out process. The basic concept of a stack can be understood by thinking situation of your data as a stack of plates or books where you can only take the top item off the stack in order to remove things from it.

READ :  Difference between Stack and Heap memory in C#.NET

Useful Methods of Stack Class:

Some of the most and useful methods of stack are:

Push() :

This method is used to add an element in the stack.It insert element at the top of the Stack.

Pop() :

This method is used to remove the element from the stack or we can say that this method removes and returns the object at the top of the Stack.

Peek() :

This method returns the element at the top of the Stack without removing it.

Contain() :

It determines whether an element is in the Stack.

Clear() :

This method removes all objects from the Stack.

Example:

Now Create Console Application  in Visual Studio and write below lines of codes.

READ :  Vue Drag and Drop Sortable Example - Vuejs Draggable

Example of the Stack Algorithm in C#.Net
[php]
using System;
using System.Collections;

namespace ConsoleDemo
{
class Program
{
static void Main(string[] args)
{
Stack _Color = new Stack();

_Color.Push(“Red”);
_Color.Push(“Pink”);
_Color.Push(“Blue”);

Console.WriteLine(“Current stack: “);
foreach (string c in _Color)
{
Console.Write(c + ” “);
}

Console.WriteLine(“\n\nAdd 2 new color to stack: “);

Console.WriteLine();

_Color.Push(“White”);
_Color.Push(“Gray”);
Console.WriteLine(“\nCurrent stack: “);
foreach (string c in _Color)
{
Console.Write(c + ” “);
}
Console.WriteLine(“\n\nThe next poppable value in stack: {0}”, _Color.Peek());
Console.WriteLine(“Current stack: “);
foreach (string c in _Color)
{
Console.Write(c + ” “);
}

Console.WriteLine();

Console.WriteLine(“\n\nRemoving values “);
_Color.Pop();

Console.WriteLine(“\n\nCurrent stack: “);
foreach (string c in _Color)
{
Console.Write(c + ” “);
}

Console.ReadKey();
}
}
}

[/php]

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

READ :  vuejs Table Searching Sorting and Pagination

I hope you get an idea about Stack implementation in C#.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment