• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

istack.c

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 //
00003 // File:           istack.c
00004 //
00005 // Created:        06/06/2001
00006 //
00007 // Author:         Pavel Sakov
00008 //                 CSIRO Marine Research
00009 //
00010 // Purpose:        Handling stack of integers
00011 //
00012 // Description:    None
00013 //
00014 // Revisions:      None
00015 //
00016 //--------------------------------------------------------------------------
00017 
00018 #define STACK_NSTART    50
00019 #define STACK_NINC      50
00020 
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include "istack.h"
00024 
00025 static void istack_init( istack* s )
00026 {
00027     s->n          = 0;
00028     s->nallocated = STACK_NSTART;
00029     s->v          = malloc( STACK_NSTART * sizeof ( int ) );
00030 }
00031 
00032 istack* istack_create()
00033 {
00034     istack* s = malloc( sizeof ( istack ) );
00035 
00036     istack_init( s );
00037     return s;
00038 }
00039 
00040 void istack_reset( istack* s )
00041 {
00042     s->n = 0;
00043 }
00044 
00045 int istack_contains( istack* s, int v )
00046 {
00047     int i;
00048 
00049     for ( i = 0; i < s->n; ++i )
00050         if ( s->v[i] == v )
00051             return 1;
00052     return 0;
00053 }
00054 
00055 void istack_push( istack* s, int v )
00056 {
00057     if ( s->n == s->nallocated )
00058     {
00059         s->v           = realloc( s->v, ( s->nallocated + STACK_NINC ) * sizeof ( int ) );
00060         s->nallocated += STACK_NINC;
00061     }
00062 
00063     s->v[s->n] = v;
00064     s->n++;
00065 }
00066 
00067 int istack_pop( istack* s )
00068 {
00069     s->n--;
00070     return s->v[s->n];
00071 }
00072 
00073 void istack_destroy( istack* s )
00074 {
00075     if ( s != NULL )
00076     {
00077         free( s->v );
00078         free( s );
00079     }
00080 }

Generated on Wed Oct 12 2011 20:42:21 for PLplot by  doxygen 1.7.1