I am facing a problem with my linked list enqueue function, where it only adds one node and overwrites the previous node whenever I try to enqueue more than one node. Here is the code for the function:
void enqueue_w(student_info *CSC4352_queue, int *rear, w_list *w_queue, student_info ToEnqueue)
{
// Allocate memory for the enqueued student
node *added_student = (node *)malloc(sizeof(node));
// Error handling
if (added_student == NULL)
{
// If memory allocation fails, stop
return;
}
// Set the new node to the student to enqueue
added_student->student.ID = ToEnqueue.ID;
strcpy(added_student->student.name, ToEnqueue.name);
added_student->next = NULL;
if (w_queue->front == NULL)
{
// If the queue is empty, the front and rear are set to the enqueued student node
w_queue->front = w_queue->rear = added_student;
}
if (w_queue->front->next == NULL)
{
// If the queue only has one node, the next to the front is the added node
w_queue->front->next = added_student;
// The rear will become the new node
w_queue->front = added_student;
}
else
{
// Else, we just enqueue the student after the rear
w_queue->rear->next = added_student;
// Then we set the rear to the added student node
w_queue->rear = added_student;
}
}
and in the main :
// Collecting the student's data
printf("Enter your ID: ");
scanf("%d", &ToEnqueue.ID);
getchar();
printf("\n");
printf("Enter your name : ");
fgets(ToEnqueue.name, 50, stdin);
printf("\n");
enqueue_w(CSC4352_queue, &rear, &w_queue, ToEnqueue);
I have tried modifying the code to use a temporary node and properly assigning pointers, but I am still encountering the same issue. I have also checked my input values and confirmed that they are correct. I suspect there may be an issue with the memory allocation or pointer manipulation, but I am not sure. Any guidance or suggestions would be greatly appreciated.