gpt4 book ai didi

C程序编译但不会打印出main方法中的测试用例

转载 作者:行者123 更新时间:2023-11-30 21:22:36 26 4
gpt4 key购买 nike

我的 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 代码,因此如果我可能遗漏了任何错误,我深表歉意。

最佳答案

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->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;
}

关于C程序编译但不会打印出main方法中的测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53094230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com