Member-only story
Hi everyone, welcome back. In this tutorial, we will be going over the Stack and how to use the built in functions regarding them. A stack is a linear type of data structure that is capable of storing objects. The stack is a last-in-first-out or LIFO data structure which means that the last item placed into the stack is the first item that comes off of the stack. With this introduction out of the way, let’s get into it.
Creating The Stack
Let’s start by creating a Stack. Before we can create the Stack, we need to make sure we have our Stack import, which is included within the System.Collections.Generic package:
using System.Collections.Generic;
Now, we can successfully create our Stack:
Stack<int> myStack = new Stack<int>();
So now, we have created our Stack variable of the ‘int’ data type and named it ‘myStack.’ When we create our Stack, we need to specify the data type. As we can see in the example above, we have used the ‘int’ data type. We can use other data types as well, such as the string and boolean data types.
Adding Elements to The Stack
Now, let’s see how we can add an element to the Stack. We can do this by using the built in Push() function that is provided. Let’s see…