【12.8-7】
编写一个函数,把一个新单词插入到问题7所描述的索引表中。函数接受两个参数,一个指向list指针的指针和一个字符串,该字符串假定包含单个单词,如果这个单词原先并未存在索引表中,它应该复制一块动态分配的节点并插入到这个索引表中,如果成功插入返回真,如果该字符原先已经存在于索引表中,或字符串不是以字符开头或其他错误返回假。函数应该维护一个一级链表,节点的排序以字母为序,其余的二级链表则以单词为序排列。
- 第一步构建一个索引列表:
typedef struct WORD {
struct WORD *next;
char *word;
} Word;
typedef struct LIST {
struct LIST *next;
Word *word_list;
char letter;
} List;
//插入索引链表节点函数
List *insert_list(List *list, char ch) {
List *node = (List *)malloc(sizeof(List));
if(node == NULL) {
return NULL;
}
node->letter = ch;
node->next = NULL;
node->word_list = NULL;
if(list == NULL) {
list = node;
} else {
List *this = list;
//找到尾部
printf("Looking for a location to insert:\n");
printf("%c ", this->letter);
while(this->next != NULL) {
this = this->next;
printf("%c ", this->letter);
}
printf("\n");
//插入尾部
this->next = node;
}
printf("insert [%c]\n", node->