Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct person{
char name[20];
char father[20];
struct person *next;
}tPerson;
void createList(tPerson** list)
{
int i, qFather, qSons, qGrandsons, j, k;
char namefather[20], hisName[20];
printf("\nHow many parents?: ");
scanf("%d", &qFather);
for(i=0; i<qFather; i++)
{
printf("\nname father %d: ", i);
scanf("%s", hisName);
printf("\nname of his father: ");
scanf("%s", namefather);
insertAtTheEnd(list, hisName, namefather);
printf("\nHow many sons?: ");
scanf("%d", &qSons);
for(j=0; j<qSons; j++)
{
printf("\nname: ");
scanf("%s", hisName);
printf("\nname of his father: ");
scanf("%s", namefather);
insertAtTheEnd(list, hisName, namefather);
printf("\nHow many sons?: ");
scanf("%d", &qGrandsons);
for(k=0; k<qGrandsons; k++)
{
printf("\nname: ");
scanf("%s", hisName);
printf("\nname of his father: ");
scanf("%s", namefather);
insertAtTheEnd(list, hisName, namefather);
}
}
}
}
void insertAtTheEnd(tPerson** person, char name[20], char father[20])
{
if((*person)!=NULL)
insertAtTheEnd(&((*person)->next), name, father);
else
{
(*person)=(tPerson*)malloc(sizeof(tPerson));
strcpy((*person)->name, name);
strcpy((*person)->father, father);
(*person)->next=NULL;
}
}
void printlist(tPerson* list)
{
tPerson*aux;
aux=list;
while(aux!=NULL)
{
printf("\nname: %s", aux->name);
printf("\nfather: %s", aux->father);
aux=aux->next;
}
}
tPerson* searchNodeInlist(tPerson* list, char name[20])
{
int a;
while(list!=NULL)
{
a=strcmp(list->name, name);
if(a==0)
return list;
list=list->next;
}
return NULL;
}
int searchFather(tPerson* list, char name[20])
{
int a, b;
tPerson* father;
tPerson* searched;
tPerson* first=list;
searched=(tPerson*)malloc(sizeof(tPerson)); // I don't know it this is necessary
father=(tPerson*)malloc(sizeof(tPerson)); // I don't know it this is necessary
searched=searchNodeInlist(list, name);
while(list!=NULL)
{
a=strcmp(searched->father, list->name);
if(a==0)
{
father=searchNodeInlist(list, searched->father);
while(first!=NULL){
b=strcmp(father->father, first->name);
if(b==0)
{
return 2; //has father and grandfather
}
first=first->next;
return 1; //has father
}
}
list=list->next;
}
return 0; //has no father
}
tPerson* lastNi(tPerson* ni, tPerson* actual)
{
if((ni)!=NULL)
lastNi(((ni)->next), actual);
else{
(ni)=(tPerson*)malloc(sizeof(tPerson));
strcpy((ni)->name, actual->name);
strcpy((ni)->father, actual->father);
((ni)->next)=NULL;
}
return ni;
}
tPerson* listgrandsons(tPerson* grandsons)
{
int q, n=0;
tPerson* following=NULL;
tPerson* previous=NULL;
tPerson* actual=grandsons;
tPerson* ni=NULL;
tPerson* niAux=NULL;
previous=grandsons;
actual=actual->next;
following=actual->next;
while(actual!=NULL)
{
q=searchFather(grandsons, actual->name);
if(q==2) //if q==2, it's a grandson
{
previous->next=following;
ni=lastNi(ni, actual);
actual=actual->next;
following=actual->next;
printlist(ni);
}
else{
if(following==NULL)
return ni;
previous=actual;
actual=following;
following=following->next;
}
}
return ni;
}
int main()
{
tPerson* list=NULL;
tPerson* ni=NULL;
char nameB[20];
int qFathers;
createList(&list);
printlist(list);
printf("\nname fathers to search: ");
scanf("%s", nameB);
qFathers=searchFather(list, nameB);
ni=listgrandsons(list);
printlist(ni);
printlist(list);
return 0;
}
I hope you can help me! I don't know what to do