Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i have to write a linked list program in C, in which the number of members within each node itself is variable.The members within node could be of same or different data types. Any ideas would be greatly appreciated...
Posted

If you've a finite set of possible node type, then you may define a struct for each node type, and store, in every node both
<ul><li>a pointer of the actual data structure, as <code>void *</code>.</li>
<li>a integer value representing the actual type of the data.</li>

(of course you've also to store a pointer to the next item of the list...)

for instance:
/* data structs, i.e. node types */
typedef struct tagPoint
{
  int x;
  int y;
}Point;

typedef struct tagUser
{
  const char * name;
  int age;
  /*..*/
} User;
/*... other data types...*/

typedef enum  TagNodeType
{
  ePoint,
  eUser,
  /*...*/
  eNODETYPES
} NodeType;

typedef struct TagListItem
{
  NodeType nt;
  void * pData;
  struct TagListItem * pNext;
} ListItem;


Then implement a addListItem function accepting a pointer to actual data and a value representing the data type.
On retrieving data you've to check the nt field in order to properly cast the pData pointer.

:)


 
Share this answer
 
v2
Are you talking about a B Tree?
 
Share this answer
 
Actually any concept in C, that could implement this sort of requirement is fine. To simplify let us consider that members within a node can be of same data type for example int. So there can be some members with int and some with integer array.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900