Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Examples  

test_list.c

This file shows how to use the function list_init().

#include <stdlib.h>
#include <stdio.h>
#include "list.h"



/* ----------------------------------------------------------- */
/* Type of data to put into the linked list                    */
/* ----------------------------------------------------------- */

struct sdata {
                int   size;
                char  *s;    /* dinamically allocated */
             };

/* ----------------------------------------------------------- */
/* Function used to destroy the data                           */
/* ----------------------------------------------------------- */

void free_data (void* d)
{
  struct sdata* data;
  data = (struct sdata*)d;

  free (data->s);
  free (data);

  return;
}

/* ----------------------------------------------------------- */
/* Function used to compare two elements                       */
/* ----------------------------------------------------------- */

int compare_element (const void* d1, const void* d2)
{
  struct sdata *data1, *data2;
  data1 = (struct sdata*)d1;
  data2 = (struct sdata*)d2;

  if (strcmp(data1->s, data2->s) == 0) { return 1; }

  return 0;
}

/* ----------------------------------------------------------- */
/* Program's entry point                                       */
/* ----------------------------------------------------------- */

int main (int argc, char *argv[])
{
   List          liste;
   int           i, max;
   struct sdata  *data, d;
   char          buff[32];
   ListElmt      *element, *prec;

   /* -------------------------------------------------------- */
   /* Check command line arguments                             */
   /* -------------------------------------------------------- */

   if (argc != 2)
   {
      fprintf (stderr, "\nUsage: list.test <number of elements>\n");
      return 1;
   }

   max = atoi (argv[1]);

   /* -------------------------------------------------------- */
   /* initialize the linked list                               */
   /* -------------------------------------------------------- */

   list_init (&liste, free_data, compare_element);

   /* -------------------------------------------------------- */
   /* Fill linked list with elements                           */
   /* -------------------------------------------------------- */

   for (i=0; i<max; i++)
   {
     /* ------------------------------------------------------ */
     /* Allocate new data structure                            */
     /* ------------------------------------------------------ */

     data = (struct sdata*)malloc(sizeof(struct sdata));
     if (data == NULL)
     {
        fprintf (stderr, "\nError: can not allocate memory\n");
        return 1;
     }

     /* ------------------------------------------------------ */
     /* initialize the newly allocated data structure          */
     /* ------------------------------------------------------ */

     sprintf (buff, "%d", i);
     data->size = strlen(buff);

     data->s = (char*)malloc((data->size+1)*sizeof(char));
     if (data->s == NULL)
     {
        fprintf (stderr, "\nError: can not allocate memory\n");
        return 1;
     }

     strcpy (data->s, buff);

     /* ------------------------------------------------------ */
     /* Add a new element to the linked list                   */
     /* ------------------------------------------------------ */

     if (list_ins_next (&liste, list_tail(&liste), data) == -1)
     {
        fprintf (stderr, "\nError: can not allocate memory\n");
        return 1;
     }
   }

   /* -------------------------------------------------------- */
   /* Print the linked list                                    */
   /* -------------------------------------------------------- */

   element = list_head (&liste);

   while (element != NULL)
   {
     fprintf (stdout, "\n%s", ((struct sdata*)list_data(element))->s);
     element = list_next(element);
   }

   /* -------------------------------------------------------- */
   /* Searching the linked list                                */
   /* -------------------------------------------------------- */

      /* ----------------------------------------------------- */
      /* First search                                          */
      /* ----------------------------------------------------- */

   d.size = 1;
   sprintf (buff, "4");
   d.s = buff;

   fprintf (stdout, "\nSearching (from the beginning) for element containing [%s]", d.s);
   element = list_find (&liste, NULL, &d, &prec);
   if (element == NULL)
   {
     fprintf (stdout, "\nNot found");
   }
   else
   {
     fprintf (stdout, "\nFound");
   }

      /* ----------------------------------------------------- */
      /* Second search                                         */
      /* ----------------------------------------------------- */

   d.size = 1;
   sprintf (buff, "1000");
   d.s = buff;

   fprintf (stdout, "\nSearching (from the beginning) for element containing [%s]", d.s);
   element = list_find (&liste, NULL, &d, &prec);
   if (element == NULL)
   {
     fprintf (stdout, "\nNot found");
   }
   else
   {
     fprintf (stdout, "\nFound");
   }

      /* ----------------------------------------------------- */
      /* Third search                                          */
      /* ----------------------------------------------------- */

   d.size = 1;
   sprintf (buff, "4");
   d.s = buff;

   element = list_find (&liste, NULL, &d, &prec);
   if (element != NULL)
   {
      d.size = 1;
      sprintf (buff, "1000");
      d.s = buff;

      fprintf (stdout, "\nSearching (from the element that contains \"4\") for element containing [%s]", d.s);
      element = list_find (&liste, NULL, &d, &prec);
      if (element == NULL)
      {
        fprintf (stdout, "\nNot found");
      }
      else
      {
        fprintf (stdout, "\nFound");
      }
   }

   /* -------------------------------------------------------- */
   /* Remove element after "4"                                 */
   /* -------------------------------------------------------- */

   d.size = 1;
   sprintf (buff, "4");
   d.s = buff;

   element = list_find (&liste, NULL, &d, &prec);
   if (element != NULL)
   {
     fprintf (stdout, "\nRemoving element next to the element that contains 4");
     if (list_rem_next (&liste, element, (void**)&data) == 0)
     {
       fprintf (stdout, "\nElement removed [%s]", data->s);

       /* ---------------------------------------------------- */
       /* Don't forget to free the data's memory !!!           */
       /* ---------------------------------------------------- */

       free_data (data);
     }
     else
     {
       fprintf (stdout, "\nNo element after the one that contains \"4\"");
     }
   }

   /* -------------------------------------------------------- */
   /* Free all allocated memory and return                     */
   /* -------------------------------------------------------- */

   list_destroy(&liste);

   return 0;
}


Generated on Thu Apr 3 16:23:41 2003 for Common_C_libraries by doxygen1.3-rc1