'Peek'에 해당되는 글 1건

  1. 2007/08/27 Queue Tutorial In C#
2007/08/27 08:31

Queue Tutorial In C#

Adding Items to theQueue
To add items to the Queue you useEnqueue 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(qPeek());


 

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 theDequeue statement. This returns the topmost object of the queue.

while( q.Count > 0)
{
   Console.Write(q.Dequeue   " ");
}

Trackback 0 Comment 0