Queue(T) Class represents a first-in, first-out collection of objects. : Queue « Collections Data Structure « C# / C Sharp






Queue(T) Class represents a first-in, first-out collection of objects.

  
using System;
using System.Collections.Generic;

class Example
{
    public static void Main()
    {
        Queue<string> numbers = new Queue<string>();
        numbers.Enqueue("one");
        numbers.Enqueue("two");
        numbers.Enqueue("three");
        numbers.Enqueue("four");
        numbers.Enqueue("five");

        foreach( string number in numbers )
        {
            Console.WriteLine(number);
        }

        Console.WriteLine(numbers.Dequeue());
        Console.WriteLine(numbers.Peek());
        Console.WriteLine(numbers.Dequeue());
        Queue<string> queueCopy = new Queue<string>(numbers.ToArray());

        foreach( string number in queueCopy )
        {
            Console.WriteLine(number);
        }
        string[] array2 = new string[numbers.Count * 2];
        numbers.CopyTo(array2, numbers.Count);
        Queue<string> queueCopy2 = new Queue<string>(array2);

        foreach( string number in queueCopy2 )
        {
            Console.WriteLine(number);
        }

        Console.WriteLine(queueCopy.Contains("four"));

        queueCopy.Clear();
        Console.WriteLine("queueCopy.Count = {0}", queueCopy.Count);
    }
}

   
    
  








Related examples in the same category

1.Put elements into a queue
2.Put user-defined objects to Queue collection
3.Implements the queue data type using an arrayImplements the queue data type using an array
4.A queue class for charactersA queue class for characters
5.illustrates the use of a Queueillustrates the use of a Queue
6.Queue testQueue test
7.Add exception handling to the queue classesAdd exception handling to the queue classes
8.Demonstrate the Queue classDemonstrate the Queue class
9.Priority Queue
10.Priority Queue (2)
11.Dequeue
12.Implements a non-locking queue
13.Task queue
14.Cyclic Queue
15.Syncronized Queue