leetcode 设计循环队列 c语言

typedef struct {
    int head;
    int tail;
    int size;
    int data[1000];
} MyCircularQueue;

/** IsEmpty和IsFull需要提前声明,因为其他函数用到这两个,而这两个函数体本身再最末尾,需要提前声明。也可以直接将函数体写在最前面。 */
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;
    obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    obj->size=k;
    obj->tail=-1;
    obj->head=-1;
    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)) {
       return false;
    }
    if (myCircularQueueIsEmpty(obj)) {
       obj->head=0;
    }
    obj->tail=(obj->tail+1)%obj->size;
    obj->data[obj->tail]=value;
    return true;
}

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

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

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

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

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

void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj);
}

/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * struct 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);
 */

如果IsEmpty和IsFull没有提前声明,而且函数本身也没有定义在调用其功能的函数的前面,leetcode上会出现如下报错:

conflicting types for 'myCircularQueueIsEmpty'

在自己的机器上使用gcc进行编译出现:

test3.c:59:6: 错误:与‘IsEmpty’类型冲突
 bool IsEmpty(MyCircularQueue* obj) {
      ^
test3.c:24:9: 附注:‘IsEmpty’的上一个隐式声明在此
     if (IsEmpty(obj)) {
         ^
test3.c:63:6: 错误:与‘IsFull’类型冲突
 bool IsFull(MyCircularQueue* obj) {
      ^
test3.c:21:9: 附注:‘IsFull’的上一个隐式声明在此
     if (IsFull(obj)) {


解决方式就是需要提前声明,或者将函数体写在前面。

你可能感兴趣的:(leetcode)