It's a data structure from which items can only be removed in the order in which they're added.
So the first item added is the first to be removed.
This is called a 'first in, first out' or FIFO data structure.
.NET has both generic and non-generic implementations of a queue. Where the items are all of the same type the generic implementation (System.Colloections.Generic.Queue) should be preferred.
Both classes provide (amongst others) the following methods:
Enqueue - to add a new item to the tail of the queue.
Dequeue - to retrieve and remove the item at the head of the queue.
Peek - to retrieve the item at the head of the queue without removing it.
There's also a Count property so you can check whether there's any items in the queue before attempting to retrieve the head one.
Abhay ShankerPosted Mar 19, 2014, 2:10 AM
Queue is a FIFO collection. It processes elements
in a first-in, first-out order.
The following table lists some of the commonly used
methods of the Queue class
The following example demonstrates use of Stack:
using System;
using System.Collections;
namespace CollectionsApp
{
class Program
{
static void Main(string[] args)
{
Queue q = new Queue();
q.Enqueue('A');
q.Enqueue('B');
q.Enqueue('C');
q.Enqueue('D');
Console.WriteLine("Current queue: ");
foreach (char c in q)
Console.Write(c + " ");
Console.WriteLine();
q.Enqueue('E');
q.Enqueue('E');
Console.WriteLine("Current queue: ");
foreach (char c in q)
Console.Write(c + " ");
Console.WriteLine();
Console.WriteLine("Removing some values ");
char ch = (char)q.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
ch = (char)q.Dequeue();
Console.WriteLine("The removed value: {0}", ch);
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result:
Current queue:
A B C D
Current queue:
A B C D E F
Removing values
The removed value: A
The removed value: B
VulpesPosted Mar 18, 2014, 6:43 PM
The output is: