Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7555521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:37:53+00:00 2026-05-30T11:37:53+00:00

Suppose I got an external library bst that handles custom data types insertion in

  • 0

Suppose I got an external library bst that handles custom data types insertion in a bst

Here are the new_node,insert and search functions :

//new node

struct bst_node* new_node(void* data) 
{
    struct bst_node* result = malloc(sizeof(struct bst_node));
    assert(result);

    result->data = data;
    result->left = result->right = NULL;
    return result;
}

//insert node

void insert(struct bst_node** root, void* data) {
    struct bst_node** node = search(root, data);
    if (*node == NULL) {
        *node = new_node(data);
    }
}

//search node

    struct bst_node** search(struct bst_node** root, void* data) {
        struct bst_node** node = root;
        while (*node != NULL) {

        if (data, (*node)->data < 0)
            node = &(*node)->left;
        else if (compare_result > 0)
            node = &(*node)->right;
        else
            break;
    }
    return node;
}

and the main.c ,suppose i read the models from a txt file :

#include <stdio.h>
#include <stdlib.h>
#include "bst.h"
#include <string.h>

#define MAX 50

typedef struct data_t{
    int gg,mm,aaaa;    
}data;

typedef struct accesories_t{
    char name[MAX];
    int price;
    struct accesories_t *next;    
}accesories;

typedef struct model_t{
    //int index;
    char name[MAX];
    char file_a[MAX];
    data date;
    int price;
    accesories *acs;
}model;


int main(int argc, char *argv[])
{
    int menu=0;

    char nf[MAX];

    char name[MAX];
    char fa[MAX];
    int price,gg,mm,a;

    strcpy(nf,argv[1]);

    FILE *fp=fopen(nf,"+r");
    model m;
    struct bst_node* root = NULL;

    while(fscanf(fp,"%s %d//%d//%d %d %s",name,gg,mm,a,price,fa)!=EOF){
        strcpy(m.name,name);
        strcpy(m.file_a,fa);
        m.date.gg=gg;
        m.date.mm=mm;
        m.date.aaaa=a;
        m.price=price;
        m.index=index++;  

        insert(&root ,m);  
    }

  system("PAUSE");  
  return 0;
}

So my question arises in the search function, how can i manage a comparator on custom data (let’s say insert the models ordered by name (strcmp) ?
I’m very confused on how can i pass the names to the bst.c given that bst.c has no idea how my model struct is made.

Should I modify the bst library and maybe on bst struct add before data some sort of index and use that as comparator ?

OK I’ve managed to fix that by adding a string key inside the struct bst

What I’m trying to achieve now is to return the void* data type casted into struct model,
suppose _I got the tree with nodes containing the data, once I do a search I’d like to return for
example the data contained in a node and work on it, any clues ????

tried someting like without any success
suppose node is a returned node from a search function

model *m;
m=(model*)node->data;

how could I achieve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:37:55+00:00Added an answer on May 30, 2026 at 11:37 am

    Example for using compare functions as callbacks. Definitions omitted for brevity.

    int llist_cmp(struct llist *l, struct llist *r)
    {
    if (!l) return 1;
    if (!r) return -1;
    return strcmp(l->payload,r->payload);
    }
    
    struct llist * llist_split(struct llist **hnd, int (*cmp)(struct llist *l, struct llist *r) )
    {
    struct llist *this, *save, **tail;
    
    for (save=NULL, tail = &save; this = *hnd; ) {
            if (! this->next) break;
            if ( cmp( this, this->next) <= 0) { hnd = &this->next; continue; }
            *tail = this->next;
            this->next = this->next->next;
            tail = &(*tail)->next;
            *tail = NULL;
            }
    return save;
    }
    
    struct llist * llist_merge(struct llist *one, struct llist *two, int (*cmp)(struct llist *l, struct llist *r) )
    {
    struct llist *result, **tail;
    
    for (result=NULL, tail = &result; one && two; tail = &(*tail)->next ) {
            if (cmp(one,two) <=0) { *tail = one; one=one->next; }
            else { *tail = two; two=two->next; }
            }
    *tail = one ? one: two;
    return result;
    }
    

    BTW: the above snippet handles linked lists, but the mechanism for passing function pointers is the same as with trees, of course. And after all it was homework 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you've got a customer who wants an application that has its data centralized
I've got an iPhone App that is supposed to send POST data to my
Suppose I've got a custom form label with some HTML on it like so:
Suppose it's a website that sells photo cameras. Here are my entities (tables): Camera:
Suppose you've got a webapp that's passing usernames and passwords around in hidden form
Suppose I've got a method that accepts an array and processes each element in
Suppose I got this function call: cook(rice, kitchen->pot); After writing that, I notice parameter
Suppose I've got a generic MyClass<T> that needs to compare two objects of type
Suppose I got a div that uses AJAX to retrieve prices and information about
I just got a question that I can't answer. Suppose you have this loop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.