我正在尝试用 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/