#845 – Using the Generic Queue Class
May 16, 2013 1 Comment
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"));
// Remove guy from front of line -- Ernest Person next = peopleInLine.Dequeue();