加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#中队列(Queue)实现方法

(2013-02-25 17:14:18)
标签:

queue

队列

队列实现

c队列

杂谈

分类: 技术

C#队列(Queue)实现方法

public class Queue
    {
        object[] data;  //数据元素
        int maxsize;    //最大容量
        int front;      //指向队头
        int rear;       //指向队尾

        ///
        /// 初始化队列
        ///
        ///
        public Queue(int size)
        {
            this.maxsize = size;
            data = new object[size];
            front = rear = -1;

        }

        ///
        /// 最大容量属性
        ///
        public int MaxSize
        {
            get
            {
                return this.maxsize;
            }
            set
            {
                this.maxsize = value;
            }
        }

        ///
        /// 队尾属性
        ///
        public int Rear
        {
            get
            {
                return this.rear;

            }
        }

        ///
        /// 队头属性
        ///
        public int Front
        {
            get
            {
                return this.front;
            }
        }

        ///
        /// 数据属性
        ///
        ///
        ///
        public object this[int index]
        {
            get
            {
                return data[index];
            }
        }

        ///
        /// 获得队列的长度
        ///
        ///
        public int GetQueueLength()
        {
            return rear - front;
        }

        ///
        /// 判断队列是否满
        ///
        ///
        public bool IsFull()
        {
            if (GetQueueLength() == maxsize)
                return true;
            else
                return false;
        }
        //判断队列是否为空
        public bool IsEmpty()
        {
            if (rear == front)
                return true;
            else
                return false;
        }

        ///
        /// 清空队列
        ///
        public void ClearQueue()
        {
            rear = front = -1;
        }

        ///
        /// 入队
        ///
        ///
        public void In(object e)
        {
            if (IsFull())
            {
                return;
            }
            data[++rear] = e;
        }
       
        ///
        /// 出队
        ///
        ///
        public object Out()
        {
            if (IsEmpty())
            {
                return null;
            }
            if (rear - front > 0 && front < data.Length - 1)
            {
                object tmp = data[++front];
                return tmp;
            }
            else
            {
                ClearQueue();
                return null;
            }
        }
    }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有