Queue Class Working
The following code shows you the basic functionality of Queue class, for this you have to include namespace System.Collections.
Code
using System.Collections;
public class QueueWorking
{
public static void Main(string[] args)
{
Queue qu = new Queue();
//Adding elements in queue
qu.Enqueue("Osama");
qu.Enqueue("Mursleen");
qu.Enqueue("Maverick");
qu.Enqueue("Mascot");
qu.Enqueue("Veracious");
//Removing the element from queue
qu.Dequeue();
Console.WriteLine("Queue demonstration:");
//Displaying Queue elements through foreach loop
foreach (Object o in qu)
Console.WriteLine(o.ToString());
//Displaying Queue elements through for loop
for (int i = 1; i <= qu.Count; i++)
Console.WriteLine(qu.Dequeue().ToString());
//Displays the object at the beginning of Queue
Console.WriteLine(qu.Peek());
//Displays the total elements in stack
Console.WriteLine(qu.Count);
//Converting Queue to an object array and displaying that new array
object[] queueArray = qu.ToArray();
foreach (Object o in queueArray)
Console.WriteLine(o.ToString());
//Check whether the mentioned object is in Queue or not.
if (qu.Contains("Mascot"))
{
Console.WriteLine("Yes It Is In Queue");
}
}
}
0 comments:
Post a Comment