C#数据结构学习笔记——队列

本文详细介绍了C#中的队列数据结构,包括数组队列和循环队列的概念及实现。通过循环数组优化了出队操作的时间复杂度,并对比了数组队列与循环队列的性能。此外,还提到了链表队列及其优势。
摘要由CSDN通过智能技术生成

1.队列

先进先出是队列。只能再队头删除元素(出队),在队尾添加元素(入队),应用例如:排队。队列的具体操作:

  1. 入队(队尾添加元素):void Enqueue(e)

  2. 出队(队头删除元素):E Dequeue()

  3. 查看队首元素:E Peek()

  4. 队列的元素数量:int Count { get; }

  5. 队列是否空:bool IsEmpty { get; }

队列接口设计:

	interface IQueue<E>
    {
   
        int Count {
    get; }
        bool IsEmpty {
    get; }
        void Enqueue(E e);
        E Dequeue();
        E Peek();
    }

2.数组队列

基于动态数组实现队列:

	/// <summary>
    /// //动态数组框架
    /// </summary>
    class Array1<T>
    {
   
        private T[] data;//存储元素的静态数组data
        private int N;//存储元素个数N

        public Array1(int capacity)//有参构造
        {
   
            data = new T[capacity];
            N = 0;
        }

        public Array1() : this(10) {
    }//无参构造
        //public Array1()
        //{
   
        //    data = new int[10];
        //    N = 0;
        //}

        private void ResetCapacity(int newCapacity)
        {
   
            T[] newData = new T[newCapacity];
            for (int i = 0; i < N; i++)
                newData[i] = data[i];

            data = newData;
        }

        public int Capacity//获取数组容量
        {
   
            get {
    return data.Length; }
        }

        public int Count//获取数组元素个数
        {
   
            get {
    return N; }
        }

        public bool IsEmpty//判断数组是否为空
        {
   
            get {
    return N == 0; }
        }

        public void Add(int index, T e)//添加元素到数组中的指定索引位置
        {
   
            if (index < 0 || index > N)//判断索引位置是否合法
                throw new ArgumentException("数组索引越界");

            if (N == data.Length)//判断数组容量是否满了
                ResetCapacity(2 * data.Length);

            for (int i = N - 1; i >= index; i--)//从数组末尾向后移动元素
                data[i + 1] = data[i];

            data[index] = e;
            N++;
        }

        public void AddLast(T e)//数组末尾添加元素
        {
   
            Add(N, e);
        }

        public void AddFirst(T e)//数组头部添加元素
        {
   
            Add(0, e);
        }

        public T Get(int index)//获取指定索引位置元素
        {
   
            if (index < 0 || index >= N)
                throw new ArgumentException("数组索引越界");

            return data[index];
        }

        public T GetFirst()//获取首个元素
        {
   
            return Get(0);
        }

        public T GetLast()//获取末尾元素
        {
   
            return Get(N - 1);
        }

        public void Set(int index, T newE)//修改指定索引的元素
        {
   
            if (index < 0 || index >= N)
                throw new ArgumentException("数组索引越界");

            data[index] = newE;
        }

        public bool Contains(T e)//是否包含元素
        {
   
            for (int i = 0; i < N; i++)
            {
   
                if (data[i].Equals(e))
                    return true;
            }

            return false;
        }

        public int IndexOf(T e)//搜索元素的索引
        {
   
            for (int i = 0; i < N; i++)
            {
   
                if (data[i].Equals(e))
                    return i;
            }

            return -1;
        }

        public T RemoveAt(int index)//删除指定索引的元素
        {
   
            if (index < 0 || index >= N)
                throw new ArgumentException("索引超出了数组界限");

            T del = data[index];

            for (int i = index + 1; i <= N - 1; i++)
                data[i - 1] = data[i];

            N--;
            data[N] = default(T);

            if (N == data.Length / 4)
                ResetCapacity(data.Length / 2);

            return del;
        }

        public T RemoveFirst()
        {
   
            return RemoveAt(0);
        }

        public T RemoveLast()
        {
   
            return RemoveAt(N - 1);
        }

        public void Remove(T e)//删除指定元素
        {
   
            int index = IndexOf(e);
            if (index != -1)
                RemoveAt(index);
        }

        public override string ToString()//打印数组
        {
   
            StringBuilder res = new StringBuilder();
            //res.Append(string.Format("Array1: count={0} capacity={1}\n", N, data.Length));
            res.Append("[");
            for (
  • 1
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值