Originally Posted by
blueboyz
Can you explain it more detailed? I bit confused with void *data. With an example of program maybe?
The point I was trying to make was that, the nodes and your list are containers for your data. Your data itself does not need to, and should not contain links. Using void* only means that any type of data can be attached to the list, not just student structs.
Here is an example with an insert in front function.
Code:
struct student {
char name[128];
char age;
};
struct node {
void *data;
struct node *next;
};
int insert_node(struct node **list, void *data) {
struct node *n = calloc(1, sizeof(struct node));
if(!n) return 0;
n->data = data;
if(*list == NULL) {
*list = n;
return 1;
}
n->next = *list;
*list = n;
return 1;
}
Then you could use it like this for example:
Code:
struct node *list = NULL;
struct student *student = NULL;
int retval = 0;
student = calloc(1, sizeof(struct student));
printf("Enter name: ");
scanf("%127s", student->name);
printf("Enter age: ");
scanf("%d", student->age);
retval = insert_node(&list, student);
if(retval == 0) // error
You could probably get some ideas by reading this as well. http://cslibrary.stanford.edu/103/LinkedListBasics.pdf