对队列执行计数函数

本文关键字:函数 执行 队列 | 更新日期: 2023-09-27 18:29:55

我正在尝试编写对队列中的数字进行计数的代码。问题是,我删除了原始队列中的项目,我宁愿按原样保存。

这是我的代码:

public static int Count(Queue<int> q) //checks how many items there's in the argument queue.
    {
        Queue<int> pos = q;
        Queue<int> qtemp1 = new Queue<int>();
        int counter = 0;
        while (pos != null && pos.IsEmpty())
        {
            qtemp1.Insert(pos.Remove());
            counter++;
        }
        return counter;
}

对队列执行计数函数

尝试q.count

public static int Count(Queue<int> q)
{
    return q.Count;
}