home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l217 / 2.ddi / EXAMPLES / CH24EX08.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-26  |  1.5 KB  |  65 lines

  1. /*
  2.    Copyright (c) 1986, 90 by Prolog Development Center
  3. */
  4.    
  5. #define    alloc_gstack walloc
  6. #define    listfno  1
  7. #define    nilfno   2
  8.  
  9. void *alloc_gstack(unsigned);
  10.  
  11. typedef struct ilist {
  12.    char Functor;
  13.    int Value;
  14.    struct ilist *Next;
  15. } IntList;
  16.  
  17. int ListToArray(IntList *List,int **ResultArray)
  18. {   /* Convert a list to an array placed on the global stack */
  19.    IntList *SaveList = List;
  20.    int *Array;
  21.    int  i = 0;
  22.   
  23.  /* Count the number of elements in the list */
  24.    for(i=0; List->Functor==listfno; List=List->Next)
  25.       i++;
  26.  
  27.    Array = alloc_gstack(i*sizeof(int));  /* Allocate the needed
  28. memory */
  29.    List = SaveList; /* Transfer the elements from the list to the
  30. array */
  31.  
  32.    for(i=0; List->Functor==listfno; List=List->Next)
  33.       Array[i++]=List->Value;
  34.  
  35.    *ResultArray=Array;
  36.    return(i);
  37. }
  38.  
  39. ArrayToList(int Array[],int n,IntList **List) /* Convert an array
  40. to a list */
  41. {
  42.    int i;
  43.  
  44.    for (i=0; i<n; i++)  /* Allocate a record for each element */
  45.    {
  46.       IntList *p=*List=alloc_gstack(sizeof(IntList));
  47.       p->Functor=listfno;
  48.       p->Value=Array[i];
  49.       List=&(*List)->Next;
  50.    }
  51.  
  52.    {  /* Allocate the last record in the list */
  53.       IntList *p=*List=alloc_gstack(sizeof(char));
  54.       p->Functor=nilfno;
  55.    }
  56. }
  57.  
  58. inclist_0(IntList *InList,IntList **OutList)
  59. {  /* Increment all values in a list */
  60.    int i, n, *Array;
  61.    n=ListToArray(InList,&Array);
  62.    for(i=0; i<n; i++) Array[i]++;
  63.    ArrayToList(Array,n,OutList);
  64. }
  65.