Hi,
Apologies !! . I have made changes to the code again and added function to free memory.
Code:
#include <stdio.h>
typedef struct test{
char name[10];
char city[10];
int marks;
struct test *node;
}RECORD;
void reading_values (RECORD *);
void free_allocated_memory (RECORD *);
int main()
{
int i,v,j,g;
RECORD *allocation_for_struct, *next,*head;
allocation_for_struct=(RECORD *)(malloc(sizeof(RECORD)));
printf("value of allocation_for_struct is %u", allocation_for_struct);
printf("\n how many entries do you want to make?");
scanf("%d",&v);
for (j=0;j<v;j++)
{
if (j==0)
{
head=allocation_for_struct;
printf("\n enter #### %d ####name ",j+1);
scanf("%s",head->name);
printf("\n enter #### %d ####city ",j+1);
scanf("%s",head->city);
printf("\n enter #### %d ####marks ",j+1);
scanf("%d",&head->marks);
printf("\nvalue of original pointer %u",allocation_for_struct);
allocation_for_struct= (RECORD *)(malloc(sizeof(RECORD)));
head->node=allocation_for_struct;
printf("\nvalue of pointer %u",head->node);
printf("\nvalue of next pointer %u",allocation_for_struct);
printf("\n##########################");
}
else
{
next=allocation_for_struct;
printf("\n enter #### %d ####name ",j+1);
scanf("%s",next->name);
printf("\n enter #### %d ####city ",j+1);
scanf("%s",next->city);
printf("\n enter #### %d ####marks ",j+1);
scanf("%d",&next->marks);
printf("\nvalue of original pointer %u",allocation_for_struct);
allocation_for_struct=(RECORD *)(malloc(sizeof(RECORD)));
next->node=allocation_for_struct;
printf("\nvalue of pointer %u",next->node);
printf("\nvalue of next pointer %u",allocation_for_struct);
printf("\n##########################");
}
}
next->node = NULL;
printf("\nvalue of pointer %u",next->node);
free(allocation_for_struct);
printf("To read the list : please press 1 :: ");
scanf("%d",&g);
if(g==1)
{
reading_values(head);
free_allocated_memory(head);
}
return 0;
}
/* Function to read values from the list*/
void reading_values(RECORD *head)
{
RECORD *pointer_list_retreaval;
int count=1;
pointer_list_retreaval=head;
while(pointer_list_retreaval!=NULL)
{
printf("\n @@@@@@@@@@@@@@@@@@@HERE IS YOUR DATA @@@@@@@@@@@@@@@@@@@@@@@@@@@@");
printf("\n %d name :: %s #### ",count,pointer_list_retreaval->name);
printf("\n %d city :: %s #### ",count,pointer_list_retreaval->city);
printf("\n %d marks :: %d ####marks ",count,pointer_list_retreaval->marks);
printf("\n pointer location ###### %u ######",pointer_list_retreaval);
printf("\n next pointer location ###### %u ######",pointer_list_retreaval->node);
printf("\n @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
pointer_list_retreaval=pointer_list_retreaval->node;
count++;
}
}
/*Freeing memory*/
void free_allocated_memory(RECORD *head)
{
RECORD *pointer_list_retreaval,*temp;
pointer_list_retreaval=head;
printf("\n#########################################");
printf("\n####### This is head %u #######",head);
while(pointer_list_retreaval!=NULL)
{
temp=pointer_list_retreaval;
printf("\n #######this is pointer to the list %u #####",pointer_list_retreaval);
pointer_list_retreaval=temp->node;
printf("\n #######this is pointer temp %u #####",temp);
free(temp);
}
}