LeetCode – Design Circular Queue

题目:

Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. It is also called “Ring Buffer”.

One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.

Your implementation should support following operations:

  • MyCircularQueue(k): Constructor, set the size of the queue to be k.
  • Front: Get the front item from the queue. If the queue is empty, return -1.
  • Rear: Get the last item from the queue. If the queue is empty, return -1.
  • enQueue(value): Insert an element into the circular queue. Return true if the operation is successful.
  • deQueue(): Delete an element from the circular queue. Return true if the operation is successful.
  • isEmpty(): Checks whether the circular queue is empty or not.
  • isFull(): Checks whether the circular queue is full or not.

Example:

MyCircularQueue circularQueue = new MyCircularQueue(3); // set the size to be 3
circularQueue.enQueue(1);  // return true
circularQueue.enQueue(2);  // return true
circularQueue.enQueue(3);  // return true
circularQueue.enQueue(4);  // return false, the queue is full
circularQueue.Rear();  // return 3
circularQueue.isFull();  // return true
circularQueue.deQueue();  // return true
circularQueue.enQueue(4);  // return true
circularQueue.Rear();  // return 4

 

Note:

  • All values will be in the range of [0, 1000].
  • The number of operations will be in the range of [1, 1000].
  • Please do not use the built-in Queue library.

解题:

1. 创建结构体包含head,tail指针, cnt队列的数量;
( 开始的时候没有增加这个变量, 通过tail与head来判断处理起来比较复杂);
2. head设置为0, tail设置为-1, 后续的队列操作时将head 与 tail 与size取余数实现下标的循环。

实现:

typedef struct {
    int *value;
    int head;
    int tail;
    int size;
    int cnt;
    
} MyCircularQueue;
bool myCircularQueueIsEmpty(MyCircularQueue* obj) ;
bool myCircularQueueIsFull(MyCircularQueue* obj);

/** Initialize your data structure here. Set the size of the queue to be k. */

MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue* obj;
    
    if (k == 0){
        return NULL;
    }
    obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    obj->value = (int*)malloc(sizeof(int)*k);
    obj->size=k;
    obj->head=0;
    obj->tail=-1;
    obj->cnt=0;
    return obj;
}

/** Insert an element into the circular queue. Return true if the operation is successful. */
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if (!myCircularQueueIsFull(obj)){  
        obj->tail = (obj->tail+1) % obj->size;
        obj->value[obj->tail]= value;
        obj->cnt += 1;
        return true;
    }
    else{
        return false;
    }
}

/** Delete an element from the circular queue. Return true if the operation is successful. */
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
      if (myCircularQueueIsEmpty(obj)){
          return false;
      }
      else {
          obj->head = (obj->head+1) % obj->size;
          obj->cnt -= 1;
          return true;
      }
}

/** Get the front item from the queue. */
int myCircularQueueFront(MyCircularQueue* obj) {
     if (!myCircularQueueIsEmpty(obj)){
          return obj->value[obj->head];
     }
     else {
         return -1;
     }
}

/** Get the last item from the queue. */
int myCircularQueueRear(MyCircularQueue* obj) {
    if (!myCircularQueueIsEmpty(obj)){
        return obj->value[obj->tail];
    }else{
        return -1;
    }
}

/** Checks whether the circular queue is empty or not. */
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
      if (obj == NULL){
          return false;
      }
      if (obj->cnt == 0){
          return true;
      }
      else{
          return false;
      }
}

/** Checks whether the circular queue is full or not. */
bool myCircularQueueIsFull(MyCircularQueue* obj) {
      if (obj->cnt ==  obj->size ){
          return true;
      }
      else{
          return false;
      }
}

void myCircularQueueFree(MyCircularQueue* obj) {

    free(obj->value);
    free(obj);
}

/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * MyCircularQueue* obj = myCircularQueueCreate(k);
 * bool param_1 = myCircularQueueEnQueue(obj, value);
 
 * bool param_2 = myCircularQueueDeQueue(obj);
 
 * int param_3 = myCircularQueueFront(obj);
 
 * int param_4 = myCircularQueueRear(obj);
 
 * bool param_5 = myCircularQueueIsEmpty(obj);
 
 * bool param_6 = myCircularQueueIsFull(obj);
 
 * myCircularQueueFree(obj);
*/

Runtime: 24 ms
Memory Usage: 13.9 MB

Be First to Comment

发表回复