LeetCode第 622 题:设计循环队列 (C++)

622. 设计循环队列 - 力扣(LeetCode)
根据示例来看,需要设计的循环队列是满载的(不需要预留一个元素位),也就是通过设置一个tag来标志队列是否已满。

但是,将数组元素多分配一个:capacity = k + 1,那么也可以预留出一个位置来。那就采用这个办法吧。。。

至于容器使用内置数组或者vecetor都是可以的。

class MyCircularQueue {
private:
    int head = 0; // 队首
	int tail = 0; // 队尾的下一个
	int capacity; // 容量
	int *array;

public:
    /** Initialize your data structure here. Set the size of the queue to be k. */
    MyCircularQueue(int k) {
        head = 0;
	    tail = 0;
        capacity = k +1;
	    array = new int[capacity];
    }
    
    /** Insert an element into the circular queue. Return true if the operation is successful. */
    bool enQueue(int value) {
        // 队满,插入失败
	    if ((tail+1) % capacity == head)   return false;
	    array[tail] = value;
	    tail = (tail+1) % capacity;
        return true;
    }
    
    /** Delete an element from the circular queue. Return true if the operation is successful. */
    bool deQueue() {
        //队空,删除失败
        if (head == tail)   return false;
		head = (head+1) % capacity;
        return true;
    }
    
    /** Get the front item from the queue. */
    int Front() {
        if (head == tail)   return -1;
        return array[head];
    }
    
    /** Get the last item from the queue. */
    int Rear() {
        if (head == tail)   return -1;
        return array[(tail-1+capacity) % capacity];
    }
    
    /** Checks whether the circular queue is empty or not. */
    bool isEmpty() {
        return head == tail;
    }
    
    /** Checks whether the circular queue is full or not. */
    bool isFull() {
        return (tail+1) % capacity == head;
    }
};

数组换为vector也很简单:

class MyCircularQueue {
private:
    int head = 0; // 队首
	int tail = 0; // 队尾的下一个
	int capacity; // 容量
	vector<int> vec;

public:
    /** Initialize your data structure here. Set the size of the queue to be k. */
    MyCircularQueue(int k) {
        head = 0;
	    tail = 0;
        capacity = k +1;
	    vec.assign(capacity, 0);
    }
    
    /** Insert an element into the circular queue. Return true if the operation is successful. */
    bool enQueue(int value) {
        // 队满,插入失败
	    if ((tail+1) % capacity == head)   return false;
	    vec[tail] = value;
	    tail = (tail+1) % capacity;
        return true;
    }
    
    /** Delete an element from the circular queue. Return true if the operation is successful. */
    bool deQueue() {
        //队空,删除失败
        if (head == tail)   return false;
		head = (head+1) % capacity;
        return true;
    }
    
    /** Get the front item from the queue. */
    int Front() {
        if (head == tail)   return -1;
        return vec[head];
    }
    
    /** Get the last item from the queue. */
    int Rear() {
        if (head == tail)   return -1;
        return vec[(tail-1+capacity) % capacity];
    }
    
    /** Checks whether the circular queue is empty or not. */
    bool isEmpty() {
        return head == tail;
    }
    
    /** Checks whether the circular queue is full or not. */
    bool isFull() {
        return (tail+1) % capacity == head;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值