Gcc 将二叉搜索树转换为双链表-代码在gdb中运行良好,但在运行时崩溃

Gcc 将二叉搜索树转换为双链表-代码在gdb中运行良好,但在运行时崩溃,gcc,gdb,linked-list,binary-search-tree,Gcc,Gdb,Linked List,Binary Search Tree,下面的代码是将二叉搜索树转换为双链表的代码。它在GDB中工作正常,但在运行时崩溃。当我只有2个输入到二进制搜索树时,当有2个以上的输入时,它工作正常,崩溃。代码可能有什么问题。双链接列表不是循环列表。因此,第一个节点的前一个节点为NULL,最后一个节点的下一个节点为NULL #include<stdio.h> struct node{ int data; struct node* large; struct node* small; }; typedef struct node*

下面的代码是将二叉搜索树转换为双链表的代码。它在GDB中工作正常,但在运行时崩溃。当我只有2个输入到二进制搜索树时,当有2个以上的输入时,它工作正常,崩溃。代码可能有什么问题。双链接列表不是循环列表。因此,第一个节点的前一个节点为NULL,最后一个节点的下一个节点为NULL

#include<stdio.h>
struct node{
int data;
struct node* large;
struct node* small;
};

typedef struct node* Node;

void add_to_tree(Node *root, int num){
Node current = *root;
Node temp_store;
Node new_node = malloc(sizeof(Node));
new_node->data = num;
new_node->large = NULL;
new_node->small = NULL;

if(current == NULL){
    current = new_node;
    *root = current;
}
else
{
    while(current != NULL){
        temp_store = current;
        if(num > current->data)
            current = current->large;
        else
            current = current->small;
    }

    if(num <= temp_store->data)
        temp_store->small = new_node;
    else
        temp_store->large = new_node;
}
}
void display_list(Node head){
Node current = head;
if(head == NULL){
    printf("\nThe list is empty");
}
else{
    printf("\nThe list is:\n");
    while(current !=NULL){
        printf("%d\t", current->data);
        current = current->large;
    }
}
}

void join(Node a, Node b){
a->large = b;
b->small = a;
}

Node append(Node a, Node b){
Node aLast;
Node current;

if(a == NULL) return (b);
if(b == NULL) return (a);

current = a;
while(current->large != NULL)
    current = current->large;
aLast = current;

join(aLast, b);

return (a);
}

Node bst_to_dll(Node root){
Node aList;
Node bList;
if(root == NULL)
    return NULL;

aList = bst_to_dll(root->small);
bList = bst_to_dll(root->large);

root->small = NULL;
root->large = NULL;

aList = append(aList, root);
aList = append(aList, bList);

return aList;
}

int main(){
Node bin_tree = NULL;
Node d_list;


add_to_tree(&bin_tree, 1);
add_to_tree(&bin_tree, 2);
add_to_tree(&bin_tree, 3);

d_list = bst_to_dll(bin_tree);

display_list(d_list);

return 0;

}
#包括
结构节点{
int数据;
结构节点*大;
结构节点*小;
};
类型定义结构节点*节点;
void将_添加到_树(节点*根,int num){
节点当前=*根;
节点温度存储;
Node new_Node=malloc(sizeof(Node));
新建_节点->数据=num;
新建_节点->大=空;
新建_节点->小=空;
如果(当前==NULL){
当前=新节点;
*根=电流;
}
其他的
{
while(当前!=NULL){
温度存储=当前;
如果(数值>当前->数据)
电流=电流->大电流;
其他的
电流=电流->小电流;
}
if(num数据)
临时存储->小=新节点;
其他的
临时存储->大型=新建节点;
}
}
无效显示列表(节点头){
节点电流=头;
if(head==NULL){
printf(“\n列表为空”);
}
否则{
printf(“\n列表为:\n”);
while(当前!=NULL){
printf(“%d\t”,当前->数据);
电流=电流->大电流;
}
}
}
无效连接(节点a、节点b){
a->large=b;
b->small=a;
}
节点附加(节点a、节点b){
节点aLast;
节点电流;
如果(a==NULL)返回(b);
如果(b==NULL)返回(a);
电流=a;
while(当前->大!=NULL)
电流=电流->大电流;
aLast=电流;
加入(阿拉斯特,b);
报税表(a);
}
节点bst\u到\u dll(节点根){
节点列表;
节点列表;
if(root==NULL)
返回NULL;
aList=bst\u to\u dll(根->小);
bList=bst\u to\u dll(根->大);
root->small=空;
根->大=空;
aList=append(aList,root);
aList=append(aList,bList);
回归主义者;
}
int main(){
节点bin_tree=NULL;
节点d_列表;
将_添加到_树(&bin_树,1);
将_添加到_树(&bin_树,2);
将_添加到_树(&bin_树,3);
d_list=bst_to_dll(bin_树);
显示_列表(d_列表);
返回0;
}

您的程序在编译时会生成警告:

gcc -g t.c
t.c: In function ‘add_to_tree’:
t.c:13: warning: incompatible implicit declaration of built-in function ‘malloc’
在Valgrind下,它还会生成32个错误,其中第一个错误是:

==29346== Invalid write of size 8
==29346==    at 0x4005E9: add_to_tree (/tmp/t.c:15)
==29346==    by 0x400820: main (/tmp/t.c:97)
==29346==  Address 0x51b6078 is 0 bytes after a block of size 8 alloc'd
==29346==    at 0x4C2A4D6: malloc (coregrind/m_replacemalloc/vg_replace_malloc.c:263)
==29346==    by 0x4005D7: add_to_tree (/tmp/t.c:13)
==29346==    by 0x400820: main (/tmp/t.c:97)

这应该足以让您找出您的bug。

“不会发生崩溃”!=“很好”;)对我在windows中使用的gcc不是最新的。这就是它崩溃的原因。无论如何,新gcc=>warning有一个警告:内置函数“malloc”的不兼容隐式声明是因为不包括stdlib.h头文件