How to Use Queues in C# for Efficient Data Processing | Queue Data Structure Tutorial

preview_player
Показать описание
Can you help me to buy a coffee:

using System;
using System.Collections;

class Program
{
static void Main(string[] args)
{
Queue myQueue = new Queue();

// add items to the queue
myQueue.Enqueue("apple");
myQueue.Enqueue("banana");
myQueue.Enqueue("cherry");

// print the items in the queue
foreach (object item in myQueue)
{
Console.WriteLine(item);
}

// remove the first item from the queue
object removedItem = myQueue.Dequeue();
Console.WriteLine("Removed item: " + removedItem);

// print the updated queue
foreach (object item in myQueue)
{
Console.WriteLine(item);
}

// check if an item is in the queue
bool isInQueue = myQueue.Contains("banana");
Console.WriteLine("Is banana in the queue? " + isInQueue);

// clear the queue
myQueue.Clear();
Console.WriteLine("Queue cleared. Count: " + myQueue.Count);
}
}
Рекомендации по теме