How To Use Stacks In C# – Data Structures Made Easy
Stacks are limited access data structures, which means we can't access every element. It only allows us to access the last object we added.
Filter by Category
Stacks are limited access data structures, which means we can't access every element. It only allows us to access the last object we added.
Circular linked lists are an upgrade to generic double linked lists. They can loop around when navigating to adjacent elements.
Linked lists are a type of list that allow us to traverse its elements without using their index positions.
Sorted lists in c# are lists that will automatically sort its elements. This guide explains how to use them and what methods are available.
Generic lists in C# are a data structures that allow us to add and remove objects to store inside without declaring its size.
Array lists are one of the simplest varieties of lists that C# libraries have to offer and this tutorial explains how to use them.
Quicksort algorithm or divide and conquer algorithm is one of the most used sorting algorithms, because of its supperior efficiency.
Bubble sort is one of the simplest sorting algorithms and this guide shows how to implement it in C# programming language.
Insertion sort is one of the simplest sorting algorithms for sorting single dimensional arrays of various different data types.
Selection sort algorithm is one of the simplest sorting algorithms out there. You can use it to sort different data type arrays.
Stacks are special data structures in C# that fall under limited access data structures category. Therefore, we can only access certain objects, but not all of them.
I didn’t mention this with arrays or lists before, but those fall under the random access data structures. In other words, we can access any element and change it or remove it. As I mentioned before, this is not the case with stacks.
First of all, imagine that you have a deck of cards and you can only draw the top card. You can also add cards to this deck. However, you can only place each of them at the top as well. This is basically how stacks work, it’s a last in first out principle.
They have 3 vital methods for their use, which are Push, Pop and Peek. Firstly, Push method will add an object to the top of the stack. Secondly, Pop will remove the last object that we added and return its info. And lastly, Peek will return the info of the top object without changing the stack.
However, there are other methods we could use, like Clear, which will remove all objects stored inside and Contains, which will check if a certain object exists.
Let’s take a look at some C# code to see how ti works in practice.
Stack<string> cards = new Stack<string>();
cards.Push("King of Hearts");
cards.Push("Ace of Spades");
cards.Push("Queen of Clubs");
cards.Push("Jack of Diamonds");
Console.WriteLine("Total cards left: " + cards.Count);
string draw = cards.Pop();
Console.WriteLine("You've drawn: " + draw);
Console.WriteLine("Total cards left: " + cards.Count);
string peek = cards.Peek();
Console.WriteLine("Top card of the deck is: " + peek);
Console.WriteLine("Total cards left: " + cards.Count);
string answer = cards.Contains("Ace of Spades") ? "Yes" : "No";
Console.WriteLine("Ace of Spades in deck? " + answer);
cards.Clear();
Console.WriteLine("Total cards left: " + cards.Count);
For this example I chose to set up a deck of 4 cards and I added each with Push method. Furthermore, by using the Count property, we can confirm that it indeed holds 4 cards.
Next, we can see the Pop method at work, which removed the last card I added and returned its name.
Similarly, by using Peek method, which only returned the name of the last card. Since we already used Pop, it’s going to return the second last card we added in the beginning.
And lastly, we can see Contains and Clear methods at work, of which results are displayed as well.
I hope this short guide helped you gain a better understanding about how stacks in C# work.
You can also download the project I set up for this demonstration and try it out yourself.