循环队列-获取队列的长度

分两种情况

①就是队尾rear指针大于队头指针front,length = rear - front

循环队列与队列的链式结构

②队尾rear指针小于队头指针front

length1 = 数组长度 - front

length2 = rear

length = length1 + length2 = rear - front + 数组长度


循环队列与队列的链式结构

③可以总结出

lenght = (rear - front + 数组长度)% 数组长度

//获取循环队列长度
public int GetLength() {
    int maxLength = this._maxLength + 1;
    return (this._rear - this._front + maxLength) % maxLength;
}

首页 我的博客
粤ICP备17103704号