C 打印树结果、分割错误、插入到树中
下面是代码,我用一个例子运行它,它是有效的,但是当它出现时 我不明白怎么了,提前谢谢你的帮助 任何帮助。我需要正确打印字典文本(插入、打印),仍然无法想出解决方案,我的意思是使用字典数据结构之类的C 打印树结果、分割错误、插入到树中,c,C,下面是代码,我用一个例子运行它,它是有效的,但是当它出现时 我不明白怎么了,提前谢谢你的帮助 任何帮助。我需要正确打印字典文本(插入、打印),仍然无法想出解决方案,我的意思是使用字典数据结构之类的 #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #include <stdio.h> typedef struct Node_s
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
typedef struct Node_s {
char *element;
struct Node_s *left, *right;
} Node;
typedef struct {
Node *head;
} Table;
//Table *initialize();
//Node *createNode(const char *element);
Table *initialize() {
Table *tb = malloc(sizeof(Table)*1000);
tb->head = NULL;
return tb;
}
Node *createNode( char * element ) {
Node *temp = malloc(sizeof(temp));
temp->element = element ;
temp->left = temp->right = NULL;
return temp;
}
void insert(Table *temp, char *element) {
Node *nd = createNode(element);
Table * place = NULL;
Node *new = NULL;
int cmp = 0;
if(temp->head == NULL) {
temp->head= nd;
printf("empty ! \n");
return;
}
else {
Table *current = temp;
while (current!=NULL) {
cmp = strcmp(current->head->element,element);
if(cmp < 0) {
current->head= current->head->left;
}
else if(cmp > 0) {
current->head = current->head->right;
}
} //while
place = current;
new = nd;
if(cmp > 0 ) {
place->head->right = new ;
}
else if(cmp <0 ) {
place->head->left = new;
}
}
}
void print_table(Table *temp) {
if(temp!=NULL || !temp->head) return;
print_table(temp->head->left);
printf("%s \n",temp->head->element);
print_table(temp->head->right);
}
int main () {
Node * nd = NULL;
//nd->element = "key";
// nd = createNode("key");
Table *tb = initialize();
//tb->head = createNode("key");
//tb->head = createNode("key");
insert(tb, "table element1");
insert(tb, "table element2");
insert(tb, "table element2");
//nd = createNode("key1");
// print_table(t);
//printf("%s \n",nd->element);
print_table(tb);
// printf("%s \n",tb->head->element);
free(nd);
return 0;
}
#包括
#包括
#包括
#包括
#包括
类型定义结构节点{
字符*元素;
结构节点左、*右;
}节点;
类型定义结构{
节点*头;
}表;
//表*初始化();
//节点*创建节点(常量字符*元素);
表*初始化(){
表*tb=malloc(sizeof(表)*1000);
tb->head=NULL;
返回结核病;
}
节点*创建节点(字符*元素){
节点*temp=malloc(sizeof(temp));
温度->元素=元素;
临时->左=临时->右=空;
返回温度;
}
无效插入(表*临时,字符*元素){
Node*nd=createNode(元素);
表*place=NULL;
Node*new=NULL;
int-cmp=0;
如果(临时->头部==NULL){
温度->压头=nd;
printf(“空!\n”);
返回;
}
否则{
表*电流=温度;
while(当前!=NULL){
cmp=strcmp(当前->头部->元件,元件);
如果(cmp<0){
当前->头部=当前->头部->左侧;
}
如果(cmp>0){
当前->头部=当前->头部->右侧;
}
}//而
地点=电流;
新=nd;
如果(cmp>0){
地点->头部->右侧=新建;
}
否则如果(cmp头->左=新;
}
}
}
无效打印表格(表格*温度){
如果(temp!=NULL | |!temp->head)返回;
打印表格(临时->磁头->左侧);
printf(“%s\n”,temp->head->element);
打印表格(临时->磁头->右侧);
}
int main(){
Node*nd=NULL;
//nd->element=“key”;
//nd=创建节点(“键”);
表*tb=initialize();
//tb->head=createNode(“键”);
//tb->head=createNode(“键”);
插入(tb,“表元素1”);
插入(tb,“表元素2”);
插入(tb,“表元素2”);
//nd=创建节点(“键1”);
//打印表格(t);
//printf(“%s\n”,nd->element);
打印表格(tb);
//printf(“%s\n”,tb->head->element);
免费(nd);
返回0;
}
这里有很多潜在的bug,但您的主要问题是createNode的以下行:
Node *temp = malloc(sizeof(temp));
这里您正在执行sizeof(temp),temp是指针。这意味着您只为指针分配了足够的内存(通常为8字节)。因此,在使用堆分配结构的左/右成员时,您在分配内存之外进行写入。修复方法:
Node *temp = malloc(sizeof(Node));
// EXTRA: I also recommend that you verify that the allocation was successful
if (temp) {
temp->element = element ;
temp->left = temp->right = NULL;
}
return temp;
在printTable中,在传递可能为NULL的函数参数时,还应验证temp本身是否为NULL:
if(!temp || !temp->head) return;
另外,删除main末尾的
free(nd);
,因为在未分配的堆内存上调用free()会损坏堆并通常导致segfault。这里有很多潜在的bug,但您的主要问题在createNode的以下行中:
Node *temp = malloc(sizeof(temp));
这里您正在执行sizeof(temp),temp是指针。这意味着您只为指针分配了足够的内存(通常为8字节)。因此,在使用堆分配结构的左/右成员时,您在分配内存之外进行写入。修复方法:
Node *temp = malloc(sizeof(Node));
// EXTRA: I also recommend that you verify that the allocation was successful
if (temp) {
temp->element = element ;
temp->left = temp->right = NULL;
}
return temp;
在printTable中,在传递可能为NULL的函数参数时,还应验证temp本身是否为NULL:
if(!temp || !temp->head) return;
另外,删除main末尾的
free(nd);
,因为在未分配的堆内存上调用free()会损坏堆并通常导致segfault。这里有很多潜在的bug,但您的主要问题在createNode的以下行中:
Node *temp = malloc(sizeof(temp));
这里您正在执行sizeof(temp),temp是指针。这意味着您只为指针分配了足够的内存(通常为8字节)。因此,在使用堆分配结构的左/右成员时,您在分配内存之外进行写入。修复方法:
Node *temp = malloc(sizeof(Node));
// EXTRA: I also recommend that you verify that the allocation was successful
if (temp) {
temp->element = element ;
temp->left = temp->right = NULL;
}
return temp;
在printTable中,在传递可能为NULL的函数参数时,还应验证temp本身是否为NULL:
if(!temp || !temp->head) return;
另外,删除main末尾的
free(nd);
,因为在未分配的堆内存上调用free()会损坏堆并通常导致segfault。这里有很多潜在的bug,但您的主要问题在createNode的以下行中:
Node *temp = malloc(sizeof(temp));
这里您正在执行sizeof(temp),temp是指针。这意味着您只为指针分配了足够的内存(通常为8字节)。因此,在使用堆分配结构的左/右成员时,您在分配内存之外进行写入。修复方法:
Node *temp = malloc(sizeof(Node));
// EXTRA: I also recommend that you verify that the allocation was successful
if (temp) {
temp->element = element ;
temp->left = temp->right = NULL;
}
return temp;
在printTable中,在传递可能为NULL的函数参数时,还应验证temp本身是否为NULL:
if(!temp || !temp->head) return;
另外,删除main末尾的
free(nd);
,因为对未分配的堆内存调用free()会损坏堆,通常会导致segfault。打印方法在到达左侧最后一个节点时崩溃,因为它将调用print_table(NULL)因为左边没什么了。之后当它执行这行时
if(!temp->head) return;
由于temp为NULL,您会遇到内存访问冲突,您还应该检查temp本身是否为NULL
if( !temp || !temp->head ) return;
这应该可以解决您的问题。您的打印方法在到达左侧的最后一个节点时崩溃,因为它将调用print_table(NULL),因为左侧没有其他内容。之后,当它执行该行时
if(!temp->head) return;
由于temp为NULL,您会遇到内存访问冲突,您还应该检查temp本身是否为NULL
if( !temp || !temp->head ) return;
这应该可以解决您的问题。您的打印方法在到达左侧的最后一个节点时崩溃,因为它将调用print_table(NULL),因为左侧没有其他内容。之后,当它执行该行时
if(!temp->head) return;
由于temp为NULL,您会遇到内存访问冲突,您还应该检查temp本身是否为NULL
if( !temp || !temp->head ) return;
这应该可以解决您的问题。当r