C# 通過Queue類來實現隊列的先進先出


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace queue
{
    using System;
    using System.Collections;
    public class SamplesQueue
    {

        public static void Main()
        {

            // Creates and initializes a new Queue.
            Queue myQ = new Queue();
            for (char c = 'a'; c <= 'z'; c++)
                myQ.Enqueue(c);  //將一個元素添加到的末尾 Queue 


            // Displays the properties and values of the Queue.
            Console.WriteLine("myQ");
            Console.WriteLine("\tCount:    {0}", myQ.Count);
            Console.Write("\tValues:");
            PrintValues(myQ);

            Console.WriteLine($"myQ中第一個元素為{myQ.Peek()}");    //獲取queue中第一個元素,但不刪除第一個元素
            Console.WriteLine("peek后myQ后序列如下:");
            PrintValues(myQ);
            Console.WriteLine($"myQ中第一個元素為{myQ.Dequeue()}");    //獲取queue中第一個元素,同時刪除第一個元素
            Console.WriteLine($"Dequeue后myQ中第一個元素為{myQ.Peek()}");
            Console.WriteLine("Dequeue后myQ后序列如下:");
            PrintValues(myQ);

        }

        public static void PrintValues(IEnumerable myCollection)
        {
            foreach (Object obj in myCollection)
                Console.Write("    {0}", obj);
            Console.WriteLine();
        }
    }
}

myQ

Count: 26
Values: a b c d e f g h i j k l m n o p q r s t u v w x y z
myQ中第一個元素為a
peek后myQ后序列如下:
a b c d e f g h i j k l m n o p q r s t u v w x y z
myQ中第一個元素為a
Dequeue后myQ中第一個元素為b
Dequeue后myQ后序列如下:
b c d e f g h i j k l m n o p q r s t u v w x y z

 

 

 

Queue類將隊列實現為循環數組。 queue對象實現了在一端插入並從另一端刪除(即先進先出功能)

如存在多個CCD檢測情況下,每個ccd檢測結果信息存儲到Queue中,最后一個ccd檢測完成,然后發送數據到robot(plc)等,通過Queue的Dequeue()方法來達到先進先出功能


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM