top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

If we are using C language to implement the heterogeneous linked list, what pointer type will we use,explain?

+2 votes
5,488 views
If we are using C language to implement the heterogeneous linked list, what pointer type will we use,explain?
posted Sep 9, 2013 by Arvind Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

Don't know what you are trying to do it would be better if you provide more details, check if the following helps you -

typedef struct {
    int type_indicator;
    union {
        float f;
        int i;
        double d;
        void *p;
        char c;
    }
} generic_item;

Above struct that includes a type indicator and a union of the various types that you want to handle will cover almost all the stuff and u can use this build your link list.

answer Sep 9, 2013 by Salil Agrawal
+2 votes

A linked list is known as heterogeneous when every node of linked list can contain different type of information.
A void pointer can point to any type of data either in-built data type or user defined structure . storing such pointer in a void in not a problem. Problem comes at the time of accessing/ reading. since without knowing data structure type how it can be deferenced . so before accessing you should aware about the type. If your program has so many user defined different different data types then defined a

typedef enum { char, integer, float, double, struct type 1, struct type 2 ...etc } datatype;

typedef Struct  heterogeneous_linkedlist
{
datatype type;
void * nodeInfo.
Struct  heterogeneous_linkedlist *next;
} heterogeneous_node;
answer Sep 9, 2013 by Vimal Kumar Mishra
...