Question Details:
Maintain a family tree and output family data in correct order.
Details
To solve this problem you have to:
Declare structure named Family that has following eight members:
Structure Family
member of type Description
dob Structure Date Date of Birth
name character array of size 20 Name
father character array of size 20 Father’s name
mother character array of size 20 Mother’s name
next Structure Family pointer Pointer to next structure
previous Structure Family pointer Pointer to previous structure
p_to_father Structure Family pointer Pointer to father structure
p_to_mother Structure Family pointer Pointer to mother structure
Structure Date
member of type
day integer
month integer
year integer
Define a function (say get_personInfo) to take input of family members.
Arguments no arguments
Return type a pointer to a Family Structure.
Within the function
Define a pointer to Family type Structure (say temp) and allocate it memory by calling malloc()
Take following input from user:
Name of Person, Date of Birth, Father’s name, Mother’s name
and store this information into temp and return temp.
Define a function (say related) to fill in pointers to mother or father relationship
Arguments two pointers to Family Structure (say first person and second person)
Return type true or false
Within the function
call set_relationship twice to test all possibilities of relationship. That is:
return set_relationship(first person, second person) || set_relationship(second person, first person)
set_relationship()
Arguments two pointers to Family Structure (say first person and second person)
Return type true or false
Within the function
if first person’s father is equal to second person’s name
first person’s p_to_father = second person
return true
if first person’s mother is equal to second person’s name
first person’s p_to_mother = second person
return true
otherwise
return false
The main() function
Declarations:
name of type Description
first Structure Family pointer Pointer to first person
current Structure Family pointer Pointer to current person
last Structure Family pointer Pointer to last person
input char input character Y or N
/* take input from user */
infinite loop
printf “Do you want to enter details of a person (Y or N)? ”
scanf input
if input is n
break the loop
current = get_personInfo()
if first == NULL
first = current
last = current
else
last->next = current
current->previous = last
last = current
/* check and set the person’s relationships */
Point current to the first
loop until current->next is not NULL
declare a parent count of type int local to this block
point last to current->next
loop until last is not NULL
if related(current, last)
if(++parent == 2)
break the inner loop
last = last->next
current = current->next
/* Output family data in correct order */
Iterate through the list and print person’s name, birth day, birth month, birth year
and also parent’s information i.e.; name, birth day, birth month, birth year
/* Free the memory allocated through malloc() */
Iterate through the list and call
free(current)
Sample Output
Do you want to enter details of a person (Y or N)? y
Enter person name: ABC
Enter ABC’s date of birth (day month year): 2 2 70
Enter ABC’s father name: DEF
Enter ABC’s mother name: GHI
Do you want to enter details of a person (Y or N)? y
Enter person name: JKL
Enter JKL’s date of birth (day month year): 4 4 74
Enter JKL’s father name: MNO
Enter JKL’s mother name: PQR
Do you want to enter details of a person (Y or N)? y
Enter person name: STU
Enter STU’s date of birth (day month year): 6 6 96
Enter STU’s father name: ABC
Enter STU’s mother name: JKL
Do you want to enter details of a person (Y or N)? n
ABC was born in 2/2/70, and has DEF and GHI as parents.
JKL was born in 4/4/74, and has MNO and PQR as parents.
STU was born in 6/6/96, and has ABC and JKL as parents.
ABC’s Birth date is 2/2/70 and JKL’s birth date is 4/4/74.