I'm new in C. My task is to implement Binary Search Tree. So, I'm really confused with pointers. Though, the concept is clear in trivial cases but using pointers in structures is confusing for me.
I have header
#include <stdio.h>
typedef struct node_st {
int value;
struct node_st *leftchild;
struct node_st *rightchild;
} node;
typedef struct tree_st {
int size;
node root;
} tree;
extern void insert(tree *t, int value);
I want to implement insert. Here is my attempt
extern void insert(tree *t, int value) {
int size = t->size;
node insertNode;
node *toInsert = &insertNode;
toInsert->value = value;
toInsert->leftchild = NULL;
toInsert->rightchild = NULL;
if(size == 0) {
t->root = toInsert;
t->size++;
} else {
node *current;
current = t->root;
for(int i = 0; i < size; ++i) {
if(current->value < value) {
if(current->rightchild != NULL) {
current = current->rightchild;
}
else {
current->rightchild = toInsert;
t->size++;
break;
}
} else if(current->value > value) {
if(current->leftchild != NULL) {
current = current->leftchild;
}
else {
current->leftchild = toInsert;
t->size++;
break;
}
} else {
printf("The value has been already inserted");
}
}
}
}
I get the following error:
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function
_start': (.text+0x20): undefined reference tomain' collect2: error: ld returned 1 exit status
Questions & Problems:
- What that error means?
- Are all pointers in the function correct?
- Is it necessary to define root as a pointer in
struct node_stat all?
main()function.breakafter theprintffor(int i = 0; i < size; ++i) {could simply bewhile(1) {