示例程序涉及链表和结构不起作用 [英] Sample program involving linked lists and structs doesn't work

查看:190
本文介绍了示例程序涉及链表和结构不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是我的教授提供的示例程序。它的功能是搜索树的链接列表,并返回天气的结果,或者不它发现由用户在树的输入。但是,不管我投入它始终返回false。这有什么错呢?

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#定义NL 20typedef结构树
{
    烧焦tree_name [NL]
    结构树旁边*;
}树;INT
checktree(树* P,焦炭名[])
{
   INT发现= 0;
   而(P!= NULL)
   {
       如果(STRCMP(P - > tree_name,名)== 0)
          找到= 1;
       P = - >下一个;
   }
   返回(发现);
}
INT
主要(无效)
{
    / *声明变量来保存树节点* /
    树树1,tree2,tree3,tree4;    / *宣告启动和行驶指针* /
    树* P *启动;    烧焦TREENAME [NL]    / *把树名到每个节点* /
    的strcpy(tree1.tree_name,枫);
    的strcpy(tree2.tree_name,杉);
    的strcpy(tree3.tree_name,松);
    的strcpy(tree4.tree_name,橡树);    / *设置列表的开始在树1 * /
    开始=&放大器;树1;    / *链接的其它树种在一起* /
    tree1.next =安培; tree2;
    tree2.next =安培; tree3;
    tree3.next =安培; tree4;    / *设置tree4作为列表的最后一个节点* /
    tree4.next = NULL;    / *检查,如果一棵树在列表* /
    的printf(请输入树名称搜索:);
    与fgets(TREENAME,sizeof的(TREENAME),标准输入);    如果(checktree(启动,TREENAME))
       的printf(%S是在我们的树列表中找到\\ n,TREENAME);
    其他
       的printf(%s没有在我们的树列表中找到\\ n,TREENAME);
    返回(0);
}


解决方案

与fgets 读取一行还包括换行字符。添加code调用 checktree 之前删除它。

 与fgets(TREENAME,sizeof的(TREENAME),标准输入);
INT LEN = strlen的(TREENAME);
如果(TREENAME [LEN-1] =='\\ n')
{
   TREENAME [LEN-1] ='\\ 0';
}如果(checktree(启动,TREENAME)){...}

So, this is a sample program supplied by my prof. Its function is to search a linked list of trees and return a result of weather or not it found the tree input by the user. However, no matter what I input it always returns false. What's wrong with it?

#include <stdio.h>
#include <string.h>
#define NL 20

typedef struct tree 
{
    char tree_name [NL];
    struct tree* next;
}tree;

int 
checktree (tree *p, char name[])
{
   int found = 0;
   while (p != NULL)
   {
       if (strcmp(p -> tree_name, name) == 0)
          found = 1;
       p = p -> next;
   }
   return (found);
}


int
main (void)
{
    /* declaring variables to hold tree nodes */
    tree tree1, tree2, tree3, tree4;

    /* declaring the starting and traveling pointers */
    tree *p, *start;

    char treename[NL];

    /* putting tree names into each node */
    strcpy (tree1.tree_name, "Maple");
    strcpy (tree2.tree_name, "Fir");
    strcpy (tree3.tree_name, "Pine");
    strcpy (tree4.tree_name, "Oak");

    /* setting the start of the list at tree1 */
    start = &tree1;

    /* linking the other trees together */
    tree1.next = &tree2;
    tree2.next = &tree3;
    tree3.next = &tree4;

    /* sets tree4 as the last node of the list  */
    tree4.next = NULL;

    /* checking if a tree is in the list */
    printf ("Enter tree name to search: ");
    fgets (treename,sizeof(treename),stdin);

    if (checktree(start, treename))
       printf ("%s was found in our list of trees.\n", treename);
    else
       printf ("%s was not found in our list of trees.\n", treename);
    return (0);
}

解决方案

fgets reads a line that also includes the newline character. Add code to remove it before the call to checktree.

fgets (treename,sizeof(treename),stdin);
int len = strlen(treename);
if ( treename[len-1] == '\n' )
{
   treename[len-1] = '\0';
}

if (checktree(start, treename)) { ... }

这篇关于示例程序涉及链表和结构不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆