1

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{
  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;
  }
}

}

I am getting the following error

/tmp/ccLJHnMG.o: In function main': queue1.c:(.text+0xda): undefined reference toqueue_empty' collect2: ld returned 1 exit status**

1
  • This is not the code you're running! It has at least one syntax error! Commented Jan 26, 2015 at 13:52

2 Answers 2

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){
...
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.