I was trying to implement a simple queue in C programming. But I encountered with the following error. Can anyone find me what is wrong in my program
#include<stdio.h>
#include<stdlib.h>
#define QUENE_SIZE 10
struct queue_structure{
unsig开发者_如何学Goned 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;
}
}
}
I am getting the following error
/tmp/ccLJHnMG.o: In function main':
queue1.c:(.text+0xda): undefined reference to
queue_empty'
collect2: ld returned 1 exit status**
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){
...
The final curly brace is misplaced, it should be like this:
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 */
Otherwise queue_empty
is defined within queue_full
. This is not standard C, but it appears to be supported by gcc
as an extension, hence no errors during compilation.
When your code is compiled with -pedantic
, gcc does flag it up:
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
精彩评论