I have a code that aims to enqueue a student into a queue of places available for a course. Here is the function I designed to do it:
int enqueue_CSC(student_info *CSC4352_queue, int *rear, student_info ToEnqueue)
{
// The status is used to determine if the student should be added to the waitlist
int status;
// We check if the queue is full
if (*rear == -1)
{
*rear = 0;
CSC4352_queue[*rear] = ToEnqueue;
status = 1;
}
if (*rear == 4)
{
printf("\tThere are no seats available for the Big Data Analytics (CSC 4352) course.\n");
printf("\tYou will be added to the waiting list.\n");
// status 0 means waitlist
status = 0;
return status;
}
// The queue is not full, increment the rear index and add the student;
CSC4352_queue[++(*rear)] = ToEnqueue;
// status 1 means successfully added
status = 1;
return status;
}
My waitlist function works just fine, but whenever I try to enqueue a student into the course queue, they get duplicated in the array, example:
Input: 101084 John
Printing the queue: 101084 John 101084 John
I have debugged the code and I am sure the queue bears a duplicate, but I do not know how to solve this issue.
Here's the print function:
void print_CSC4352_queue(student_info *CSC4352_queue, int rear, int front)
{
printf("The content of the CSC 4352 queue is : \n");
for (int i = 0; i < rear; i++)
{
printf("\t Student ID : %d\n", CSC4352_queue[i].ID);
printf("\t Student name : %s\n", CSC4352_queue[i].name);
printf("\n");
}
return;
}