为队列实现删除函数时的原型声明错误
Prototype Declaration Error when Implementing a remove function for a Queue
我试图在 C 中实现一个队列,并且在我的 remove(q)
方法中得到了一个原型声明。我想知道我在这里做错了什么,我正在使用旧版本的队列实现作为练习,但不能将它们放在一起。
#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;
}
如果你这样声明你的函数会怎样:
void
empty(struct queue *pq)
{
...
}
和
void
remove(struct queue *pq)
{
...
}
你的函数不是函数。您需要调整它们以将队列作为参数,并且 return 您的值如下所示:
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]);
}
进行这些更改以及您的主要 (if (empty(q)
-> if (empty(&q)
) 中的一项更改编译并输出 hello
.
我试图在 C 中实现一个队列,并且在我的 remove(q)
方法中得到了一个原型声明。我想知道我在这里做错了什么,我正在使用旧版本的队列实现作为练习,但不能将它们放在一起。
#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;
}
如果你这样声明你的函数会怎样:
void
empty(struct queue *pq)
{
...
}
和
void
remove(struct queue *pq)
{
...
}
你的函数不是函数。您需要调整它们以将队列作为参数,并且 return 您的值如下所示:
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]);
}
进行这些更改以及您的主要 (if (empty(q)
-> if (empty(&q)
) 中的一项更改编译并输出 hello
.