c - 为队列实现删除函数时出现原型(prototype)声明错误

标签 c compiler-errors queue

我正在尝试用 C 语言实现一个队列,并且在我的 remove(q) 方法中得到了一个原型(prototype)声明。我想知道我在这里做错了什么,我使用旧版本的队列实现作为练习,但无法将其放在一起。

#include <stdio.h>

#define MAXQUEUE 100
#define TRUE  (1==1)
#define FALSE (1==0)

struct queue {
    int items[MAXQUEUE];
    int front, rear;
};

empty(pq)
struct queue *pq;
{
    return ((pq->front == pq->rear) ? TRUE : FALSE);
}

remove(pq)
struct queue *pq;
{
    if (empty(pq)) {
        printf("queue underflow\n");
        exit(1);
    }

    if (pq->front == MAXQUEUE - 1)
        pq->front = 0;
    else
        (pq->front)++;
    return (pq->items[pq->front]);  
}

int main() {
    struct queue q;

    q.front = q.rear = MAXQUEUE - 1;

    if (empty(q)) {
        printf("hello");
    } else
        printf("\n sod off\n");

    return 0;
}

最佳答案

你的函数不是函数。您需要调整它们以将队列作为参数并返回您的值,如下所示:

int empty(struct queue *pq)
{
    return ((pq->front == pq ->rear) ? TRUE : FALSE);
}

int remove(struct queue *pq)
{
    if(empty(pq)){
        printf("queue underflow\n");
        exit(1);
    }

    if(pq->front == MAXQUEUE-1)
        pq->front = 0;
    else
        (pq->front)++;
    return (pq->items[pq->front]);  
}

进行这些更改以及 main 中的一项更改 (if (empty(q) -> if (empty(&q))) 编译并输出 hello .

关于c - 为队列实现删除函数时出现原型(prototype)声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890650/

相关文章:

java - 只使用 <= 和 >= 形式(不包括 =< 和 =>)进行比较的原因是什么?

c++ - 尝试使用可变参数编译代码时出错

python - 如何克服python中的缩进错误?

python - 在设计循环队列中将queue.front初始化为-1或0

java - 我可以将 Redis 用作计划队列,对 key 过期事件使用react吗

c++ - C 与 C++ 中的枚举存储差异

c - 如何检查变量是否已在 C 中初始化?

c - 将局部变量的地址压入堆栈(程序集)的目的是什么

c - c 中 3x3 矩阵转置的函数

list - 列表的堆栈和队列 "special cases"如何?