单链表实现单词索引表(C语言)

[非转载 原文地址]原文地址

转载请注明出处,有疑问请点击原文地址留言

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#define N 30
typedef struct WORD{//保存由value开头的所有单词,由不同字母开头的单词 
    struct WORD    *next;
    char        *word;
} Word;


typedef struct NODE{//保存所有的单词
    struct NODE *next;
    char letter;
    Word* data;
} Node;
int insert(Node **rootp,char *str);
void print(Node* rootp);
int main (void)
{
    Node* p1=(Node*)malloc(sizeof(Node));
    if(p1==NULL)
    perror("malloc error");

    p1->next=NULL;
    p1->letter='a';
    Word* the_word;
    the_word=(Word*)malloc(sizeof(Word));
    if(the_word==NULL)
    perror("malloc error");
    
    the_word->next=NULL;
    
    the_word->word=(char*)malloc(N);
    if(the_word->word=NULL)
    perror("malloc error");
    
    the_word->word="ahello";
    
    p1->data=the_word;
    
    char value[N];
    printf("输入插入的字符串:\n");
    while(fgets(value,N,stdin)!=NULL)
    {
        value[strlen(value)-1]='\0';//处理fgets读入的换行符转为结束符 
        insert(&p1,value);
    }
    
    
    print(p1);
    
}
int insert(Node **rootp,char *the_word)
{
    Node* current_node;
    Word* current_word;
    Word** wordlink;
    int first_letter;
    Word* new_word;
    Node* new_node;
    int num;
    
    first_letter=*the_word;//获取插入字符串的首字符
    if(isalpha(first_letter)==0) //如果首字符不是字母,出错返回 
    return -1;
    
    while((current_node=*rootp)!=NULL&¤t_node->letter<first_letter)
    rootp=¤t_node->next;
    
    /*如果current_node==NULL或者当前节点中的letter大于等于first_letter跳出循环*/
    if(current_node==NULL||current_node->letter>first_letter) 
    /*如果当前节点为NULL,或者当前节点的字符索引大于first_letter
    创建一个新的节点并插入到索引表中*/ 
    {
    new_node=(Node*)malloc(sizeof(Node));
    if(new_node==NULL)
    return -1;
    
    new_node->letter=first_letter;//把节点插入到索引表中 
    new_node->next=current_node;
    new_node->data=NULL;
    *rootp=new_node;
    current_node=new_node;
    /*把新插入的节点赋值给current_node*/ 
    }
    
    wordlink=¤t_node->data;//处理索引表中的字符和想要插入字符相等的情况 
    while((current_word=*wordlink)!=NULL) 
    //在二级链表中进行单词的插入,先找到插入位置
    {
        num=strcmp(current_word->word,the_word);
        if(num>=0)
        break;
        wordlink=¤t_word->next;
    }
    if(current_word!=NULL&&num==0)//current_word不为空,且num为0,则word已经存在 
    return -1;
    
    new_word=(Word*)malloc (sizeof( Word));
    if(new_word==NULL)
    return -1;
    
    new_word->word=(char*)malloc(strlen(the_word)+1);//给单词分配空间 
    if(new_word->word==NULL)
    return -1;
    
    strcpy(new_word->word,the_word);//插入单词到二级链表中 
    new_word->next=current_word;
    *wordlink=new_word;
    return 0;
}
void print(Node* rootp)
{
    assert(rootp);
    Node* tmp=rootp;
    Word* word_tmp;
    while(tmp!=NULL)
    {
        word_tmp=tmp->data;
        printf("%c-------%c\n\n",tmp->letter,tmp->letter);
        while(word_tmp!=NULL) 
        {
            printf("%s-->",word_tmp->word);
            word_tmp=word_tmp->next;
        }
        printf("NULL\n");
    printf("%c-------%c\n\n",tmp->letter,tmp->letter);
        tmp=tmp->next;
    }
    
}

猜你喜欢

转载自blog.csdn.net/DT_Billy/article/details/80020271