c queue Queue learn sample sharing
- 2020-05-26 10:03:39
- OfStack
A collection of > Queue Queue > Create a queue
The System.Collections.Queue class provides four overloaded constructors.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Construct using the default constructor Queue
Queue qu = new Queue();
qu.Enqueue(" Elements of the queue 1");
qu.Enqueue(" Elements of the queue 2");
qu.Enqueue(null);
// Using the implementation ICollection The class instance of the interface, here is the list of arrays, constructed Queue
Queue qu2 = new Queue(new string[5] { " Elements of the queue 1", " Elements of the queue 2", " Elements of the queue 3", " Elements of the queue 4", " Elements of the queue 5" });
// Use the initial capacity of 20 Individual element structure Queue.
Queue qu3 = new Queue(20);
// Use the initial capacity of 20 And the equal ratio factor is 2 To construct the Queue.
Queue qu4 = new Queue(20, 2);
}
}
}
The isometric factor means that the current capacity is 5, and the isometric factor is 2 when the capacity is expected to be expanded to 10.
The default capacity of Queue is 32 elements.
A collection of > Queue Queue > Entry and exit of elements
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Queue qu = new Queue();
qu.Enqueue(" The element 1");
qu.Enqueue(" The element 2");
qu.Enqueue(" The element 3");
qu.Enqueue(" The element 4");
qu.Enqueue(" The element 5");
Console.WriteLine(" The original queue is shown below: ");
DisplayResult(qu);
qu.Dequeue();
Console.WriteLine(" Remove the first 1 After one element ");
DisplayResult(qu);
qu.Dequeue();
Console.WriteLine(" Remove the first 2 After one element ");
DisplayResult(qu);
Console.ReadLine();
}
static void DisplayResult(Queue qu)
{
foreach (object s in qu)
{
Console.WriteLine(s);
}
}
}
}