【数据结构】循环队列

上次实现了数组队列,这次来实现循环队列
循环队列的几个要点,front指向队头元素,tail指向队尾元素的下一个位置,front=tail时队列为空,(front+1)% data.Length = tail时队列为满,还是会使用第一节所编写的数组类做最底层。

实现

 class LoopQueue<E> : Queue<E>
    {
        private E[] data;
        private int front, tail;
        private int size;
        public LoopQueue(int capacity)
        {
            data = new E[capacity + 1];
            front = 0;
            tail = 0;
            size = 0;
        }
        public LoopQueue():this(10)
        {
            
        }
        public int getCapacity()
        {
            return data.Length - 1;
        }
        public E dequeue()
        {
            if (isEmpty())
                throw new ArgumentException("Cannot dequeue from an empty queue");
            E ret = data[front];
            data[front] = default(E);
            front = (front + 1) % data.Length;
            size--;
            if (size == getCapacity() / 4 && getCapacity() / 2 != 0)
                resize(getCapacity() / 2);
            return ret;

        }

        public void enqueue(E e)
        {
            if ((tail + 1) % data.Length == front)
                resize(getCapacity() * 2);
            data[tail] = e;
            tail = (tail + 1) % data.Length;
            size++;
        }
        private void resize(int newCapacity)
        {
            E[] newData = new E[newCapacity + 1];
            for(int i = 0; i < size; i++)
            {
                newData[i] = data[(i + front) % data.Length];
            }
            data = newData;
            front = 0;
            tail = size;
        }
        public E getFront()
        {
            if (isEmpty())
                throw new ArgumentException("Queue is empty");
            return data[front];
            
        }

        public int getSize()
        {
            return size;
        }

        public bool isEmpty()
        {
            return front == tail;
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"Queue: size ={size},capacity ={getCapacity()}");
            sb.Append("front [");
            for (int i = front; i !=tail; i=(i+1)%data.Length)
            {
                sb.Append(data[i]);
                if ((i+1)% data.Length!=tail)
                    sb.Append(", ");
            }
            sb.Append("] tail");
            return sb.ToString();
        }
    }

测试

static void Main(string[] args)
        {
            LoopQueue<int> queue = new DataStructure.LoopQueue<int>();
            for (int i = 0; i < 10; i++)
            {
                queue.enqueue(i);
                Console.WriteLine(queue);
                if (i % 3 == 2)
                {
                    queue.dequeue();
                    Console.WriteLine(queue);
                }
            }
            Solution su = new Solution();
            Console.ReadKey();
        }

在这里插入图片描述

可以看到结果和预期是一样的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Maybe_ch

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值