• 数据结构和算法-数组模拟环形队列


    队列:

    队列是一个有序列表,遵循先入先出原则,可以用数组或链表实现

    使用场景

    用于排队,按顺序执行

    客户端:

            public static void Main(string[] args)
            {
                ArrayQueue<int> queue = new ArrayQueue<int>(6);
                queue.Push(1);
                queue.Push(2);
                queue.Push(3);
                queue.Push(4);
                queue.Push(5);
    
                Console.WriteLine(queue.Pop());
                queue.Push(6);
                Console.WriteLine(queue.Pop());
                queue.Push(7);
                Console.WriteLine(queue.Pop());
                queue.Push(8);
                Console.WriteLine(queue.Pop());
                queue.Push(9);
                Console.WriteLine(queue.Pop());
                queue.Push(10);
                Console.WriteLine(queue.Pop());
                queue.Push(11);
                Console.WriteLine(queue.Pop());
                queue.Push(12);
                Console.WriteLine(queue.Pop());
                queue.Push(13);
                Console.WriteLine(queue.Pop());
                queue.Push(14);
                Console.WriteLine(queue.Pop());
                
                queue.Print();
    
                Console.ReadKey();
            }
    

    数组队列

    public class ArrayQueue<T>
        {
            private int _front = 0;//队首
            private int _rear = 0;//队尾
            private int _maxSize = 0;
            private T[] _arr = null;
            public ArrayQueue(int maxSize)
            {
                _maxSize = maxSize;
                _arr = new T[maxSize];
            }
            public bool IsFull()
            {
                return (_rear+1)%_maxSize==_front;
            }
            public bool IsEmpty()
            {
                return _front == _rear;
            }
            public void Push(T n)
            {
                if (this.IsFull())
                {
                    throw new Exception("队列已满");
                }
                _arr[_rear] = n;
                _rear = (_rear + 1) % _maxSize;
            }
            public T Pop()
            {
                if (this.IsEmpty())
                {
                    throw new Exception("队列已空");
                }
                T frontNode = _arr[_front];
                _front = (_front + 1) % _maxSize;
                return frontNode;
            }
            public void Print()
            {
                if (this.IsEmpty())
                {
                    Console.WriteLine("队列已空");
                    return;
                }
                int valueCount = this.GetSize();
                for (int i = _front; i <= _front+ valueCount; i++)
                {
                    int index = i % _maxSize;
                    Console.WriteLine($"{i%_maxSize}:{_arr[index]}");
                }
            }
            /// <summary>
            /// 获取当前队列数据个数
            /// </summary>
            /// <returns></returns>
            public int GetSize()
            {
                return (_rear + _maxSize - _front) % _maxSize;
            }
        }
    
    
  • 相关阅读:
    1.2 软件测试的分类和职业生涯
    1.1:软件测试的发展
    1,select查询详解
    7、网页
    6、开学典礼
    5、边框属性和display
    4、盒子模型和margin、padding
    3、字体、背景、和文本的属性
    2、Css中的样式选择器
    16. C# 对象初始化器
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/13284786.html
Copyright © 2020-2023  润新知