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 709651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:31:37+00:00 2026-05-14T04:31:37+00:00

I need to write AVL-tree with generic type in C. The best way I

  • 0

I need to write AVL-tree with generic type in C. The best way I know is to use [ void* ] and to write some functions for creating, copying, assignment and destruction. Please, tell me some better way.

  • 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-14T04:31:37+00:00Added an answer on May 14, 2026 at 4:31 am

    I will give you an example on how you can achieve generics functionality in C. The example is on a linked list, but I am sure you can adapt it on your AVL tree if necessary.

    First of all you will need to define a structure for list element. A possible (most simple implementation):

    struct list_element_s {
        void *data;
        struct list_element_s *next;
    
    };
    typedef struct list_element_s list_element;
    

    Where ‘data’ will act as the “container” where you are going to keep your information, and ‘next’ is the reference to the direct linked element. (NOTE: Your binary tree element should include a reference to the right / left children elements).

    After you create you element structure, you will need to create your list structure. A good practice is to have some members that are pointing to functions: destructor (needed to free the memory being hold by ‘data’), and comparator (to be able to compare two of your list elements).

    A list structure implementation could look like this:

    struct list_s {
        void (*destructor)(void *data);    
        int (*cmp)(const void *e1, const void *e2);
        unsigned int size;
        list_element *head;
        list_element *tail;
    
    };
    typedef struct list_s list;
    

    After you design your data structure, you should design your data structure interface. Let’s say our list will have the following, most simple, interface:

    nmlist *list_alloc(void (*destructor)(void *data));
    int list_free(list *l);
    int list_insert_next(list *l, list_element *element, const void *data);
    void *list_remove_next(list *l, list_element *element);
    

    Where:

    • list_alloc : will alocate memory for your list.
    • list_free : will free memory allocated for list, and all ‘data’ being held by list_element(s).
    • list_insert_next : will insert a new element next to ‘element’ . If ‘element’ is NULL, the insertion will be made at the head of the list.
    • list_remove_next : will remove & return (void*)’data’ being held by ‘element->next’ . If ‘element’ is NULL, it will perform “list->head removal”.

    And now the functions implementation:

    list *list_alloc(void (*destructor)(void *data))
    {
        list *l = NULL;
        if ((l = calloc(1,sizeof(*l))) != NULL) {
            l->size = 0;
            l->destructor = destructor;
            l->head = NULL;
            l->tail = NULL;
        }
        return l;
    }
    
    int list_free(list *l)
    {
        void *data;
        if(l == NULL || l->destructor == NULL){
            return (-1);
        }    
        while(l->size>0){    
            if((data = list_remove_next(l, NULL)) != NULL){
                list->destructor(data);
            }
        }
        free(l);
        return (0);
    }
    
    int list_insert_next(list *l, list_element *element, const void *data)
    {
        list_element *new_e = NULL;
        new_e = calloc(1, sizeof(*new_e));
        if (l == NULL || new_e == NULL) {
            return (-1);
        }
        new_e->data = (void*) data;
        new_e->next = NULL;
        if (element == NULL) {
            if (l->size == 0) {
                l->tail = new_e;
            }
            new_e->next = l->head;
            l->head = new_e;
        } else {
            if (element->next == NULL) {
                l->tail = new_e;
            }
            new_e->next = element->next;
            element->next = new_e;
        }
        l->size++;
        return (0);
    }
    
    void *list_remove_next(list *l, list_element *element)
    {
        void *data = NULL;
        list_element *old_e = NULL;
        if (l == NULL || l->size == 0) {
            return NULL;
        }
        if (element == NULL) {
            data = l->head->data;
            old_e = l->head;
            l->head = l->head->next;
            if (l->size == 1) {
                l->tail = NULL;
            }
        } else {
            if (element->next == NULL) {    
                return NULL;    
            }    
            data = element->next->data;
            old_e = element->next;
            element->next = old_e->next;
            if (element->next == NULL) {
                l->tail = element;
            }
        }
        free(old_e);
        l->size--;
        return data;
    }
    

    And now, how to use your simple generic linked list implementation. In the following example the list is acting like a stack:

    #include <stdlib.h>
    #include <stdio.h>
    #include "nmlist.h"
    
    void simple_free(void *data){
        free(data);
    }
    
    int main(int argc, char *argv[]){
        list *l = NULL;
        int i, *j;
    
        l = list_alloc(simple_free);
        for(i = 0; i < 10; i++){
            j = calloc(1, sizeof(*j));
            if(j != NULL){
                *j = i;
                list_insert_next(l, NULL, (void*) j);
            }
        }
    
        for(i = 0; i < 10; i++){
            j = (int*) list_remove_next(l, NULL);
            if(j != NULL){
                printf("%d \n", *j);
            }
        }
    
        list_free(l);
    
        return (0);
    }
    

    Note that instead of “int *j” you can use a pointer that references more complex structures. If you do, don’t forget to modify your ‘list->destructor’ function accordingly.

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

Sidebar

Related Questions

I need write a program to resize other program's windows, what I know is
I need write a program on Mac OS X to manage some number of
I use node.js for a while, now I need write an add-on, but I
I need to write application in .NET which will be make some calculations on
I need write an update statement that used multiple tables to determine which rows
I need to write a program used internally where different users will have different
I need to write a Java Comparator class that compares Strings, however with one
I need to write a Delphi application that pulls entries up from various tables
I need to write a web application using SQL Server 2005, asp.net, and ado.net.
I need to write a java script. This is supposed to validate if the

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.