C# में Queue<T> के अंत में ऑब्जेक्ट जोड़ने (एनक्यू ऑपरेशन) के लिए, हम Enqueue() मेथड का उपयोग करते हैं। Queue एक FIFO (फर्स्ट-इन-फर्स्ट-आउट) संग्रह है जहां तत्व पीछे जोड़े जाते हैं और सामने से हटाए जाते हैं (Dequeue())। यह System.Collections.Generic नेमस्पेस में स्थित है।
उदाहरण 1: Queue में श्रेणियाँ जोड़ना और Contains जांचना
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
Queue<string> queue = new Queue<string>();
queue.Enqueue("Electronics");
queue.Enqueue("Accessories");
queue.Enqueue("Toys");
queue.Enqueue("Books");
queue.Enqueue("Furniture");
queue.Enqueue("Clothing");
queue.Enqueue("Footwear");
queue.Enqueue("Cookware");
queue.Enqueue("Pet Supplies");
Console.WriteLine("Queue में तत्व...");
foreach (var element in queue)
{
Console.WriteLine(element);
}
Console.WriteLine("क्या Queue में तत्व Books है? = " + queue.Contains("Books"));
}
}
आउटपुट
Queue में तत्व... Electronics Accessories Toys Books Furniture Clothing Footwear Cookware Pet Supplies क्या Queue में तत्व Books है? = True
उदाहरण 2: Queue साफ़ करना और गिनना
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
Queue<string> queue = new Queue<string>();
queue.Enqueue("Gary");
queue.Enqueue("Jack");
queue.Enqueue("Ryan");
queue.Enqueue("Kevin");
queue.Enqueue("Mark");
queue.Enqueue("Jack"); // डुप्लिकेट अनुमत
queue.Enqueue("Ryan"); // डुप्लिकेट अनुमत
queue.Enqueue("Kevin"); // डुप्लिकेट अनुमत
Console.Write("तत्वों की संख्या = ");
Console.WriteLine(queue.Count);
queue.Clear();
Console.Write("तत्वों की संख्या (अपडेटेड) = ");
Console.WriteLine(queue.Count);
}
}
आउटपुट
तत्वों की संख्या = 8 तत्वों की संख्या (अपडेटेड) = 0
मुख्य बिंदु:
Enqueue()तत्व को Queue के अंत में जोड़ता है - O(1) ऑपरेशनDequeue()सामने से तत्व हटाता और लौटाता है - O(1)Peek()सामने का तत्व बिना हटाए लौटाता हैContains()तत्व की उपस्थिति जांचता है - O(n)Queueडुप्लिकेट तत्वों की अनुमति देता हैClear()सभी तत्वों को हटा देता हैforeachसामने से पीछे की ओर पुनरावृति करता है (FIFO क्रम)