孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

     Queue对象类似于数据结构中的队列,先进先出(FIFO)。

    

/*
  Example11_10.cs illustrates the use of a Queue
*/

using System;
using System.Collections;

class Example11_10
{

  public static void Main()
  {

    // create a Queue object
    Queue myQueue = new Queue();

    // add elements to myQueue using the Enqueue() method
    myQueue.Enqueue("This");
    myQueue.Enqueue("is");
    myQueue.Enqueue("a");
    myQueue.Enqueue("test");

    // display the elements in myQueue
    foreach (string myString in myQueue)
    {
      Console.WriteLine("myString = " + myString);
    }

    // get the number of elements in myQueue using the
    // Count property
    int numElements = myQueue.Count;

    for (int count = 0; count < numElements; count++)
    {

      // examine an element in myQueue using Peek()
      Console.WriteLine("myQueue.Peek() = " +
        myQueue.Peek());

      // remove an element from myQueue using Dequeue()
      Console.WriteLine("myQueue.Dequeue() = " +
        myQueue.Dequeue());

    }

  }

}
posted on 2011-05-06 22:07  孤独的猫  阅读(349)  评论(0编辑  收藏  举报