My Code is:-
/* Program to store data in link list and then print all data*/
#include <stdio.h>
#include <malloc.h>
struct value
{
char data;
struct value *next;
};
void main(void)
{
struct value *head, *temp;
char i;
head = (struct value *) malloc (sizeof(struct value));
temp = head;
while (temp != NULL)
{
printf("Enter value to store in link list Enter z to stop\n");
scanf("%c", &i);
temp->data = i;
if (i == 'z')
{
temp->next = NULL;
}
else
{
temp->next = (struct value *) malloc (sizeof(struct value));
}
temp = temp->next;
}
temp = head;
while (temp != NULL)
{
printf("Data is %c\n", temp->data);
temp = temp->next;
}
}
Output:-
Enter value to store in link list Enter z to stop
a
Enter value to store in link list Enter z to stop
Enter value to store in link list Enter z to stop
b
Enter value to store in link list Enter z to stop
Enter value to store in link list Enter z to stop
c
Enter value to store in link list Enter z to stop
Enter value to store in link list Enter z to stop
z
Data is a
Data is
Data is b
Data is
Data is
c
Data is
Data is
z
Process exited after 4.95 seconds with return value 0 Press any key to continue . . .
Please tell me why its printing one statement twice. Whats wrong with the code Thanks