c 二叉树的使用

简单的通过一个寻找嫌疑人的小程序 来演示二叉树的使用

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 
  6 /**
  7  *  数据结构 - 二叉树 - 节点
  8  */
  9 typedef struct node {
 10     char *querstion;
 11     struct node *no;
 12     struct node *yes;
 13 }node;
 14 
 15 /**
 16  *  方法 输入和输入
 17  *  根据打印,询问答案是否正确,y是正确
 18  */
 19 
 20 int yes_no(char *question) {
 21     printf("%s ? (y/n)",question);
 22     char answer[3];
 23     fgets(answer, 3, stdin);
 24     return answer[0] == 'y';
 25 }
 26 
 27 /**
 28  *  根据问题创建节点
 29  *
 30  *  @param question 问题描述
 31  *
 32  *  @return 返回一个node
 33  */
 34 node *create(char *question) {
 35     node *n = malloc(sizeof(node));
 36     n->querstion = strdup(question);
 37     n->no = NULL;
 38     n->yes = NULL;
 39     return n;
 40 }
 41 
 42 /**
 43  *  释放内存
 44  *
 45  *  @param n 节点
 46  */
 47 void release(node *n) {
 48     if (n) {
 49         if (n -> yes) {
 50             free(n->yes);
 51         }
 52         if (n -> no) {
 53             free(n->no);
 54         }
 55         if (n -> querstion) {
 56             free(n->querstion);
 57         }
 58         free(n);
 59     }
 60 }
 61 
 62 
 63 int main(int argc, const char * argv[]) {
 64    
 65     // 初始化...
 66     char question[80];
 67     char suspect[20];
 68     
 69     // 初始化第一个数据
 70     // 嫌疑犯如果有胡子就是张三,否则是李四
 71     node *start = create("嫌疑犯有胡子");
 72     start->no = create("李四");
 73     start->yes = create("张三");
 74     
 75     node *current;
 76     do {
 77         
 78         current = start;
 79         // 循环的访问节点
 80         while (1) {
 81             if (yes_no(current->querstion)) { //y
 82                 if (current->yes) {
 83                     current = current->yes;
 84                 }else {
 85                     printf("找到了嫌疑犯!\n");
 86                     break;
 87                 }
 88             }else if (current->no) { // 跳到no 分支
 89                 current = current->no;
 90             }else{ // 添加更多信息
 91                 
 92                 // 添加嫌疑犯
 93                 printf("谁是嫌疑犯?");
 94                 fgets(suspect, 20, stdin);
 95                 node *yes_node = create(suspect);
 96                 current->yes = yes_node;
 97                 
 98                 // n
 99                 node *no_node = create(current->querstion);
100                 current->no = no_node;
101                 
102                 // question
103                 printf("请输入一个问题,如果正确嫌疑犯是:%s,否则嫌疑犯是:%s",suspect,current->querstion);
104                 fgets(question, 80, stdin);
105                 current->querstion = strdup(question);
106                 break;
107             }
108         }
109         
110     } while (yes_no("再来一次"));
111     
112     release(start);
113     
114     return 0;
115 }

运行程序,我们来查看打印信息

  然而,表面上看这段代码没什么问题,其实有一部分存储器没事释放的,下边我们使用Valgrind工具来看一下

Valgrind 可以在这里下载http://valgrind.org/downloads/current.html#current

Valgind 是一款内存调试,内存泄露检测 和性能调优的 开源软件 

使用方法是:

下载并解压好文件后 -》 cd 到文件目录 然后依次运行下边的命令

#./configure --prefix=/usr/local/webserver/valgrind
#make
#make install

可能会报这样的错误,我使用的环境是mac

make[2]: *** No rule to make target `/usr/include/mach/mach_vm.defs', needed by `m_mach/mach_vmUser.c'.  Stop.
make[1]: *** [install-recursive] Error 1
make: *** [install] Error 2

运行下边的命令

xcode-select --install

等待一段时间后,安装完成后

./configure --disable-tls --enable-only64bit --build=amd64-darwin
make
sudo make install

ok 成功安装,接下来让我们使用Valgrind

gcc -g suspect.c -o sus

valgrind --leak-check=full ./sus

之后就能看到检测后的信息了 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值