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

test_stack.c

This test prpgram shows how to use the stack API.

#include <stdio.h>
#include "stack.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 main entry point                                  */
/* ----------------------------------------------------------- */

int main (int argc, char *argv[])
{
  Stack          my_stack;
  int            i, number_of_data, cr;
  struct sdata   *data;  
  char           buff[32];


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

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

  number_of_data = atoi(argv[1]);

  /* --------------------------------------------------------- */
  /* initialize the stack                                      */
  /* --------------------------------------------------------- */

  stack_init (&my_stack, free_data, compare_element);

  /* --------------------------------------------------------- */
  /* Put data on top of the stack                              */
  /* --------------------------------------------------------- */

  for (i=0; i<number_of_data; 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 stack                         */
     /* ------------------------------------------------------ */

     fprintf (stdout, "\nPutting [%s, %d] on top of the stack", data->s, data->size);

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

  /* --------------------------------------------------------- */
  /* Getting the number of elements in the stack               */
  /* --------------------------------------------------------- */

  fprintf (stdout, "\n\nThere are %d elements in the stack\n\n", stack_size(&my_stack));

  /* --------------------------------------------------------- */
  /* Peek element at the top of the stack                      */
  /* --------------------------------------------------------- */

  data = stack_peek(&my_stack);
  if (data != NULL)
  {
    fprintf (stdout, "\n\nTop element is [%s, %d]", data->s, data->size);
  }

  /* --------------------------------------------------------- */
  /* Now extract (and print) all elements in the stack         */
  /* --------------------------------------------------------- */

  while (stack_size(&my_stack))
  {
    /* ------------------------------------------------------- */
    /* Extract one element from the stack                      */
    /* ------------------------------------------------------- */

    cr = stack_pop (&my_stack, (void**)&data);
    if (cr == -1)
    {
       fprintf (stderr, "\nError: Trying to remove an element from an empty stack!\n");
       return 1;
    }

    /* ------------------------------------------------------- */
    /* Print the extracted element                             */
    /* ------------------------------------------------------- */

    fprintf (stdout, "\nExtracted element: [%s, %d]", data->s, data->size);

    /* ------------------------------------------------------- */
    /* Free the memory allocated for the element               */
    /* ------------------------------------------------------- */

    free_data ((void*)data);
  }

  /* --------------------------------------------------------- */
  /* Getting the number of elements in the stack               */
  /* --------------------------------------------------------- */

  fprintf (stdout, "\n\nThere are %d elements in the stack\n\n", stack_size(&my_stack));

  /* --------------------------------------------------------- */
  /* Free all memory allocated with the stack                  */
  /* --------------------------------------------------------- */

  stack_destroy(&my_stack);

  return 0;
}

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