0

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.

Réda
  • 15
  • 3

1 Answers1

0

The problem has been found and corrected, so this is not an answer.
The following shows why it is better to strive to write less code.

The relevant OP code (where comments don't affect what happens):

    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->rear = added_student; // correction applied here
    }
    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, the same functionality expressed in fewer lines of code:

    // initiate or append to linked list
    if (w_queue->front == NULL)
        w_queue->rear = w_queue->front = added_student;
    else
        w_queue->rear = w_queue->rear->next = added_student;

Strive for clarity in the code, not clarity in the description.

Fe2O3
  • 6,077
  • 2
  • 4
  • 20