#845 – Using the Generic Queue Class

A queue is a data structure that allows adding elements at one end of the queue, through Enqueue operations, and removing elements from the other end, through Dequeue operations.

The .NET Framework includes a Queue<T> type, defined in System.Collections.Generic, that implements a queue data type.

When you declare a queue, you indicate the type of objects that the queue will contain.

            Queue<Person> peopleInLine = new Queue<Person>();

You can now add instances of Person objects to the (back of) the queue using the Enqueue method.

            // Ernest - 1st guy in line
            peopleInLine.Enqueue(new Person("Ernest", "Hemingway"));

            // Scott in line behind Ernest
            peopleInLine.Enqueue(new Person("F. Scott", "Fitzgerald"));

845-001
You can remove the object from the front of the queue using the Dequeue method.

            // Remove guy from front of line -- Ernest
            Person next = peopleInLine.Dequeue();

845-002

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #845 – Using the Generic Queue Class

  1. Pingback: Dew Drop – May 16, 2013 (#1,548) | Alvin Ashcraft's Morning Dew

Leave a comment