Daisy chaining the horses both ways : Structure Array « Structure « C / ANSI-C

Home
C / ANSI-C
1.assert.h
2.Console
3.ctype.h
4.Data Structure Algorithm
5.Data Type
6.Development
7.File
8.Function
9.Language Basics
10.Macro Preprocessor
11.Math
12.math.h
13.Memory
14.Pointer
15.setjmp.h
16.signal.h
17.Small Application
18.stdio.h
19.stdlib.h
20.String
21.string.h
22.Structure
23.time.h
24.wctype.h
C Tutorial
C++
C++ Tutorial
Visual C++ .NET
C / ANSI-C » Structure » Structure ArrayScreenshots 
Daisy chaining the horses both ways
Daisy chaining the horses both ways

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
struct cat
{
     int age;
     int height;
     char name[20];
     char father[20];
     char mother[20];
     struct cat *next;     /* Pointer to next structure         */
     struct cat *previous; /* Pointer to previous structure     */
};

int main()
{

   struct cat *first = NULL;
   struct cat *current = NULL;
   struct cat *last = NULL;

   char test = '\0';

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

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

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

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

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

     printf("\nHow high is %s ( in hands )? ", current -> name);
     scanf("%d", &current -> height);     /* Read the cat'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 its the last cat...*/
     last = current;           /* Save address of last cat   */
   }

   while(current != NULL)      /* Output cat 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 cat we output */
   }
}

           
       
Related examples in the same category
1.Using a linked list of structures representing a person's name
2.Exercising the horses: Structure array declaration
3.A simple mailing list example using an array of structuresA simple mailing list example using an array of structures
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.