I need help with a link list, the datastructure receives each struct into the list, but loses the previous one when going through the loop. The first two result print was to check if the list did contain both structs when coming out from the function....
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char * Line;
struct node *next;
}New_prototype;
New_prototype ** Insert_head(New_prototype **head)
{
New_prototype * Current, *tempt;
Current = (New_prototype *)malloc(sizeof(New_prototype));
printf(" Enter a string below\n");
scanf("%s",Current->Line);
Current->next = NULL;
if(*head == NULL)
*head = Current;
else
{
tempt = *head;
while( tempt->next != NULL)
{tempt = tempt->next;}
tempt->next = Current;
}
free(Current);
return(head);
}
int main(void)
{
New_prototype * Head = NULL;
Insert_head(&Head);
printf(" Here is the result is %s \n",Head->Line);
Insert_head(&Head);
printf(" the next result is %s \n",Head->Line);
int i = 0;
while(Head != NULL)
{
char * sentence;
sentence = Head->Line;
printf(" Here is the result %s \n",sentence);
Head = Head->next;
getchar();
}
return(0);
}