我的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代码,因此如果可能错过的错误我深表歉意。
答案 0 :(得分: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->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;
}