Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เพิ่มวัตถุที่ส่วนท้ายของ Queue - Enqueue Operation ใน C #


หากต้องการเพิ่มวัตถุที่ส่วนท้ายของคิว โค้ดจะเป็นดังนี้ -

ตัวอย่าง

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("Elements in the Queue...");
      foreach(var element in queue){
         Console.WriteLine(element);
      }
      Console.WriteLine("Do the queue has the element Books? = "+queue.Contains("Books"));
   }
}

ผลลัพธ์

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

Elements in the Queue...
Electronics
Accessories
Toys
Books
Furniture
Clothing
Footwear
Cookware
Pet Supplies
Do the queue has the element Books? = True

ตัวอย่าง

เรามาดูตัวอย่างอื่นกัน −

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("Count of elements = ");
      Console.WriteLine(queue.Count);
      queue.Clear();
      Console.Write("Count of elements (updated) = ");
      Console.WriteLine(queue.Count);
   }
}

ผลลัพธ์

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

Count of elements = 8
Count of elements (updated) = 0