B
Bill Cunningham
I have written these functions and the source code compiles but my
problem is in main(). I get either segmentation fault or a dereferncing
incomplete type error. I basically don't know how to call these functions
from main. I've tried all I know to try and it's beyond me.
Thanks for help.
Bill
#include <stdio.h>
#include <stdlib.h>
struct tree {
char *name;
int age;
int num;
char *color;
struct tree *right, *left;
};
struct tree *insert(struct tree *tp, int v, char *n, int a, char
*c)
{
if (tp) {
if (tp->num > v)
tp->left = insert(tp->left, v, n, a, c);
if (tp->num < v)
tp->right = insert(tp->right, v, n, a, c);
} else if (tp = malloc(sizeof *tp)) {
tp->num = v;
tp->right = tp->left = NULL;
}
return tp;
}
int has(struct tree *tp, int v)
{
if (tp) {
if (tp->num < v)
return has(tp->right, v);
if (tp->num > v)
return has(tp->left, v);
else
return 1;
}
return 0;
}
void print(struct tree *tp)
{
if (tp) {
print(tp->left);
printf("%i\n", tp->num);
print(tp->right);
}
}
#include <stdio.h>
#include <stdlib.h>
int main(void) {
struct tree *p;
has(p,1);}
problem is in main(). I get either segmentation fault or a dereferncing
incomplete type error. I basically don't know how to call these functions
from main. I've tried all I know to try and it's beyond me.
Thanks for help.
Bill
#include <stdio.h>
#include <stdlib.h>
struct tree {
char *name;
int age;
int num;
char *color;
struct tree *right, *left;
};
struct tree *insert(struct tree *tp, int v, char *n, int a, char
*c)
{
if (tp) {
if (tp->num > v)
tp->left = insert(tp->left, v, n, a, c);
if (tp->num < v)
tp->right = insert(tp->right, v, n, a, c);
} else if (tp = malloc(sizeof *tp)) {
tp->num = v;
tp->right = tp->left = NULL;
}
return tp;
}
int has(struct tree *tp, int v)
{
if (tp) {
if (tp->num < v)
return has(tp->right, v);
if (tp->num > v)
return has(tp->left, v);
else
return 1;
}
return 0;
}
void print(struct tree *tp)
{
if (tp) {
print(tp->left);
printf("%i\n", tp->num);
print(tp->right);
}
}
#include <stdio.h>
#include <stdlib.h>
int main(void) {
struct tree *p;
has(p,1);}