队列循环实现(C#)

C#队列的循环实现:

View Code
class MyQueue <T>
    {
        private const int MAXLIMIT = 10;
        private int count;
        private int rear, front;
        private T[] entry = new T[MAXLIMIT];

        public MyQueue()
        {
            count = 0;
            rear = MAXLIMIT - 1;
            front = 0;
        }

        public bool empty()
        {
            if (count > 0)
                return false;
            else
                return true;
        }

        public ErrorCode append(T item)
        {
            ErrorCode outcome = ErrorCode.success;
            if (count < MAXLIMIT)
            {
                count++;
                rear = (rear + 1) == MAXLIMIT ? 0 : rear + 1;
                entry[rear] = item;
            }
            else
                outcome = ErrorCode.overflow;
            return outcome;
        }

        public ErrorCode serve()
        {
            ErrorCode outcome = ErrorCode.success;
            if (count > 0)
            {
                count--;
                front = (front + 1) == MAXLIMIT ? 0 : front + 1;
            }
            else
                outcome = ErrorCode.underflow;
            return outcome;
        }

        public ErrorCode retrieve(ref T item)
        {
            ErrorCode outcome = ErrorCode.success;
            if (count > 0)
            {
                item = entry[front];
            }
            else
                outcome = ErrorCode.underflow;
            return outcome;
        }

        public bool full()
        {
            if (count == MAXLIMIT)
                return true;
            else
                return false;
        }

        public int size()
        {
            return count;
        }

        public void clear()
        {
            count = 0;
            rear = MAXLIMIT - 1;
            front = 0;
        }

        public ErrorCode serve_and_retrieve(ref T item)
        {
            ErrorCode outcome = ErrorCode.success;
            if (count > 0)
            {
                count--;
                item = entry[front];
                front = (front + 1) == MAXLIMIT ? 0 : front + 1;
            }
            else
                outcome = ErrorCode.underflow;
            return outcome;
        }
    }

 

posted @ 2012-10-24 19:42  zhoutk  阅读(910)  评论(0编辑  收藏  举报