Basics of a family tree in C

 
#include
#include
#include

struct Family *get_person(void); /* Prototype for input function */

struct Date
{
int day;
int month;
int year;
};

struct Family /* Family structure declaration */
{
struct Date dob;
char name[20];
char father[20];
char mother[20];
struct Family *next; /* Pointer to next structure */
struct Family *previous; /* Pointer to previous structure */
};

int main(void)
{
struct Family *first = NULL; /* Pointer to first person */
struct Family *current = NULL; /* Pointer to current person */
struct Family *last = NULL; /* Pointer to previous person */
char more = '\0'; /* Test value for ending input */

for( ; ; )
{
printf("\nDo you want to enter details of a%s person (Y or N)? ",
first != NULL?"nother" : "");
scanf(" %c", &more);
if(tolower(more) == 'n')
break;

current = get_person();

if(first == NULL)
{
first = current; /* Set pointer to first Family */
last = current; /* Remember for next iteration */
}
else
{
last->next = current; /* Set next address for previous Family */
current->previous = last; /* Set previous address for current */
last = current; /* Remember for next iteration */
}
}

/* Now tell them what we know */

/* Output Family data in reverse order */
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); /* Free memory for the Family we output */
}
return 0;
}

/* Function to input data on Family members */
struct Family *get_person(void)
{
struct Family *temp; /* Define temporary structure pointer */

/* Allocate memory for a structure */
temp = (struct Family*) malloc(sizeof(struct Family));

printf("\nEnter the name of the person: ");
scanf("%s", temp -> name ); /* Read the Family's 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 ); /* Get the father's name */

printf("\nWho is %s's mother? ", temp->name );
scanf("%s", temp -> mother ); /* Get the mother's name */

temp->next = temp->previous = NULL; /* Set pointers to NULL */

return temp; /* Return address of Family structure */
}




Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!