generic linked list

Given a structure

struct node
{
void *data;
struct node *link;

};
write a function to insert elements in list.

Comments

  1. void insert(struct node **s, void *data,unsigned int n)
    {
    struct node *temp,*r;
    int i;
    if(*s == NULL)
    {
    temp = malloc(sizeof(struct node));
    temp->data = malloc(n);
    for (i = 0; i < n; i++)
    *(char *)(temp->data + i) = *(char *)(data + i);
    temp->link = NULL;
    *s = temp;
    }
    else
    {
    temp =*s;
    while(temp->link != NULL)
    temp = temp->link;
    r = malloc(sizeof(struct node));
    r->data = malloc(sizeof(n));
    for (i = 0; i < n; i++)
    *(char *)(r->data + i) = *(char *)(data + i);
    r->link = NULL;
    temp->link = r;
    }
    }

    In order to display the value You need to send the type of data it is storing as a parameter.

    ReplyDelete

Post a Comment

Popular posts from this blog