/* TestSGAryTO2Ary.c */

#include <stdio.h>
#define NN 100
int SGAryTO2Ary(int*,int*,int*);
void Ary2TOSGAry(int*, int*, int*, int);

void main(void)
{
 int C[]={-1,2, -2,3,5, -3,5, -4,5, -6,0, -7,0, 0,0}, D[NN],
   A[NN],B[NN],
   I,N;

 N= SGAryTO2Ary(C,A,B);

 printf("N=%d\n",N);
 for (I=0; I<N; I++) printf("%d ",A[I]); puts("");
 for (I=0; I<N; I++) printf("%d ",B[I]); puts("");

 Ary2TOSGAry(A,B,D,N);

 N=D[0];
 for (I=0; I<=N; I++) if (D[I]<0) printf(" %d,",D[I]); else printf("%d,",D[I]);

 getchar();
}

/***********************************************************/

/* subprogram for converting Short Graph-array to 2 Array Representation */
int SGAryTO2Ary(int*P,int*A,int*B)
{
 int I,K=0;

LP1:
 if (!*P) {A[K]=B[K]=0; return K;}
 /* *P<0 */
 I=-*P;  P++;
LP2:
 A[K]=I; B[K]=*P; P++; K++;
 if (*P>0) goto LP2;
 goto LP1;  /* *P<=0 */
}

/********************************************************/

/* subprogram for converting 2 Array representation to Short Graph-array */


void Ary2TOSGAry(int *P, int *Q, int *R, int N)
 {
  int I,K,X,*RSAV;

 RSAV=R;
 for (I=0; I<NN; I++) {*R=0; R++;} R=RSAV;

 P[N]=Q[N]=K=0;

 X=*P; R++;
 while (X)
  {
   *R=-X; R++; K++;
   while (X==*P) {*R=*Q; K++; R++; Q++; P++;}
   X=*P;
  }
                                           
 R++; *R=0;  *RSAV=K;
 return;
}

