【发布时间】:2019-10-20 20:01:14
【问题描述】:
我正在尝试将一个字符串插入到其他字符串的链接列表中。 目标是让用户输入他们想要的任何字符串(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;
}
}
【问题讨论】:
-
这是一个字典 - 不是“字典” - 一个“n”就足够了!
标签: c string linked-list