【问题标题】:C program compiles but won't print out the test cases in the main methodC 程序编译但不会在 main 方法中打印出测试用例
【发布时间】:2018-11-01 01:56:41
【问题描述】:

我的 C 代码的重点是按字母顺序插入字符串节点。这是我的代码....

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct node {
    char * word;
    struct node * left;
    struct node * right;
}treeNode;

treeNode * head = NULL;

void traverse (treeNode * h)
{
    if(h->left == NULL){
        scanf("%s ", h->word);
        if(h->right != NULL){
            traverse(h->right);
        }
    }
    else{
        traverse(h->left);
        printf("%s ", h->word);
        if(h->right != NULL){
            traverse(h->right);
        }
    }

    treeNode *newNode(char * s)
    {
        treeNode *insert = (treeNode*)malloc(100*sizeof(treeNode));
        insert->left = NULL;
        insert->right = NULL;
        insert->word = s;
        return insert;
    }

    treeNode * addNode (treeNode * h, char * s)
    {
        if(h == NULL){
            return newNode(s);
        }
        else{
            if (strcmp (h->word, s)> 0){
                h->left = addNode(h->left,s);
            }
            else{
                h->right = addNode(h->right,s);
            }
        }
        return h;
    }
    void main()
    {

        printf("\nTest Animals 1");
        head = insert(head, "dog");
        insert(head, "horse");
        insert(head, "frog");
        insert(head, "fish");
        insert(head, "cow");
        traverse(head);
        head = NULL;

        printf("\nTest Food 2");
        head = insert(head, "pizza");
        insert(head, "sushi");
        insert(head, "burger");
        insert(head, "salad");
        insert(head, "nuggets");
        traverse(head);
        head = NULL;

        printf("\nTest Sports 3");
        head = insert(head, "soccer");
        insert(head, "basketball");
        insert(head, "football");
        insert(head, "tennis");
        insert(head, "gymnastics");
        traverse(head);
        head = NULL;

    }

它编译完美,完全没有错误,但我的主要方法不允许我打印出我的示例测试用例。会不会是代码本身的问题?我已经看了一遍,我看不出它有什么问题。这也是我的第一个 C 代码,如果有可能遗漏的错误,我深表歉意。

【问题讨论】:

  • 欢迎来到 SO!您能解释一下为什么在遍历中使用scanfprintf 吗?你期待什么样的输出? (treeNode*)malloc(100*sizeof(treeNode)) 不需要演员表,100 似乎是任意的。我找不到您的 insert 方法,并且从未使用过 addNode
  • "它编译完美,完全没有错误" 它肯定不会! bad.c:31:3:错误:此处不允许函数定义 bad.c:40:1:错误:此处不允许函数定义 bad.c:55:1:错误:此处不允许函数定义错误。 c:86:2: 错误:预期 '}'
  • 您好!我在创建代码时在网上查找了很多这些方法,scanf 不应该在那里。基本上,我试图按字母顺序排列我的节点列表。
  • 函数insert 未在您的代码中声明/定义。另外,分配一个节点只需要treeNode *insert = malloc(sizeof(treeNode));,不要乘以100
  • 哦,我现在看到问题了,我只是不确定在我的 main 函数中调用 addNode 方法时应该使用哪些参数。还在挣扎

标签: c nodes treenode


【解决方案1】:
treeNode *newNode(char * s)
{
    treeNode *insert = (treeNode*)malloc(100*sizeof(treeNode));
    insert->left = NULL;
    insert->right = NULL;
    insert->word = s;
    return insert;
}

使用malloc(sizeof(treeNode)) 分配单个节点。除非您想要 100 个节点的内存,否则不要乘以 100。

不要只将指针分配给文字字符串 (insert-&gt;word = s),而是为该字符串分配内存,并使用 strcpy。示例:

insert->word = malloc(strlen(str) + 1);
strcpy(insert->word, str);

如果目标是插入已排序的项目,那么您必须遍历所有项目。我建议避免使用递归函数,特别是对于初学者。

最后,编译您的程序时发出最大警告。解决所有警告。

typedef struct node {
    char * word;
    struct node * left;
    struct node * right;
}treeNode;

void traverse(treeNode *head)
{
    treeNode *ptr = head;
    while(ptr)
    {
        printf("%s, ", ptr->word);
        ptr = ptr->right;
    }
    printf("\n");
}

treeNode *insert(treeNode *head, char *str)
{
    treeNode *ptr = malloc(sizeof(treeNode));
    ptr->left = NULL;
    ptr->right = NULL;
    ptr->word = malloc(strlen(str) + 1);
    strcpy(ptr->word, str);

    if(head == NULL)
    {
        head = ptr;
        return head;
    }
    else
    {
        int inserted = 0;
        treeNode *walk = head;
        treeNode *prev = NULL;
        while(walk)
        {
            if(strcmp(ptr->word, walk->word) < 0)
            {
                if(walk == head)
                {
                    ptr->right = head;
                    head = ptr;
                }
                else
                {
                    prev->right = ptr;
                    ptr->right = walk;
                }
                inserted = 1;
                break;
            }
            prev = walk;
            walk = walk->right;
        }

        if(!inserted)
        {
            prev->right = ptr;
        }
    }

    return NULL;
}

int main(void)
{
    treeNode *head = NULL;
    printf("\nTest Animals 1");
    head = insert(head, "dog");
    insert(head, "horse");
    insert(head, "frog");
    insert(head, "fish");
    insert(head, "cow");
    traverse(head);
    return 0;
}

如果这是一个二叉搜索树,那么按照下面的代码:

void traverse(treeNode *head)
{
    if(head)
    {
        traverse(head->left);
        printf("%s \n", head->word);
        traverse(head->right);
    }
}

treeNode *insert(treeNode *head, char * s)
{
    if(head == NULL)
    {
        treeNode *ptr = malloc(sizeof(treeNode));
        ptr->left = NULL;
        ptr->right = NULL;
        ptr->word = malloc(strlen(s) + 1);
        strcpy(ptr->word, s);
        return ptr;
    }

    if(strcmp(head->word, s) > 0)
        head->left = insert(head->left, s);
    else 
        head->right = insert(head->right, s);

    return head;
}

void main(void)
{
    treeNode *head = NULL;
    head = insert(NULL, "dog");
    insert(head, "horse");
    insert(head, "frog");
    insert(head, "fish");
    insert(head, "cow");
    traverse(head);
    return 0;
}

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2017-03-04
  • 1970-01-01
  • 2023-03-31
  • 2018-10-14
  • 1970-01-01
  • 2011-01-03
  • 2015-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多