Mega Code Archive

 
Categories / C / Data Structure Algorithm
 

Basics of a family tree 2

#include <stdio.h> #include <ctype.h> #include <stdlib.h> struct Cat *getCat(void); struct Date {    int day;    int month;    int year; }; struct Cat {    struct Date dob;    char name[20];    char father[20];    char mother[20];    struct Cat *next;    struct Cat *previous; }; int main() {    struct Cat *first = NULL;    struct Cat *current = NULL;    struct Cat *last = NULL;    char more = '\0';    int i =0;    for(i =0;i<5 ;i++ )    {      current = getCat();      if(first == NULL){        first = current;        last = current;      }else {        last->next = current;        current->previous = last;        last = current;      }    }    while (current  != NULL)    {      printf("\n%s was born %d/%d/%d, and has %s and %s as parents.",               current->name, current->dob.day, current->dob.month,               current->dob. year, current->father,  current->mother );      last = current;     /* Save pointer to enable memory to be freed */      current = current->previous;  /* current points to previous list */      free(last);    } } struct Cat *getCat(void) {    struct Cat *temp;    temp = (struct Cat*) malloc(sizeof(struct Cat));    printf("\nEnter the name of the person: ");    scanf("%s", temp -> name );    printf("\nEnter %s's date of birth (day month year); ", temp->name);    scanf("%d %d %d", &temp->dob.day, &temp->dob.month, &temp->dob.year);    printf("\nWho is %s's father? ", temp->name );    scanf("%s", temp->father );    printf("\nWho is %s's mother? ", temp -> name );    scanf("%s", temp -> mother );    temp->next = temp->previous = NULL;    return temp; }