I would like to populate a linked list which is in a node of a binary search tree. If the user already exits in the list add the ip to the linked list for that specific user.
This is what I have tried so far:
My Data Structures:
typedef struct ip{
int ip;
struct ip *ipNext;
}IP;
typedef struct bstNode
{
char data[32];
struct bstNode* left;
struct bstNode* right;
IP *ipHead; //Pointer to linked list.
}bstNode;
This is where I am having issues
My insert function to insert the IP address into the list for user:
bstNode insertIP(bstNode *head, char *username, int ip){
if (search(username)==1)
{
if (head->ipHead == NULL)
{
IP *temp;
temp = (IP*)malloc(sizeof(IP));
temp->ip = ip;
temp->ipNext = NULL;
}else{
head->ipHead->ip= ip;
head->ipHead->ipNext=NULL;
}
}
Insert Function (This works):
bstNode *insert(bstNode *node, char *word)
{
if(node==NULL){
node= malloc(sizeof(bstNode));
//IP* ipNode=malloc(sizeof(IP));
strcpy(node->data, word);
node->left=NULL;
node->right=NULL;
}
else{
if(strcmp(word, node->data)<0)
node->left=insert(node->left, word);
else if(strcmp(word, node->data)>0)
node->right=insert(node->right, word);
}
return node;
}
Search Function (This works):
void search(char* user, bstNode* root)
{
int res;
if( root!= NULL ) {
res = strcmp(root, root->data);
if( res < 0)
search( user, root->left);
else if( res > 0)
search( user, root->right);
else
printf("User Found\n");
return 1;
}
else printf("\nNot in tree\n");
return 0;
}