[英]"Segmentation fault: 11" error in binary tree constructing problem
I was working on my C programing practice which needs me to construct a strictly binary tree with given postorder data in input file.我正在研究我的 C 编程实践,这需要我在输入文件中使用给定的后序数据构造一个严格的二叉树。 My program first read the input data in inverted order into a linked list.
我的程序首先将输入数据以倒序方式读入链表。 And when I was trying to compile my tree-constructing function, the compiler showed this error:
当我试图编译我的树构造函数时,编译器显示了这个错误:
Segmentation fault: 11分段错误:11
Can anybody help me with this problem?有人可以帮我解决这个问题吗? Thanks!
谢谢!
#include <string.h>
#include <stdlib.h>
#include "pa3.h"
static char* substr(const char *src, int m, int n);
static List_Node *insert_node(List_Node *tail, char* buffer);
static struct T_Node* newNode(char* string);
static char* substr(const char *src, int m, int n)
{
int len = n - m;
char *dest = (char*)malloc(sizeof(char) * (len + 1));
for (int i = m; i < n && (*(src + i) != '\0'); i++)
{
*dest = *(src + i);
dest++;
}
*dest = '\0';
return dest - len;
}
static T_Node* newNode(char* string)
{
T_Node* t_node = malloc(sizeof(*t_node));
if (t_node == NULL)
{
printf("malloc failed");
return NULL;
}
printf("malloc success");
printf("string[0] is |%c|", string[0]);
if(string[0] == '(')
{
char* left = substr(string, 1, 13);
t_node->node->left_child = strtod(left, NULL);
char* right = substr(string, 14, 12);
t_node->node->right_child = strtod(right, NULL);
}
else
{
int l = (int)(string[0]);
printf("label is %d", l);
t_node->l_node->label = l;
char* c = substr(string, 2, 12);
t_node->l_node->cap = strtod(c, NULL);
}
t_node->left = t_node->right = NULL;
return t_node;
}
T_Node* constructBST(List_Node* tail)
{
printf("start newnode");
T_Node *t_node = newNode(tail->string);
printf("newnode succuess");
if((tail->string)[0] != '(')
{
tail = tail->prev;////////////////
return t_node;
}
tail = tail->prev;
t_node->right = constructBST(tail);
t_node->left = constructBST(tail);
return t_node;
}
static List_Node *insert_node(List_Node *tail, char* buffer)
{
List_Node *new = malloc(sizeof(*new));
if (new == NULL)
{
return NULL;
}
*new = (List_Node){.string = buffer, .next = NULL};
//printf("new is %s\n", new->string);
if (tail != NULL)
{
tail->next = new;
new->prev = tail;
}
return new;
}
List_Node* Load_Tree_From_Input(char* filename, double* rd, double* r, double* c)
{
FILE* input_fptr = NULL;
input_fptr = fopen(filename, "r");
if (input_fptr == NULL)
{
printf("open failed\n");
return NULL;
}
fscanf(input_fptr, "%le %le %le", rd, r, c);
rewind(input_fptr);
char bu[100];
fgets(bu, 100, input_fptr);
printf("stringend is %s", bu);
List_Node* tail = NULL;
while(feof(input_fptr) == 0)
{
//char* buffer = malloc(sizeof(char));
char buffer[100];
fgets(buffer, 50, input_fptr);
printf("string is %s", buffer);
tail = insert_node(tail, buffer);
}
//printf("FINAL string is %s", tail->string);
fclose(input_fptr);
return tail;
}
int main(int argc, char **argv)
{
double rd = 0;
double r = 0;
double c = 0;
if (argc == 2)
{
List_Node* tail = Load_Tree_From_Input(argv[1], &rd, &r, &c);
puts(tail->string);
//printf("FINAL string is |%s|", tail->string);
List_Node test;
T_Node* root = constructBST(&test);
printf("tree planted\n");
return EXIT_SUCCESS;
}
else
printf("argument error\n");
return EXIT_FAILURE;
}
#define __PA3_H__
typedef struct Node {
double left_child;
double right_child;
} Node;
typedef struct L_Node{
int label;
double cap;
} L_Node;
typedef struct T_Node
{
Node *node;
L_Node *l_node;
struct T_Node *left;
struct T_Node *right;
} T_Node;
typedef struct List_Node {
struct List_Node *prev;
char* string;
struct List_Node *next;
} List_Node;
List_Node* Load_Tree_From_Input(char* filename, double* rd, double* r, double* c);
T_Node* constructBST(List_Node* tail);
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.