| Adding Items to the Queue To add items to the Queue you use Enqueue method. This method takes an object of any type but to keep things simple lets just give it a string object: Queue q = new Queue();
q.Enqueue("So");
q.Enqueue("This");
q.Enqueue("Is");
q.Enqueue("How");
q.Enqueue("Queues");
q.Enqueue("Work");
Viewing a Single Object Without Removing it From the Queue You can use the Peak method to get the topmost object in the queue without removing it from the queue: Console.WriteLine(q.Peek());
Viewing All the Objects in the Queue Without Removing Them
You can read any of the data in the Queue by using an enumerator: System.Collections.IEnumerator en = q.GetEnumerator();
while (en.MoveNext())
{
Console.Write(en.Current " ");
}
Removing Items from the Queue To pop an item from the Queue you can use the Dequeue statement. This returns the topmost object of the queue.
while( q.Count > 0)
{
Console.Write(q.Dequeue " ");
}
|