2 Star 6 Fork 1

陈颖 / Blogs

 / 详情

【队列】- 设计循环队列

已完成
拥有者
创建于  
2021-11-23 10:30

设计循环队列

var MyCircularQueue = function (k) {
    this.arr = new Array(k);
    this.head = 0;
    this.tail = -1;
    this.cnt = 0;
};

/** 
 * @param {number} value
 * @return {boolean}
 */
MyCircularQueue.prototype.enQueue = function (value) {
    if (this.isFull()) return false;
    this.tail += 1;
    this.tail %= this.arr.length;
    this.arr[this.tail] = value;
    this.cnt++;
    return true;
};

/**
 * @return {boolean}
 */
MyCircularQueue.prototype.deQueue = function () {
    if (this.isEmpty()) return false;
    this.head += 1;
    this.head %= this.arr.length;
    this.cnt--;
    return true;
};

/**
 * @return {number}
 */
MyCircularQueue.prototype.Front = function () {
    if (this.isEmpty()) return -1;
    return this.arr[this.head];
};

/**
 * @return {number}
 */
MyCircularQueue.prototype.Rear = function () {
    if (this.isEmpty()) return -1;
    return this.arr[this.tail];
};

/**
 * @return {boolean}
 */
MyCircularQueue.prototype.isEmpty = function () {
    return this.cnt === 0
};

/**
 * @return {boolean}
 */
MyCircularQueue.prototype.isFull = function () {
    return this.cnt === this.arr.length;
};

评论 (0)

陈颖 创建了任务
陈颖 任务状态待办的 修改为已完成
陈颖 添加了
 
算法
标签
展开全部操作日志

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(1)
5470324 chenying1996 1619770733
JavaScript
1
https://gitee.com/ChenYing1996/blogs.git
git@gitee.com:ChenYing1996/blogs.git
ChenYing1996
blogs
Blogs

搜索帮助