|登录 |注册

查看: 1781|回复: 3
打印 上一主题 下一主题

C program treeNode 问题

[复制链接]
loonloon
2010-6-28 06:59 PM
本帖最后由 loonloon 于 2010-6-28 07:14 PM 编辑

请大大帮我修一修 if else 那一部分错了logic,因为当我key in时,search 不到。多谢帮忙

  1.                     TreeNode*root;
  2.         root = NULL;
  3.         insert (&root, "Malaysia", "Arumugam", 20);
  4.         insert (&root, "Korea", "Park Sung", 18);
  5.         insert (&root, "England", "Steven Gerrard", 19);
  6.         insert (&root, "Brazil", "Pele", 20);
  7.         search(root);
复制代码
  1. void search( TreeNode *root ){
  2.         char key[20];

  3.         TreeNode *node = root;

  4.         printf("Enter Country Name: ");
  5.         scanf("%s", key);

  6.         while(strcmp(key, “XXX” != 0){
  7.                 while(root != NULL){

  8.                         if(strcmp(key, node->countryName) == 0){
  9.                                 printf("%s %s %d", node->countryName, node->captainName, node->data);

  10.                         }else if(strcmp(key, node->countryName) < 0){
  11.                                 node = node->left;
  12.                         }else if(strcmp(key, node->countryName) > 0){
  13.                                 node = node->right;
  14.                         }
  15.                 }
  16.                                          printf("Enter Country Name: ");
  17.                            scanf("%s", key);
  18.         }
  19. }
复制代码
Super-Tomato
2010-6-28 07:49 PM
请大大帮我修一修 if else 那一部分错了logic,因为当我key in时,search 不到。多谢帮忙
loonloon 发表于 2010-6-28 06:59 PM



注意看你的 node 是 pointer, 而你 re-assign 的 node->left 是甚么型态并没有完整列出来,而且 while 條件容易陷入无穷循环
loonloon
2010-6-28 08:51 PM
型态并没有完整列出来?
Super-Tomato
2010-6-28 09:02 PM
型态并没有完整列出来?
loonloon 发表于 2010-6-28 08:51 PM



那麼你回想一下 pointer 要怎么 assign??
您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2026-1-1 03:19 PM , Processed in 0.094644 second(s), 24 queries .

Powered by Discuz! X2.5 © 2001-2012 Comsenz Inc.

本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved

Dedicated Server powered by iCore Technology Sdn. Bhd.

合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部