我正在尝试用C编程中的ID值和字符串名称来编写B +树的结构。但我有一个问题。在程序中,当我写15个ID和名称时,所有ID值排序和显示但名称都没有显示ID,我不知道为什么,但它应该有显示。也许我在第21行遗漏了一些东西,它可能是temp->name = word
但是Visual程序不接受它。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct bin_tree
{
int data;
char name[30];
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node **tree, int val, char word[30])
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node)); //Dynamic Allocation
temp->left = NULL;
temp->right = NULL;
temp->data = val;
temp->name[30]= word; //temp->name = word; - HATA
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val,word);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val,word);
}
}
struct tree *delet(struct bin_tree *ptr, int x)
{
struct bin_tree *p1,*p2;
if(!ptr)
{
printf("ID not found");
return(0);
}
else
{
if(ptr->data < x)
{
delet(ptr->right,x); //ptr->right=delet(ptr->right,x);
}
else if (ptr->data >x)
{
delet(ptr->left,x); //ptr->left=delet(ptr->left,x);
}
else
{
if(ptr->data == x)
{
if(ptr->left == ptr->right)
{
free(ptr);
return(NULL);
}
else if(ptr->left==NULL)
{
p1=ptr->right;
free(ptr);
}
else if(ptr->right==NULL)
{
p1=ptr->left;
free(ptr);
}
else
{
p1=ptr->right;
p2=ptr->right;
while(p1->left != NULL)
p1=p1->left;
p1->left=ptr->left;
free(ptr);
}
}
}
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
void print_preorder(node * tree)
{
if (tree)
{
printf("%d - %s ->",tree->data, tree->name); // Preorder ID - Name çıkmıyor
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if(tree)
{
print_inorder(tree->left);
printf("%d - %s ->",tree->data,tree->name); //Inorder ID - Name çıkmıyor
print_inorder(tree->right);
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d - %s ->",tree->data,tree->name); //Postorder ID - Name çıkmıyor
}
}
int main()
{
node *root;
node *tmp;
int a; //ID for Add func.
int item_no; //Id for delete func.
int z; //ID for search func.
int i=0;
char ans,b[100],c;
root=NULL;
while(ans!='5')
{
printf("1. Add\n");
printf("2. Delete\n");
printf("3. Search\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter the choice: ");
scanf("%s",&ans);
switch(ans)
{
case '1':
{
printf("Enter the Student ID: ");
scanf("%d",&a);
printf("Enter the student's name: ");
scanf("%d", &c);
while((c != '\n' && i<30)
{
b[i++] = c;
}
b[i] = '\0';
insert(&root,a,b);
i=0;
break;
}
case '2':
{
printf("Enter the ID: ");
scanf(" %d",&item_no);
delet(root, item_no);
break;
}
case '3':
{
printf("Enter the Student ID: ");
scanf("%d",&z);
tmp = search(&root, z);
if (tmp)
{
printf("Student ID: %d\nStudent name: %s\n", tmp->data, tmp->name);
}
else
{
printf("Data not found.\n");
}
}
case '4':
{
printf("\nPre Order Display: ");
print_preorder(root);
printf("\nIn Order Display: ");
print_inorder(root);
printf("\nPost Order Display: ");
print_postorder(root);
}
case '5':
{
printf("\nDone.");
exit (0);
}
default :
{
printf("Wrong choice!\n");
break;
}
}
}
return 0;
}
答案 0 :(得分:1)
你想要的是复制字符:
strcpy(temp->name, word);
(您尝试将char数组分配给单个char,这也恰好超出了数组的大小。name
的大小为30,因此有效索引的范围为0到29 。)