Hi, I follow one article's help to write a c code that is to make FIFO Circular queue. During Compilation I am getting the following error:
ERROR:
fifo1.c: In function ‘enqueue’:
fifo1.c:43:14: error: ‘rear’ undeclared (first use in this function)
q->items[rear] = x;
^
fifo1.c:43:14: note: each undeclared identifier is reported only once for each function it appears in
fifo1.c: In function ‘dequeue’:
fifo1.c:50:18: error: ‘front’ undeclared (first use in this function)
x = q->items[front];
^
************************************
My code is:
************************************
Code:
#include <stdio.h>
#include <stdlib.h>
#define max 10
typedef struct fifo_q{
int count;
int front;
int rear;
int items[max];
};
void initialize (struct fifo_q *q);
int isEmpty(struct fifo_q q);
int isFull(struct fifo_q q);
void enqueue(struct fifo_q *q, int x);
int dequeue(struct fifo_q *q);
void initialize (struct fifo_q *q)
{
q->count = 0;
q->front = 0;
q->rear = -1;
}
int isEmpty(struct fifo_q q)
{
return (q.count == 0);
}
int isFull(struct fifo_q q)
{
return (q.count == max);
}
void enqueue(struct fifo_q *q, int x){
if (q->count == max){
printf( "%d is not inserted. Queue is full.\n", x);
}
else{
q->count = (q->count + 1);
q->rear = (q->rear + 1) % max;
q->items[rear] = x;
}
}
int dequeue(struct fifo_q *q){
int x;
q->count = q->count - 1;
x = q->items[front];
q->front = (q->front + 1)% max;
return x;
}
int main(){
struct fifo_q fifo1;
int n;
initialize(&fifo1);
// Just to check I am calling the Enqueue function
printf("Enter your data \n");
scanf("%d", &n);
enqueue(&fifo1, n);
......
.....
return 0;
}
Please anyone give the reason why I am getting this declaration related error.