我试图在C编程中实现一个简单的队列。但我遇到了以下错误。任何人都可以找到我的程序中的错误
#include<stdio.h>
#include<stdlib.h>
#define QUENE_SIZE 10
struct queue_structure{
unsigned int queue_front, queue_rear;
int queue_array[10];
};
void enqueue(struct queue_structure *,int value);
int dequeue(struct queue_structure *);
int queue_full(struct queue_structure *);
int queue_empty(struct queue_structure *);
int main(){
struct queue_structure *queue_pointer = malloc(sizeof(struct queue_structure));
int option,number,i,dequeue_number;
queue_pointer->queue_front = queue_pointer->queue_rear = -1;
while(1){
printf("Enter the option:\n");
scanf("%d",&option);
switch(option){
case 1:
printf("Enter the number to be enqueued:\n");
scanf("%d",&number);
if(queue_full(queue_pointer) != 1){
enqueue(queue_pointer,number);
}
else{
printf("The queue is full");
}
break;
case 2:
if(queue_empty(queue_pointer) != 1){
dequeue_number = dequeue(queue_pointer);
printf("%d has been dequeued",dequeue_number);
}
else{
printf("Your queue is empty\n");
}
break;
case 3:
for(i = 0; i < 10 ; i++ ){
printf("%d",queue_pointer->queue_array[i]);
}
break;
case 4:
exit(0);
break;
}
}
}
void enqueue(struct queue_structure *qs, int number){
if(qs -> queue_front == qs -> queue_rear){
qs->queue_front = qs->queue_rear = -1;
}
else{
(qs -> queue_rear)++;
qs->queue_array[qs->queue_rear] = number;
}
}
int dequeue(struct queue_structure *qs){
int i;
if(qs->queue_front == qs->queue_rear){
qs->queue_front = qs->queue_rear = -1;
}
else{
for(i = qs->queue_front; i < qs->queue_rear ; i++){
qs->queue_array[i] = qs->queue_array[i + 1];
}
}
}
int queue_full(struct queue_structure *qs){
if((qs->queue_rear == 10) && (qs->queue_front == 0)){
return 1;
}
else{
return 0;
}
int queue_empty(struct queue_structure *qs){
if((qs->queue_rear && qs->queue_front) == -1){
return 1;
}
else{
return 0;
}
}
}
我收到以下错误
/tmp/ccLJHnMG.o:在函数main':
queue1.c:(.text+0xda): undefined reference to
queue_empty中
collect2:ld返回1退出状态**
答案 0 :(得分:2)
int queue_full(struct queue_structure *qs){
if((qs->queue_rear == 10) && (qs->queue_front == 0)){
return 1;
}
else{
return 0;
}
} <<<<<<<<<<<<<<<<<<<<<<
int queue_empty(struct queue_structure *qs){
...
答案 1 :(得分:2)
最后的花括号是错位的,它应该是这样的:
int queue_full(struct queue_structure *qs){
if((qs->queue_rear == 10) && (qs->queue_front == 0)){
return 1;
}
else{
return 0;
}
} /* added the }*/
int queue_empty(struct queue_structure *qs){
if((qs->queue_rear && qs->queue_front) == -1){
return 1;
}
else{
return 0;
}
}
/* there was a } here that I've removed */
其他queue_empty
在queue_full
内定义。这不是标准C,但它似乎被gcc
作为扩展支持,因此在编译期间没有错误。
使用-pedantic
编译代码时,gcc会对其进行标记:
aix@aix:~$ gcc -pedantic qq.c
qq.c: In function ‘queue_full’:
qq.c:78: warning: ISO C forbids nested functions
qq.c:78: warning: ISO C90 forbids mixed declarations and code