其他分享
首页 > 其他分享> > Recycle Queue Sample

Recycle Queue Sample

作者:互联网

public class RecycleQueue<T>
    {
        public int len;
        T[] v;
        int max;
        int front;
        int rear;

        public RecycleQueue(int MAXSIZE)
        {
            max = MAXSIZE;
            v = new T[max];
            len = 0;
            front = 0;
            rear = 0;
        }
       
        public T this[int i]
        {
            get
            {
                return v[(rear - i + max) % max];
            }
        }
        public void test()
        {
            for (int i = 0; i < max; i++)
            {
                Console.Write(v[i]+"\t");
            }
            Console.WriteLine();
        }
        public bool IsVaildRef(int refLen) {
            if (refLen>-1 && refLen <len)
            {
                return true;
            }
            return false;
        }
        //入队操作
        public void Push(T value)
        {
            if ( len ==0)
            {
                v[rear] = value;
                len++;
            }
            else if (len < max)
            {
                rear++;
                v[rear] = value;
                len++;
            }
            else
            {
                v[front] = value;
                rear = front;
                //rear = (rear + 1) % max;
                //溢出
                front = (front + 1) % max;
            }
           

        }
        //求队列长度
        public int GetLength()
        {
            return len;
        }
    }

标签:Sample,Console,int,max,refLen,Queue,Recycle,public,rear
来源: https://www.cnblogs.com/supersdar/p/15579373.html