C 在这个trie数据结构实现中,显示功能不起作用。为什么?

C 在这个trie数据结构实现中,显示功能不起作用。为什么?,c,C,当我显示我得到不同的答案,但搜索工作。可能是语义错误。请帮助更正它。它可能在某个地方出错。我不知道它在哪里。请帮我找到它。插入在程序中不起作用。可能是某个地方出错。我需要这个trie数据结构才能工作 #include<stdio.h> #include<stdlib.h> #define maxlength 10 typedef struct node { int isend; struct n

当我显示我得到不同的答案,但搜索工作。可能是语义错误。请帮助更正它。它可能在某个地方出错。我不知道它在哪里。请帮我找到它。插入在程序中不起作用。可能是某个地方出错。我需要这个trie数据结构才能工作

 #include<stdio.h>
    #include<stdlib.h>
    #define maxlength 10
    
    
    typedef struct node
    {
      int isend;
      struct node *branch[27];
    }trinode;
    
    int len,count;
    
    trinode *createnode()
    {
      trinode *new=(trinode *)malloc(sizeof(trinode));
      int ch;
      for(ch=0;ch<27;ch++)
      {
        new->branch[ch]=NULL;
      }
      new->isend=0;
      return new;
    }
    
    trinode *insert_trie(trinode *root,char *newenty)
    {int ind;
        trinode *proot;
        if(root==NULL)
           root=createnode();
        proot=root;
      for(int i=0;i<maxlength;i++)
      {
          ind=newenty[i]-'a';
          if(newenty[i]=='\0')
             break;
         else
         {
           if(root->branch[ind]==NULL)
              root->branch[ind]=createnode();
            root=root->branch[ind];
         }
      }
        if(root->isend!=0)
           printf("trying to insert duplicate");
        else   
           root->isend=1;
             
         return proot;  
    }
    
    void print_trie(trinode *cur)
    {
      int count;
     char word[40]; 
      
       for(int i=0;i<26;i++)
       {
        if(cur->branch[i]!=NULL)
        {
          word[count++]=(i+'a');
        if((cur->branch[i]->isend)==1)
        {
          printf("\n");
         
          for(int j=0;j<count;j++)
          {
            printf("%c",word[j]);
          }
        } 
        print_trie(cur->branch[i]); 
        }
      
       }
      count--; 
    }
    
    int search_trie(trinode *root,char *target)
    {
      int ind;
    for(int i=0;i<maxlength &&root;i++)
    {
      ind=target[i]-'a';
      if(target[i]=='\0')
        break;
      else
         root=root->branch[ind]; 
    }
    
    if(root && root->isend==1)
       return 1;
    else   
       return 0;
    }
    int main()
    {
    int ch;
    trinode *root=NULL;
    char *newenty;
    char *target;
    int check;
     newenty = (char *)malloc(maxlength);
     target= (char *)malloc(maxlength);
    while(1)
    {
    printf("\n enter option 1.insert_trie 2.display 3.search 4.exit");
    scanf("%d",&ch);
    switch(ch)
    
    {
         case 1:
            printf("enter word");
            scanf("%s",newenty);
          root=insert_trie(root,newenty);
            break;
            
         case 2:
            count =0;
            print_trie(root);
            break;
    
           case 3:
            printf("enter elem you want to search");
            scanf("%s",target);
           check=search_trie(root,target);
           if(check==0)
              printf("word not found");
            else
              printf("found");
            break;
            
            case 4:
            exit(0);
            break;
    
    }
    }
    }
#包括
#包括
#定义最大长度10
类型定义结构节点
{
int isend;
结构节点*分支[27];
}三位一体;
内伦,伯爵;
trinode*createnode()
{
trinode*new=(trinode*)malloc(sizeof(trinode));
int-ch;
for(ch=0;chbranch[ch]=NULL;
}
新建->isend=0;
归还新的;
}
trinode*insert_trie(trinode*root,char*newenty)
{int ind;
三节点*proot;
if(root==NULL)
root=createnode();
proot=根;
for(int i=0;ibranch[ind]==NULL)
根->分支[ind]=createnode();
根=根->分支[ind];
}
}
如果(根->isend!=0)
printf(“尝试插入副本”);
其他的
根->isend=1;
回程;
}
无效打印(三节点*cur)
{
整数计数;
字符字[40];
for(int i=0;ibranch[i]!=NULL)
{
字[count++]=(i++'a');
if((cur->branch[i]->isend)==1)
{
printf(“\n”);
对于(int j=0;jbranch[i]);
}
}
计数--;
}
int search_trie(trinode*root,char*target)
{
int ind;
对于(int i=0;ibranch[ind];
}
if(root&&root->isend==1)
返回1;
其他的
返回0;
}
int main()
{
int-ch;
三节点*root=NULL;
char*newenty;
字符*目标;
整数检查;
newenty=(char*)malloc(maxlength);
目标=(字符*)malloc(最大长度);
而(1)
{
printf(“\n输入选项1.insert\u trie 2.display 3.search 4.exit”);
scanf(“%d”和“ch”);
开关(ch)
{
案例1:
printf(“输入单词”);
scanf(“%s”,newenty);
root=插入(root,newenty);
打破
案例2:
计数=0;
打印目录(根目录);
打破
案例3:
printf(“输入要搜索的元素”);
扫描频率(“%s”,目标);
检查=搜索(根、目标);
如果(检查==0)
printf(“未找到单词”);
其他的
printf(“发现”);
打破
案例4:
出口(0);
打破
}
}
}

您在函数
print\u trie
中使用了
count
而没有初始化。这会调用未定义的行为。您似乎应该从正确设置代码格式(修复缩进)开始。此外,您还应该首先定义规范(打印内容等)。