Stack Collection Example

Here I’m demonstrating some simple tasks in using a Stack<T> collection. This collection has a trait of LIFO (Last In First Out). Meaning that the last element added is the first element in     the collection. This collection is good if the you need this type of behavior from a collection. A set way of adding and removing elements without having to get into complex algorithms of adding and removing items.

StackProgram StackSolution

 

Program.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StackCollection
{
    class Program
    {
        /// <summary>
        /// Demonstrating how the Stack collection type can work.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            // Creating the stack collection object.
            Stack<string> books = createStackCollection();

            // Adding elements to the stack collection.
            addElementsToStack(books);

            Console.WriteLine("****Current Full Books List****");
            foreach (string bookTitle in books)
            {
                // Notice how the bottom element in the stack is displayed first.
                Console.WriteLine(bookTitle);
            }
            Console.WriteLine();

            Console.WriteLine("****Peeking At The First Element****");
            Console.WriteLine(peekAtStackElement(books));
            Console.WriteLine();

            Console.WriteLine("****Removing An Element****");
            removeStackElement(books);
            foreach (string bookTitle in books)
            {
                Console.WriteLine(bookTitle);
            }

            Console.ReadLine();
        }

        // Private Methods
        //
        // Method that creates the stack collection object.
        private static Stack<string> createStackCollection()
        {
            return new Stack<string>();
        }

        // Method that adds elements to the stack collection.
        private static void addElementsToStack(Stack<string> stackCollection)
        {
            // Adding elements to the stack.
            stackCollection.Push("Programming WPF");
            stackCollection.Push("C# Collections Fundamentals");
            stackCollection.Push("C# MVC 4 Reference");
            stackCollection.Push("Parallel Programming Concepts");
        }

        // Method that looks at the first element in the stack collection.
        private static string peekAtStackElement(Stack<string> stackCollection)
        {
            return stackCollection.Peek();
        }

        // Method that removed an element from the stack collection.
        private static void removeStackElement(Stack<string> stackCollection)
        {
            stackCollection.Pop();
        }
    }
}

 

Here I’m creating a Stack type and adding book names to the collection. To add elements to the stack you call on the Push() method. To remove items from the stack you call on the Pop() method. Note that these will remove the element at the top of the stack. If you wish to view an element without adding or removing call on the Peek() method.

You can see from the code that I’m calling on the addElementsToStack(), removeStackElement, and peekAtStackElement() methods. These are all passing parameters to the referenced stack collection.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s