home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l210 / 1.ddi / EXAMPLES.ARC / CH23EX08.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-21  |  1.6 KB  |  68 lines

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