Creating a list in C implementing SLL.
struct dat
{
char fname[20];
char lname[20];
};
typedef struct node_s
{
struct dat data;
struct node_s *next;
}NODE;
NODE *list=NULL;
NODE *list_create(struct dat *data)
{
NODE *node;
if(!(node=malloc(sizeof(NODE))))
{
return NULL;
}
node->data = data;
node->next = NULL;
return node;
}
NODE *list_insert_after(NODE *node, struct dat *data)
{
NODE *newnode;
newnode = list_create(data);
newnode->next = node->next;
node->next = newnode;
return newnode;
}
I cant find any good examples using SLL. Now i have this append function now i will apply the two SLL functions, list_create and list_insert_after. Is this correct?
void app(struct dat x)
{
FILE *fp;
fp=fopen("database.dat","a");
if(fp==NULL)
{
printf("file error");
}
if(fp!=NULL)
{
printf("enter lname: ");
gets(x.lname);
printf("enter fname: ");
gets(x.fname);
fprintf(fp,"%s %s\n",x.lname,x.fname);
if(list == NULL)
{
list=list_create((&x));
}
else
{
next=list_insert_after(list,&x);
}
}
fclose(fp);
}