The Stack<T> collection
A stack is a linear data structure that allows us to insert and delete items in a particular order. New items are added at the top of the stack. If we want to remove an item from the stack, we can only remove the top item. Since insertion and deletion is allowed from only one end, the item to be inserted last will be the item to be deleted first. Therefore, the stack is called a Last in, First Out (LIFO) collection.
The following diagram depicts a stack, where push represents adding an item to the stack and pop represents deleting an item from the stack:
.NET provides the generic Stack<T>
class for working with stacks. This class contains several constructors that allow us to create either an empty stack or a stack initialized with a collection of elements. Take a look at the following code snippet, where we are creating a stack of strings with three initial elements and an...