Export (0) Print
Expand All

Queue.Enqueue Method

Adds an object to the end of the Queue.

[Visual Basic]
Public Overridable Sub Enqueue( _
   ByVal obj As Object _
)
[C#]
public virtual void Enqueue(
 object obj
);
[C++]
public: virtual void Enqueue(
 Object* obj
);
[JScript]
public function Enqueue(
   obj : Object
);

Parameters

obj
The object to add to the Queue. The value can be a null reference (Nothing in Visual Basic).

Remarks

If Count already equals the capacity of the Queue, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new element. The new capacity is determined by multiplying the current capacity by the growth factor, which is determined when the Queue is constructed.

If Count is less than the capacity of the internal array, this method is an O(1) operation. If the internal array needs to be reallocated to accommodate the new element, this method becomes an O(n) operation, where n is Count.

Example

[Visual Basic, C#, C++] The following example shows how to add elements to the Queue, remove elements from the Queue or just view the element at the beginning of the Queue.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesQueue
    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("The")
        myQ.Enqueue("quick")
        myQ.Enqueue("brown")
        myQ.Enqueue("fox")
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ, ControlChars.Tab)
        
        ' Removes an element from the Queue.
        Console.WriteLine("(Dequeue)" + ControlChars.Tab + "{0}", myQ.Dequeue())
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ, ControlChars.Tab)
        
        ' Removes another element from the Queue.
        Console.WriteLine("(Dequeue)" + ControlChars.Tab + "{0}", myQ.Dequeue())
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ, ControlChars.Tab)
        
        ' Views the first element in the Queue but does not remove it.
        Console.WriteLine("(Peek)   " + ControlChars.Tab + "{0}", myQ.Peek())
        
        ' Displays the Queue.
        Console.Write("Queue values:")
        PrintValues(myQ, ControlChars.Tab)
    End Sub
    
    Public Shared Sub PrintValues(myCollection As IEnumerable, _
       mySeparator As Char)
       
        Dim myEnumerator As System.Collections.IEnumerator = _
           myCollection.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Queue values:    The    quick    brown    fox
' (Dequeue)    The
' Queue values:    quick    brown    fox
' (Dequeue)    quick
' Queue values:    brown    fox
' (Peek)       brown
' Queue values:    brown    fox 

[C#] 
using System;
using System.Collections;
public class SamplesQueue  {

   public static void Main()  {

      // Creates and initializes a new Queue.
      Queue myQ = new Queue();
      myQ.Enqueue( "The" );
      myQ.Enqueue( "quick" );
      myQ.Enqueue( "brown" );
      myQ.Enqueue( "fox" );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ, '\t' );

      // Removes an element from the Queue.
      Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ, '\t' );

      // Removes another element from the Queue.
      Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ, '\t' );

      // Views the first element in the Queue but does not remove it.
      Console.WriteLine( "(Peek)   \t{0}", myQ.Peek() );

      // Displays the Queue.
      Console.Write( "Queue values:" );
      PrintValues( myQ, '\t' );
   }


   public static void PrintValues( IEnumerable myCollection, char mySeparator )  {
      System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Queue values:    The    quick    brown    fox
(Dequeue)    The
Queue values:    quick    brown    fox
(Dequeue)    quick
Queue values:    brown    fox
(Peek)       brown
Queue values:    brown    fox
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

void PrintValues( IEnumerable* myCollection, Char mySeparator )  {
   System::Collections::IEnumerator* myEnumerator = myCollection->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::Write( "{0}{1}", __box(mySeparator), myEnumerator->Current );
   Console::WriteLine();
}

int main()  {
   // Creates and initializes a new Queue.
   Queue* myQ = new Queue();
   myQ->Enqueue( S"The" );
   myQ->Enqueue( S"quick" );
   myQ->Enqueue( S"brown" );
   myQ->Enqueue( S"fox" );

   // Displays the Queue.
   Console::Write( S"Queue values:" );
   PrintValues( myQ, '\t' );

   // Removes an element from the Queue.
   Console::WriteLine( S"(Dequeue)\t{0}", myQ->Dequeue() );

   // Displays the Queue.
   Console::Write( S"Queue values:" );
   PrintValues( myQ, '\t' );

   // Removes another element from the Queue.
   Console::WriteLine( S"(Dequeue)\t{0}", myQ->Dequeue() );

   // Displays the Queue.
   Console::Write( S"Queue values:" );
   PrintValues( myQ, '\t' );

   // Views the first element in the Queue but does not remove it.
   Console::WriteLine( S"(Peek)   \t{0}", myQ->Peek() );

   // Displays the Queue.
   Console::Write( S"Queue values:" );
   PrintValues( myQ, '\t' );
}

/*
This code produces the following output.

Queue values:   The     quick   brown   fox
(Dequeue)       The
Queue values:   quick   brown   fox
(Dequeue)       quick
Queue values:   brown   fox
(Peek)          brown
Queue values:   brown   fox
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Queue Class | Queue Members | System.Collections Namespace | Dequeue | Peek

Show:
© 2014 Microsoft