I have troubles creating the rootnode of an Binary Search Tree.
I defined the node in the headerfile bst.h:
#ifndef BST_H_
#define BST_H_
struct BST
{
int data; /* data contained at root */
struct BST *left; /* left node */
struct BST *right; /* right node */
};
typedef struct BST BST;
BST * bst_new (int data);
#endif /* BST_H_ */
Here is the bst.c file :
#include "stdafx.h"
#include "bst.h"
#include <stdio.h>
BST* bst_new(int data)
{
BST *new_node = (BST*)malloc(sizeof(BST));
if (new_node == NULL)
{
fprintf(stderr, "Out of memory!!! (create_node)\n");
exit(1);
}
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
And now i try to create the Rootnode of the BST with:
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include "bst.h"
int main ()
{
/* create new tree */
BST *bst = bst_new(10);
return 0;
}
But i get an Error in bst.c saying that i make an "Write access violation" in line "new_node->data = data;". How can i get this thing working?