【发布时间】:2018-11-30 01:35:21
【问题描述】:
我正在制作一个在一个实例中存储多个事物的二叉搜索树,我遇到了两个错误。函数insert 中发生的第一个用于工作而不是随机停止,第二个在print_tree_inorder 中,我不知道为什么会被破坏。
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include <malloc.h>
char* nameArray[50] = {};
char* number[50] = {};
char* ID[50] = {};
char* hours[50] = {};
char* pPH[50] = {};
int flag;
typedef struct node
{
char* name;
char* phoneNum;
char* ID;
char* hours;
char* pPH;
struct node * left;
struct node * right;
} node_t;
void insert(node_t * tree, char* name, char* phoneNum, char* hours, char* pPH);
void print_tree_inorder(node_t * current);
int main()
{
char* n,p,id,h,pph;
int numberOfTimes = 0;
node_t * test_list = malloc(sizeof(node_t));
/* set values explicitly, alternative would be calloc() */
test_list->name = "";
test_list->phoneNum = "";
//test_list->ID = "";
test_list->hours = "";
test_list->pPH = "";
test_list->left = NULL;
test_list->right = NULL;
printf("Please enter in the amount of people you want: ");
scanf("%d",&numberOfTimes);
printf("\n");
for(int i = 0; i<numberOfTimes; i++){
printf("Please enter in name: ");
scanf("%s", &n);
nameArray[i] = n;
printf("Please enter in PhoneNumber: ");
scanf("%s", &p);
number[i] = p;
printf("Please enter in Hours: ");
scanf("%s", &h);
hours[i] = h;
//printf("\n");
printf("Please enter in pay per hour: ");
scanf("%s", &pph);
pPH[i] = pph;
insert(test_list,nameArray[i],number[i],hours[i], pPH[i] );
}
printf("\n In order\n");
print_tree_inorder(test_list);
}
void insert(node_t * tree, char* name, char* phoneNum, char* hours, char* pPH)
{
//unsigned int number = (unsigned int)ptr
if (tree->name == 0)
{
/* insert on current (empty) position */
tree->name = name;
tree->phoneNum = phoneNum;
//tree->ID = ID;
tree->hours = hours;
tree->pPH = pPH;
}
else
{
if ( strcmp(tree->name, name) > 0)
{
/* insert left */
if (tree->left != NULL)
{
insert(tree->left, name, phoneNum, hours, pPH);
}
else /* no left nodes*/
{
tree->left = malloc(sizeof(node_t));
/* set values explicitly, alternative would be calloc() */
tree->left->name = name;
tree->left->phoneNum = phoneNum;
tree->left->hours = hours;
tree->left->pPH = pPH;
tree->left->left = NULL;
tree->left->right = NULL;
}
}
else /*add node to right */
{
if ( strcmp(tree->name, name) <= 0)
{
/* insert right */
if (tree->right != NULL)
{
insert(tree->right, name, phoneNum, hours, pPH);
}
else
{
tree->right = malloc(sizeof(node_t));
/* set values explicitly, alternative would be calloc() */
tree->right->name = name;
tree->right->phoneNum = phoneNum;
tree->right->hours = hours;
tree->right->pPH = pPH;
tree->right->left = NULL;
tree->right->right = NULL;
}
}
}
}
}
void print_tree_inorder(node_t * current) {
if (current == NULL) return;
print_tree_inorder(current->left);
printf(" %s %s %s %s\n", current->name, current->phoneNum,current->hours, current->pPH);
print_tree_inorder(current->right);
}
【问题讨论】:
-
查看
char* n; .... scanf("%s", &n);。名称保存在哪里? -
那是后面的代码。调用
scanf("%s", &n)保存的名称在哪里?n是指向无处的指针。scanf("%s", &n);应该引起警告。你用的是什么编译器? -
这段代码有很多东西,比我期望的 C 新手开始的要多得多。查看如何读取一行用户输入并将其保存为字符串,然后再打印。
char buf[80]; scanf("%79s", buf); printf("%s\n", buf); -
也可以在insert方法中去掉第二个strcmp。
-
@GlenvillePecor - 将警告视为错误
标签: c binary-search-tree