.Net中队列类的操作与系统队列类queue的使用

    class MyQueue
    {
        //存放元素的数组
        private object[] _array;
        //增长因子
        private int _growFactor;

        //队头下标
        private int _head;
        //队尾下标
        private int _tail;
        private int _size;

        private const int _MinGrow = 4;
        //初始容量
        private const int _ShrikThreadhold = 0x20;

        public MyQueue()
            : this(_ShrikThreadhold, 2f)
        {
        }

        public MyQueue(int capacity, float growFactor)
        {
            if (capacity < 0)
            {
                throw new ArgumentOutOfRangeException("capacity", "初始容量不能为负!");
            }
            if ((growFactor < 1.0) || (growFactor > 10))
            {
                throw new ArgumentOutOfRangeException("growFactor", "增长因子应介于1和10之间!");
            }
            this._array = new object[capacity];
            this._head = 0;
            this._size = 0;
            this._tail = 0;
            this._growFactor = (int)(growFactor * 100f);
        }

        //入队
        public virtual void Enqueue(object obj)
        {

            if (this._size == this._array.Length)
            {//队列已满
                int capacity = (int)((this._array.Length * this._growFactor) / 100L);
                if (capacity < (this._array.Length + _MinGrow))
                {
                    capacity = this._array.Length + _MinGrow;
                }
                SetCapacity(capacity);
            }
            this._array[this._tail] = obj;
            this._tail = (this._tail + 1) % this._array.Length;
            this._size++;
        }

        //内存设置
        private void SetCapacity(int capacity)
        {
            object[] dest = new object[capacity];
            if (this._head < this._tail)//尾指针在头指针后
            {
                Array.Copy(this._array, this._head, dest, 0, this._size);
            }
            else//头指针在尾指针后
            {
                Array.Copy(this._array, this._head, dest, 0, this._array.Length - this._head);
                Array.Copy(this._array, 0, dest, this._array.Length - this._head, this._tail);
            }

            this._array = dest;
            this._head = 0;
            this._tail = (this._size == capacity) ? 0 : this._size;
        }


        //出队
        public virtual object Dequeue()
        {
            if (this._size == 0)
            {
                throw new InvalidOperationException("队列为空");

            }
            object obj = this._array[this._head];//出队
            this._array[this._head] = null;
            this._head = (this._head + 1) % this._array.Length;
            this._size--;
            return obj;
        }
        public virtual int Count
        {
            get { return this._size; }
        }
    }



        static void Main(string[] args)
        {

            Program main = new Program();
            main.testQueue();
        }
        private void testQueue()
        {
            Queue myQueue = new Queue();
            //MyQueue myQueue = new MyQueue();//与上面类使用一样
            myQueue.Enqueue(1);
            myQueue.Enqueue(2);
            myQueue.Enqueue(3);
            myQueue.Enqueue(4);

            Console.WriteLine("总数量:"+myQueue.Count.ToString());
            while (myQueue.Count > 0)
            {
                Console.WriteLine("出列:"+myQueue.Dequeue());
            }
            Console.WriteLine("数量:"+myQueue.Count.ToString());
            Console.ReadLine();
        }

.Net中队列类的操作与系统队列类queue的使用_第1张图片


你可能感兴趣的:(正则/算法)