public void EnqueueAndDequeue(int capacity, int items)
        {
            var seed = 53134;
            var q    = new SeqQueue <T>(capacity);

            Assert.Empty(q);

            // Enqueue some values and make sure the count is correct
            var source = (List <T>)CreateEnumerable(EnumerableType.List, null, items, 0, 0);

            foreach (var val in source)
            {
                q.Enqueue(val);
            }
            Assert.Equal(source, q);

            // Dequeue to make sure the values are removed in the right order and the count is updated
            for (var i = 0; i < items; i++)
            {
                var itemToRemove = source[0];
                source.RemoveAt(0);
                Assert.Equal(itemToRemove, q.Dequeue());
                Assert.Equal(items - i - 1, q.Count);
            }

            // Can't dequeue when empty
            Assert.Throws <InvalidOperationException>(() => q.Dequeue());

            // But can still be used after a failure and after bouncing at empty
            var itemToAdd = CreateT(seed++);

            q.Enqueue(itemToAdd);
            Assert.Equal(itemToAdd, q.Dequeue());
        }
        protected SeqQueue <T> GenericQueueFactory(int count)
        {
            var queue = new SeqQueue <T>(count);
            var seed  = count * 34;

            for (var i = 0; i < count; i++)
            {
                queue.Enqueue(CreateT(seed++));
            }
            return(queue);
        }
예제 #3
0
        //层序遍历(Level Order)
        public void LevelOrder(Node <T> root)
        {
            if (root == null)
            {
                return;
            }
            SeqQueue <Node <T> > sq = new SeqQueue <Node <T> >(50);

            sq.Enqueue(root);//根结点入队
            while (!sq.IsEmpty())
            {
                Node <T> temp = sq.Dequeue(); //结点出队
                Console.WriteLine(temp.Data); //处理当前结点
                if (temp.LChild != null)
                {
                    sq.Enqueue(temp.LChild);
                }
                if (temp.RChild != null)
                {
                    sq.Enqueue(temp.RChild);
                }
            }
        }
예제 #4
0
    void Start()
    {
        SeqQueue <int> seqQueue = new SeqQueue <int>();

        for (int i = 0; i < 3; i++)
        {
            seqQueue.Enqueue(i);
        }
        print("展示队列顶元素:" + seqQueue.Peek());
        print("队列数量:" + seqQueue.Length());
        for (int i = 0; i < 3; i++)
        {
            print(seqQueue.Dequeue());
        }
    }