用链表实现索引单词表

【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->letter);
    return list;
}

//初始化索引链表
List *init_list() {
    char alphabets[] = "abcdefghij
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。以下是一个用C语言链表实现的KWIC索引程序的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE_LEN 1000 #define MAX_WORD_LEN 50 typedef struct node { char *line; struct node *next; } Node; void insert(Node **head, char *line) { Node *new_node = (Node *)malloc(sizeof(Node)); new_node->line = line; new_node->next = NULL; if (*head == NULL) { *head = new_node; } else { Node *curr = *head; while (curr->next != NULL) { curr = curr->next; } curr->next = new_node; } } void free_list(Node *head) { Node *curr = head; while (curr != NULL) { Node *temp = curr; curr = curr->next; free(temp->line); free(temp); } } void kwic_index(Node *head) { Node *curr = head; while (curr != NULL) { char *line = curr->line; int len = strlen(line); char *words[len]; int num_words = 0; // Split line into words char *word = strtok(line, " "); while (word != NULL) { words[num_words++] = word; word = strtok(NULL, " "); } // Rotate words and print for (int i = 0; i < num_words; i++) { for (int j = 0; j < num_words; j++) { printf("%s ", words[(i+j)%num_words]); } printf("\n"); } curr = curr->next; } } int main() { Node *head = NULL; char line[MAX_LINE_LEN]; // Read lines from input while (fgets(line, MAX_LINE_LEN, stdin) != NULL) { int len = strlen(line); if (line[len-1] == '\n') { line[len-1] = '\0'; } char *new_line = (char *)malloc((len+1) * sizeof(char)); strcpy(new_line, line); insert(&head, new_line); } // Generate KWIC index kwic_index(head); // Free memory free_list(head); return 0; } ``` 这个程序可以从标准输入中读取多行输入,并生成KWIC索引。每行输入会被分割成单词,然后对每个单词进行旋转,生成所有可能的旋转结果,并输出到标准输出中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值