///////////////////////////////////////////////// // // Linked Lists // // Examples for 05May08 // //////////////////////////////////////////////// #include //**************************************************************** // Malloc returnms a pointer to a memory cell of size specified. // examp01 () { int count; // An integer int s; // Our size int *ptr; // Pointer to an integer void *malloc (); // Prototype for *malloc () count = 3; // Initialize count s = sizeof (int); // Get size ptr = malloc (s); // Allocate storage of the right size *ptr = 4; // Initialize to 4 *ptr = count + *ptr; // Use in calculation printf ("Examp 1:size = %d, *ptr = %d", s, *ptr); // Verify result } //****************************************************************** // Calloc returns a pointer to a group of contiguous memory cells // like an array (hear what i'm sayin' ?) examp02 () { void *calloc (int, int); // Prototype for *calloc () int *ptr; // Pointer to integer int i; // Index variable int s; // Size again int sa = 25; // Size of array (say 25) s = sizeof (int); // Always want to do this (int can change) ptr = calloc (sa, s); // Allocate an array of sa = 25 ints printf ("\nExamp 2:"); // for (i = 0; i < sa; i++) // Verify { *(ptr + i) = i; // Note ptr vs array printf ("%d ", ptr [i]); // Print out contents of position } printf ("\n"); // Clean up } //********************************************************************* // This could be in header void strcpy (char *, char *); // Prototye for strcpy (eliminates warnings) struct zoo // Define a new structure { char name [10]; // A string int feed; // struct zoo *ps; // Pointer to next item }; typedef struct zoo ZOO; // Define a new type ZOO // could be combined with struct // (usually is) ZOO a1, a2, a3; // Three anaimals of type ZOO ZOO *start, *new, *ptr; // Pointers to animals //----------------------------------------------------------------- // New animal routine // Returns an animal (of type ZOO) filled in with items passed ZOO new_animal (char *name, int feed, ZOO *zptr) { ZOO animal; // New item strcpy (animal.name, name); animal.feed = feed; animal.ps = zptr; return (animal); } //----------------------------------------------------------------- // Traverses list and prints out animals and feed requirements print_zoo (ZOO *ptr) { // Should test ptr == NULL here (empty list) while (1 == 1) // Loop forever { printf ("\nName %s Feed: %d", ptr->name, ptr -> feed); if (ptr->ps == NULL) // Is this the last record? break; // If so, were outa here else // Otherwise ptr = ptr->ps; // Move pointer down }; printf ("\n"); } // end print_zoo //------------------------------------------------------------------- examp03 () { void *malloc (); // Prototype for malloc () //------------------------------------------------------------------ //-- Define three new animals-- a1 = new_animal ("Alligator", 1, &a2); a2 = new_animal ("Bear", 2, &a3); a3 = new_animal ("Cat", 3, NULL); // We could say a3.ps = NULL; start = &a1; // Set up pointer to start of chain print_zoo (start); // Print zoo to check // New animal in zoo new = malloc (sizeof (ZOO)); // Allocate space; new is pointer *new = new_animal ("Dog", 4, NULL); // He's in // Now we need to put him in chain of references a3.ps = new; print_zoo (start); // Verify // Lets do another one new = malloc (sizeof (ZOO)); // Get another chunk of memory *new = new_animal ("Eagle", 5, NULL); ptr = start; // Point to start of list while (ptr -> ps !=NULL) // See if pointer is NULL ptr = ptr -> ps; // if NULL then end of list ptr -> ps = new; // so tack on new animal print_zoo (start); // Verify // Lets do another one -------------------------------------------- new = malloc (sizeof (ZOO)); // Get another chunk of memory *new = new_animal ("Ferret", 6, NULL); ptr = start; // Point to start of list while (ptr -> ps !=NULL) // See if pointer is NULL ptr = ptr -> ps; // if NULL then end of list ptr -> ps = new; // so tack on new animal // Same code as before -- routine here?? //print_zoo (start); // Verify } //******************************************************************* //******************************************************************* main () { examp01 (); examp02 (); examp03 (); }