Daisy chaining the horses both ways

 
#include
#include
#include

int main(void)
{
struct horse /* Structure declaration */
{
int age;
int height;
char name[20];
char father[20];
char mother[20];
struct horse *next; /* Pointer to next structure */
struct horse *previous; /* Pointer to previous structure */
};

struct horse *first = NULL; /* Pointer to first horse */
struct horse *current = NULL; /* Pointer to current horse */
struct horse *last = NULL; /* Pointer to previous horse */

char test = '\0'; /* Test value for ending input */

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

/* Allocate memory for each new horse structure */
current = (struct horse*)malloc(sizeof(struct horse));

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

printf("\nEnter the name of the horse: ");
scanf("%s", current -> name ); /* Read the horse's name */

printf("\nHow old is %s? ", current -> name);
scanf("%d", ¤t -> age); /* Read the horse's age */

printf("\nHow high is %s ( in hands )? ", current -> name);
scanf("%d", ¤t -> height); /* Read the horse's height */

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

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

current -> next = NULL; /* In case it's the last horse..*/
last = current; /* Save address of last horse */
}

/* Now tell them what we know. */
while(current != NULL) /* Output horse data in reverse order */
{
printf("\n\n%s is %d years old, %d hands high,",
current->name, current->age, current->height);
printf(" and has %s and %s as parents.", current->father,
current->mother);
last = current; /* Save pointer to enable memory to be freed */
current = current->previous; /* current points to previous in list */
free(last); /* Free memory for the horse we output */
}
return 0;
}




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!!!