Skip to content

Stacks in C#

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!

Introduction

In this lesson, we will see how to implement a stack in C#

Stacks are usually used when data reversal is needed or is a solution to our problems as we will see in the example at the end of

Defining a stack

we can define a stack in the same way we define any collection

here we will define a stack that takes integer numbers

Adding and viewing data in a Stack

We can add elements to our stack using the method Push()

and we can print the element on the top of the stack using Peek() which will return the element on the top without removing it

Now lets add elements to our stack and every time we will print the value on top

Now let’s run the application

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4ebda88d-6774-4cbd-aa73-9eae69a79853/Untitled.png

as we run the application we will see that every time we use the Peek() method we will get the last value we Pushed into our stack

Removing Elements from a Stack

To remove elements from our stack we will use the method Pop()

which will remove the element on top of the stack and will return it at the same time

of course, we don’t want to use Pop() on an empty stack since this will throw an error so first we need to check the Count property of the stack which will return how many elements are in our stack

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b947d0f4-3ae1-47b2-92cd-3e05fb3dc36c/Untitled.png

As we can see the elements we Popped out of the stack in the opposite way we pushed them (in reverse), this is why we consider the stack to be a LIFO last in first out, the last element that entered a stack will be the first element to exit a stack

Example Reversing an array

one of the many applications of stacks is data reversal in the following example we will use a stack to reverse an array

The solution is simple

  1. we will go through the elements of the array from start to end and we will push each element into a stack

2.After that we will pop and print every element in the stack until it’s empty

consider the following array of integers

Now let’s define a new stack that takes integer values

let’s print each element in the array after that we will push it into our stack using Push()

Now let’s using the Pop() method in the stack to remove each element in the stack until it’s empty and whenever we remove an element we will print it as well

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/278af1f3-183f-4036-98eb-0128aa1201a8/Untitled.png

Complete Code

Challenge: Check if a Number is a Palindromic Number Using a Stack

Take a number from the user and check if the number is a palindromic number or not then print the result

A palindromic number is a number that is the same if you read it from left to right or from right to left, In other words, the number will be the same in reverse

Example on palindromic numbers: 11, 101, 545, 123321, 9889

Solution

Without using a stack we would need a mathematical way to extract each digit in our number from the right store it in a list and compare each digit in our list with the digits of the number we have

or we can count how many digits we have to make a loop that has 2 variables i & j and run each of them from a different side of the number and compare the indexes of j & i which represents digits of the number, We also will have to make sure that we don’t touch the middle digit in case the number was composed of an odd number of digits since we won’t have a digit to compare it with

so it’s going to be a lengthy process.

But a stack can be used to solve this issue like magic we simply need to reverse the number using a stack and if the reversed number is the same as the original number then it’s a palindromic number !.

as easy as it sounds the implementation goes something like this

Solution 1

Solution 2 concatenation

More simplified solution

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide