[Non-reproduced original address] Original address
If you have any questions, please click the original address to leave a message
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <assert.h> #define N 30 typedef struct WORD{//Save all words starting with value, words starting with different letters struct WORD *next; char *word; } Word; typedef struct NODE{//Save all words 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("Enter the inserted string:\n"); while(fgets(value,N,stdin)!=NULL) { value[strlen(value)-1]='\0';//Process the newline character read by fgets into a terminator 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;//Get the first character of the inserted string if(isalpha(first_letter)==0) //If the first character is not a letter, return on error return -1; while((current_node=*rootp)!=NULL&¤t_node->letter<first_letter) rootp=¤t_node->next; /*If current_node==NULL or the letter in the current node is greater than or equal to first_letter, jump out of the loop*/ if(current_node==NULL||current_node->letter>first_letter) /* If the current node is NULL, or the character index of the current node is greater than first_letter Create a new node and insert into the index table */ { new_node=(Node*)malloc(sizeof(Node)); if(new_node==NULL) return -1; new_node->letter=first_letter;//Insert the node into the index table new_node->next=current_node; new_node->data=NULL; *rootp=new_node; current_node=new_node; /*Assign the newly inserted node to current_node*/ } wordlink=¤t_node->data;//Handle the case where the characters in the index table are equal to the characters you want to insert while((current_word=*wordlink)!=NULL) //Insert words in the secondary linked list, first find the insertion position { num=strcmp(current_word->word,the_word); if(num>=0) break; wordlink=¤t_word->next; } if(current_word!=NULL&&num==0)//current_word is not empty, and num is 0, then word already exists return -1; new_word=(Word*)malloc (sizeof( Word)); if(new_word==NULL) return -1; new_word->word=(char*)malloc(strlen(the_word)+1);//Allocate space for words if(new_word->word==NULL) return -1; strcpy(new_word->word,the_word);//Insert the word into the secondary linked list 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; } }