C# Non-Generic Stack

In C# there is both a generic and a non-generic stack. A stack can be described as a last-in first-out data structure, or LIFO for short. New elements are added to the top of the stack and the last element added is the first element available to be removed. The non-generic version, discussed here, does not require a data type to be specified for the elements contained within, so there can be a mixture of types if desired.

The example below shows the declaration of a non-generic stack, with four items added to it using the 'Push' method. In this case, all of the items added are string values.

Stack people = new Stack();

people.Push("Bob Smith");
people.Push("George Jones");
people.Push("Fred Bloggs");
people.Push("Alan White");

It is possible to see how many items there are in the stack using the ‘Count’ property of the stack itself.

Console.WriteLine(people.Count);

In order to see the items in a non-generic stack, without actually removing them, a ‘foreach’ loop can be used.

foreach (string person in people)
{
    Console.WriteLine(person);
}

The output is shown below. It should be noted that the last item added to the stack is displayed first, whilst the first item added is displayed last.

Alan White
Fred Bloggs
George Jones
Bob Smith

It is also possible to view the item at the top of the stack, without actually removing it, using the 'Peek' method.

Console.WriteLine(people.Peek());

If it is necessary to find out if a particular value exists within a non-generic stack then the 'Contains' method can be used. The result of this returns a Boolean 'True' or 'False' value.

Console.WriteLine(people.Contains("George Jones"));

In order to remove individual items from a stack, the ‘Pop’ method can be utilised. The example below assigns the number of items in the stack to a variable. This value is then used to limit the number of iterations in a ‘for’ loop. Within the loop the ‘Pop’ method is used to remove an item from the stack, display it in the console and output the number of items left in the stack.

Stack people = new Stack();

people.Push("Bob Smith");
people.Push("George Jones");
people.Push("Fred Bloggs");
people.Push("Alan White");

int stackHeight = people.Count;

Console.WriteLine("Items in stack: {0}", stackHeight);
Console.WriteLine();

for (int i = 1; i <= stackHeight; i++)
{

    Console.WriteLine("Next item from stack: {0}", people.Pop());
    Console.WriteLine("Items left in stack: {0}", people.Count);
    Console.WriteLine();

}

The resulting output is shown below.

Items in stack: 4

Next item from stack: Alan White
Items left in stack: 3

Next item from stack: Fred Bloggs
Items left in stack: 2

Next item from stack: George Jones
Items left in stack: 1

Next item from stack: Bob Smith
Items left in stack: 0

Finally, if it is necessary to remove all items in a stack in one go, instead of individually with the ‘Pop’ method, there is a ‘Clear’ method to accomplish this.

people.Clear();

Further Reading