Introduction to Stack In C#
The following article presents a general definition of the stack data structure and its most common functions.This article explores a sample stack implementation as a .NET class in C#, as well as some interesting usage scenarios.
.NET includes a Stack class inside the System.Collections namespace. It is efficient because it is implemented using a System.Collections.ArrayList, so if you need to consume a stack, it is a better idea to use the built in .NET stack class. Just for fun and for educational purposes, the following article presents a simple way to implement a stack and sample stack consumers.
Definition of Stack
A stack is a data structure that allows to add and remove objects at the same position. The last object placed on the stack is the first one to be removed following a Last In First Out (LIFO) data storing method.
Common Functions
The most common functions to manipulate a stack are
- Push(element): Adds a new object to the last position of the stack.
- Pop(): Returns and removes the last object of the stack.
- Peek(): Returns the last object without removing it.
- IsEmpty(): Validates if the stack is empty.
Implementation
There are many ways to implement a stack. I will use an array to keep the demonstration simple, even though you might consider using a linked list to allow dynamic resizing. I will define the maximum size of the stack at compilation time, but you might also define it at runtime. I will use an index to store the last object position (bottom of the stack) at any time.
Each time a Push() operation is done, I validate if the stack has enough space, I increment the index, and add the new object. Each time a Pop() operation is done, I validate if the stack IsEmpty(), I decrease the index, and return the last object. Each time a Peek() operation is done, I validate if the stack IsEmpty(), and I return the last object.
The following sample code illustrates a sample C# stack implementation
using System;
namespace DotNetTreats.DataStructures
{
public class Stack
{
private const int MaxSize = 100;
private object[] _items = new object[MaxSize];
private int _currentIndex = -1;
public Stack()
{
// Constructor
}
public void Push(object element)
{
if (_currentIndex >= MaxSize - 1)
{
throw new InvalidOperationException("Stack is full");
}
_currentIndex++;
_items[_currentIndex] = element;
}
public object Pop()
{
if (IsEmpty())
{
throw new InvalidOperationException("No elements in stack");
}
object element = _items[_currentIndex];
_items[_currentIndex] = null;
_currentIndex--;
return element;
}
public object Peek()
{
if (IsEmpty())
{
throw new InvalidOperationException("No elements in stack");
}
return _items[_currentIndex];
}
public bool IsEmpty()
{
if (_currentIndex < 0)
{
return true;
}
else
{
return false;
}
}
}
}
Listing 1. C# Stack implementation
Usage Scenarios
Scenario 1 : Simple consumer
Once the stack is implemented, a program can consume it. The following console application represents a simple way to consume the stack.
Mohamed Gani MnPosted Nov 6, 2015, 11:13 AM
Nice Explain
kamal pradhanPosted Jul 20, 2010, 5:32 PM
how to write simple or small small program in stack(push,pop,peek) using c#
yinguang zhangeditedPosted Sep 16, 2008, 10:34 PMEdited Sep 16, 2008, 10:35 PM
it waste time for writing this article . why code here to implement increasing the stack's capacity