Вставить строку в один связанный список - PullRequest
0 голосов
/ 20 октября 2019

Я пытаюсь вставить строку в связанный список других строк. Цель состоит в том, чтобы пользователь ввел любую желаемую строку (str_insert). Затем пользователь должен ввести, после какого слова он хочет вставить строку (new_node).

К сожалению, коду удается вставить слово, но он всегда вставляет его только на второй позиции. Функция, используемая для вставки слова, называется вставкой.

typedef struct dll {
    char data;
    int count;
    struct dll* next;
} dll;

typedef struct dictionary {
    dll * data;
    struct dictionary* next;``
    struct dictionary* prev;
} dictionary;

dll* entry(){
    char data = getc(stdin);
    if (data != '\n'){
        dll* curr = create_dico(data);
        curr->next=entry();
        return curr;
    }
    return NULL;
}

dictionary* insertion(dictionary *dico) {
    printf("Please enter the string you want to insert in your already 
    existing list: \n");
    dictionary * str_insert = malloc(sizeof(dictionary));
    str_insert->data = entry();
    str_insert->next = NULL;

    printf("Please enter after which word you would like to insert the 
    previous entry: \n");
    dictionary* new_node =(dictionary*)malloc(sizeof(dictionary));
    new_node->data = entry();
    new_node->next = dico->next;
    new_node->prev = dico;

    if (dico->next != NULL) {
        str_insert->next = dico->next;
        dico->next = str_insert;
    }
}

1 Ответ

1 голос
/ 21 октября 2019

Мне удалось заставить его работать, реализовав другую функцию, которая сравнивала ввод с уже существующим списком.

Вот окончательный результат:

dictionary* research(dictionary* dico, dll *search){
    dll *save = search;
    while (dico != NULL) {
        dll *tmp = dico->data;
        search = save;
        while(tmp->data == search->data && tmp->next != NULL && search->next != NULL){
            tmp = tmp->next;
            search = search->next;
        }
        if(tmp->data == search->data){
            return dico;
        }
        dico = dico->next;
    }
    return NULL;
}

void insertion(dictionary *dico) {
    printf("Please enter the string you want to insert in your already existing list: \n");
    dictionary * str_insert = malloc(sizeof(dictionary));
    str_insert->data = entry();
    str_insert->next = NULL;
    printf("Please enter after which word you would like to insert the previous entry: \n");
    dictionary* search =(dictionary*)malloc(sizeof(dictionary));
    search->data = entry();
    search = research(dico, search->data);
    if (search == NULL){
        printf("Sorry, this word isn't in the list.\n");
    }
    else{
        if(search->next != NULL){
            search->next->prev = str_insert;
        }
        str_insert->next = search->next;
        search->next = str_insert;
        str_insert->prev = search;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...