| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1155 人关注过本帖
标题:为啥崩溃?求解,谢谢
只看楼主 加入收藏
captain2050
Rank: 2
等 级:论坛游民
帖 子:57
专家分:43
注 册:2016-7-15
结帖率:92.86%
收藏
 问题点数:0 回复次数:0 
为啥崩溃?求解,谢谢
每次输入一个单词
统计各个单词输入次数
程序代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct tree
{
  char* p_word;
  int count;
  struct tree* left;
  struct tree* right;
};

int main()
{
  void output_tree(struct tree*);
  void input_tree(struct tree*,char*);
  struct tree* root=NULL;
  //input
  char* p=malloc(sizeof(char)*20);
  setbuf(stdin,NULL);
  scanf("%s",p);
  while((*p)!='0')
    {
      input_tree(root,p);
      setbuf(stdin,NULL);
      p=malloc(20*sizeof(char));
      scanf("%s",p);
    }
  //output
  output_tree(root);
}

void input_tree(struct tree* root,char* p)
{
  if(root==NULL)
    {
      root=malloc(sizeof(struct tree));
      root->p_word=p;
      root->count=1;
      root->left=root->right=NULL;
    }
  else if(strcmp(p,root->p_word)==0)
    root->count++;
  else if(strcmp(p,root->p_word)<0)
    input_tree(root->left,p);
  else if(strcmp(p,root->p_word)>0)
    input_tree(root->right,p);
}

void output_tree(struct tree* root)
{
  if(root->left!=NULL)
    output_tree(root->left);
  if(root!=NULL)
    printf("%s:%d\n",root->p_word,root->count);
  if(root->right!=NULL)
    output_tree(root->right);
}

搜索更多相关主题的帖子: struct tree char left NULL 
2017-08-29 19:32
快速回复:为啥崩溃?求解,谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017150 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved