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;
        }
    }
上一篇:蓄水池抽样算法/水塘采样算法


下一篇:Python--random