the problem says to ask input from user about name and age and store it in linked lists also ask him a number each time he finishes putting data in the linked list if he gives 1 that means he wants to store more if he gives 0 that means he doesnt want to store anything else
then we want to show the results on screen
here is what i ve done so far
Code:
#include <stdio.h>
#include <stdlib.h>
struct node{
char name[20];
int age;
struct node *next;
};
struct node *prosthiki(int il, char on[]){
struct node *a;
a = malloc(sizeof(struct node));
a->age = il;
strcpy(a->name,on);
a->next = NULL;
return a;
}
struct node *prosthiki2(int il, char on[], struct node *lol){
lol = malloc(sizeof(struct node));
lol->age = il;
strcpy(lol->name,on);
lol->next = NULL;
return lol;
}
void show(struct node *head){
struct node *l;
l=head;
while(l!=NULL){
printf("%d ",l->age);
printf("%s\n",l->name);
l = l->next;
}
}
int main(void){
int check = 1;
int count=0;
struct node *head = NULL;
struct node *current = NULL;
char on[20];
int gad;
while (check!=0){
printf("give name: \n");
scanf("%s",on);
fflush(stdin);
printf("give age: \n");
scanf("%d",&gad);
fflush(stdin);
if (count==0){
head = prosthiki(gad,on);
}
else {
current = prosthiki2(gad,on,current);
head->next = current;
current = current->next;
}
printf("success!!!!\n\n");
printf("do you want to continue: \n");
scanf("%d",&check);
fflush(stdin);
count++;
}
show(head);
getchar();
return 0;
}
it works for 2 nodes and 1 node, it doesnt work for more than 2 nodes
what am i doing wrong?
thanks
ps: it's not an assignment i was reading an ebook to understand linked lists and came up with this problem