stack.c

Go to the documentation of this file.
00001 /* Copyright (c) 2005 by John M. Boyer, All Rights Reserved.  Please see
00002  * License.txt for use and redistribution license. */
00003 /* Copyright (c) 1997 by John M. Boyer, All Rights Reserved.
00004         This code may not be reproduced or disseminated in whole or in part 
00005         without the written permission of the author. */
00006 
00007 #include "appconst.h"
00008 #include "stack.h"
00009 #include <stdlib.h>
00010 stackP sp_New (int Size)
00011 {
00012   stackP theStack;
00013   theStack = (stackP) malloc (sizeof (stack));
00014   if (theStack != NULL)
00015 
00016   {
00017     theStack->S = (int *) malloc (Size * sizeof (int));
00018     if (theStack->S == NULL)
00019 
00020     {
00021       free (theStack);
00022       theStack = NULL;
00023     }
00024   }
00025   if (theStack != NULL)
00026 
00027   {
00028     theStack->Size = Size;
00029     sp_ClearStack (theStack);
00030   }
00031   return theStack;
00032 }
00033 
00034 void sp_Free (stackP * pStack)
00035 {
00036   if (pStack == NULL || *pStack == NULL)
00037     return;
00038   (*pStack)->Size = (*pStack)->Top = 0;
00039   if ((*pStack)->S != NULL)
00040     free ((*pStack)->S);
00041   (*pStack)->S = NULL;
00042   free (*pStack);
00043   *pStack = NULL;
00044 }
00045 
00046 int sp_Copy (stackP stackDst,
00047              stackP stackSrc)
00048 {
00049   stackP newStack = NULL;
00050   int I,
00051    *p;
00052   if (stackDst->Size == stackSrc->Size)
00053 
00054   {
00055     for (I = 0; I < stackSrc->Top; I++)
00056       stackDst->S[I] = stackSrc->S[I];
00057   }
00058 
00059   else
00060 
00061   {
00062     newStack = sp_New (stackSrc->Size);
00063     if (newStack == NULL)
00064       return NOTOK;
00065     for (I = 0; I < stackSrc->Top; I++)
00066       newStack->S[I] = stackSrc->S[I];
00067     p = stackDst->S;
00068     stackDst->S = newStack->S;
00069     newStack->S = p;
00070     newStack->Size = stackDst->Size;
00071     sp_Free (&newStack);
00072   }
00073   stackDst->Top = stackSrc->Top;
00074   stackDst->Size = stackSrc->Size;
00075   return OK;
00076 }
00077 
00078 
00079 #ifndef SPEED_MACROS
00080 int sp_ClearStack (stackP theStack)
00081 {
00082   theStack->Top = 0;
00083   return OK;
00084 }
00085 
00086 int sp_IsEmpty (stackP theStack)
00087 {
00088   return !theStack->Top;
00089 }
00090 
00091 int sp_NonEmpty (stackP theStack)
00092 {
00093   return theStack->Top;
00094 }
00095 
00096 int sp_Push (stackP theStack,
00097              int a)
00098 {
00099 
00100 //     if (theStack->Top >= theStack->Size)
00101 //         return NOTOK;
00102   theStack->S[theStack->Top++] = a;
00103   return OK;
00104 }
00105 
00106 int sp_Push2 (stackP theStack,
00107               int a,
00108               int b)
00109 {
00110 
00111 //     if (theStack->Top + 1 >= theStack->Size)
00112 //         return NOTOK;
00113   theStack->S[theStack->Top++] = a;
00114   theStack->S[theStack->Top++] = b;
00115   return OK;
00116 }
00117 
00118 int sp__Pop (stackP theStack,
00119              int *pA)
00120 {
00121 
00122 //     if (theStack->Top <= 0)
00123 //         return NOTOK;
00124   *pA = theStack->S[--theStack->Top];
00125   return OK;
00126 }
00127 
00128 int sp__Pop2 (stackP theStack,
00129               int *pA,
00130               int *pB)
00131 {
00132 
00133 //     if (theStack->Top <= 1)
00134 //         return NOTOK;
00135   *pB = theStack->S[--theStack->Top];
00136   *pA = theStack->S[--theStack->Top];
00137   return OK;
00138 }
00139 
00140 
00141 #endif // SPEED_MACROS

Generated on Thu Oct 20 14:58:42 2005 for DominoParitySeparator by  doxygen 1.4.5