Queue Collection Example

The Queue<T> collection class is similar to the Stack collection class. The main difference is that instead of working on the LIFO (Last In First Out) concept it uses the FIFO (First In First Out) concept. These kind of collections work best in a manufacturing environment that uses FIFO concepts in production. Where I currently work this is the process we follow. So the programs we use that utilize this type of concept use this type of collection for various tasks.

QueueProgram QueueSolution

 

Program.cs code contents

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

namespace QueueCollection
{
    class Program
    {
        /// <summary>
        /// Demonstrating how a queue collection works. 
        /// This collection type works on the FIFO (First In First Out) concept.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // Creating the Queue collection.
            Queue<string> todoList = createQueueCollection();

            // Adding elements to the Queue collection.
            string[] itemsToAdd = {"Go To Work", "Work On Collection Fundamentals", "Finish Projects", "Eat Food", "Go To Sleep" };
            addElementsToQueue(itemsToAdd, todoList);
            Console.WriteLine("****Displaying Elements Of The To-Do List****");
            foreach (var item in todoList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            // Removing the first element from the queue.
            Console.WriteLine("****Removing Elements From The To-Do List****");
            removeElementFromQueue(todoList);
            Console.WriteLine();
            Console.WriteLine("****Displaying Revised Elements Of The To-Do List****");
            foreach (var item in todoList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();

            // Taking a peek at the next element in the queue without removing it.
            Console.WriteLine("****Peeking At The Next Element****");
            peekAtQueueElement(todoList);

            Console.ReadLine();
        }

        // Private Methods
        //
        private static Queue<string> createQueueCollection()
        {
            return new Queue<string>();
        }

        // Method that adds elements to the Queue collection.
        private static void addElementsToQueue(string[] args, Queue<string> queueCollection)
        {
            foreach (var items in args)
            {
                queueCollection.Enqueue(items);
            }
        }

        // Method that removes the first element from the Queue collection.
        private static void removeElementFromQueue(Queue<string> queueCollection)
        {
            // Showing the element being removed.
            Console.WriteLine("Element Removed: {0}", queueCollection.Dequeue());
        }

        // Method that peeks at the next item in the queue without actually removing it.
        private static void peekAtQueueElement(Queue<string> queueCollection)
        {
            Console.WriteLine("Next Element: {0}", queueCollection.Peek());
        }
    }
}

 

With this I decided to play around a bit string arrays as parameters and again with referenced objects in memory. So I’ll explain what’s going on in the code. I’m using the createQueueCollection() method to create the Queue object. To add elements to the cue I created a small string array called itemsToAdd. Then calling on the addElementsToQueue with the itemsToAdd array and todoList queue collection as parameters. In the addElementsToQueue method I’m looping thru the string array parameter and calling on the Enqueue() method to add the elements to the todoList queue.

To remove the first element from a queue you can use the Dequeue() method. This is being demostrated in the removeElementFromQueue() method. I’m passing the todoList queue as a parameter, remember that this collection is a reference type, then calling on the Dequeue() method to remove the first element. The Console.WriteLine() displays what element is being removed.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s