Обмен данными в связном списке

Здравствуйте, помогите сделать обмен данными в связном списке на Языке С, вроде бы все работает правильно, только значения в таблице не меняются.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char name[50];
    int length;
    struct {
        char source[50];
    } RiverSource;
    struct {
        int flow;
    } RiverFlow;

} River;

typedef struct Node {
    River riv;
    struct Node* next;
} Node;

void append(Node** head, River river);
void FillRiverStruct(Node** head);
void PrintInfoRivers(Node* head);
void ReplaceTwoObjectsRiver(Node** head);

int main() {
    Node* head = NULL;
    FillRiverStruct(&head);
    PrintInfoRivers(head);
    ReplaceTwoObjectsRiver(&head);
    PrintInfoRivers(head);
    return 0;
}

//Добавление в связный список
void append(Node** head, River river) {
    Node* new_node = (Node*)malloc(sizeof(Node));
    Node* last = *head;
    new_node->riv = river;
    new_node->next = NULL;
    if (*head == NULL) {
        *head = new_node;
        return;
    }
    while (last->next != NULL) {
        last = last->next;
    }
    last->next = new_node;

}
//Создание River и добавление в связный список
void FillRiverStruct(Node** head) {
    printf("Enter number of Rivers: ");
    int numRivers;
    scanf("%d", &numRivers);
    getchar();
    for (int i = 0; i < numRivers; i++) {
        River river;
        printf("Fill information for River %d\n", i + 1);
        printf("=========================\n");

        printf("Enter name of River: ");
        fgets(river.name, sizeof(river.name), stdin);

        printf("=========================\n");
        printf("Enter length of River: ");
        scanf("%d", &river.length);
        getchar(); // Consume the newline

        printf("=========================\n");
        printf("Enter source of River: ");
        fgets(river.RiverSource.source, sizeof(river.RiverSource.source), stdin);

        printf("=========================\n");
        printf("Enter flow of River: ");
        scanf("%d", &river.RiverFlow.flow);
        getchar();
        append(head, river);
        printf("Element appended!\n");
    }
}

//Вывод содержимого связного списка
void PrintInfoRivers(Node* head){
    if (!head){
        return;
    }
    Node* current = head;
    printf("________________________________________________________________\n");
    printf("|        River of Russia   |                 |                  |\n");
    printf("|---------------|----------|      Source     |       Flow       |\n");
    printf("|     Name      |  Length  |      River      |       River      |\n");
    printf("|---------------|----------|-----------------|------------------|\n");
    while (current != NULL) {
        printf("|   %8s|%24d  |%13s|%57d      |\n", current->riv.name, current->riv.length, current->riv.RiverSource.source, current->riv.RiverFlow.flow);
        printf("|---------------|----------|-----------------|------------------|\n");
        current = current->next;
    }
}

//Обмен данными 2-х рек.
void ReplaceTwoObjectsRiver(Node** head) {
    if (*head == NULL || (*head)->next == NULL) {
        printf("There are less than two elements in the list.\n");
        return;
    }

    char r1[50];
    char r2[50];

    printf("Enter River1 name: ");
    scanf("%s", r1);

    printf("Enter River2 name: ");
    scanf("%s", r2);

    // Find the nodes to be swapped
    Node* prev1 = NULL;
    Node* curr1 = *head;
    while (curr1 != NULL && strcmp(curr1->riv.name, r1) != 0) {
        prev1 = curr1;
        curr1 = curr1->next;
    }

    Node* prev2 = NULL;
    Node* curr2 = *head;
    while (curr2 != NULL && strcmp(curr2->riv.name, r2) != 0) {
        prev2 = curr2;
        curr2 = curr2->next;
    }
    if (curr1 == NULL || curr2 == NULL) {
        printf("One of the rivers is not found in the list.\n");
        return;
    }
    // Swap the nodes
    if (prev1 != NULL) {
        prev1->next = curr2;
    } else {
        *head = curr2;
    }

    if (prev2 != NULL) {
        prev2->next = curr1;
    } else {
        *head = curr1;
    }

    Node* temp = curr2->next;
    curr2->next = curr1->next;
    curr1->next = temp;

    printf("Two objects are replaced in the River list.\n");
}

таблицу выводит отлично, только кривовато. введите сюда описание изображения

Затем, когда я хочу поменять местами элементы списка по имени реки, он почему-то не видит реку. введите сюда описание изображения


Ответы (1 шт):

Автор решения: JOKKEU TINKERINO

Решил проблему заменив scanf на fgets. Всем спасибо за помощь!

→ Ссылка