Implementation of Z machines in C
Bidrectional linked lists
Queues
Binary search trees
M-aray search trees
LET Tab : ARRAY ( 5 , 10 ) ;
/** -Implementation-
**\: ARRAY OF INTEGERS**/
/** Arrays **/
typedef int Typeelem_V5_10i ;
typedef Typeelem_V5_10i * Typevect_V5_10i ;
Typeelem_V5_10i Element_V5_10i ( Typevect_V5_10i V , int I1 , int I2 )
{
return *(V + (I1-1)*10 + (I2-1) ) ;
}
void Ass_element_V5_10i ( Typevect_V5_10i V , int I1 , int I2,
Typeelem_V5_10i Val )
{
*(V + (I1-1)*10 + (I2-1) ) = Val ;
}
/**
Variables of main program **/
Typevect_V5_10i Tab;
/** Main program **/
int main(int argc, char *argv[])
{
Tab = malloc(5*10 * sizeof(int));
}
LET S : STRUCTURE ( STRING , INTEGER ) ;
typedef char * string256 ;
/** Structures **/
typedef struct Tsi
Type_Tsi ;
typedef Type_Tsi * Typestr_Tsi ;
typedef string255 Type1_Tsi ;
typedef int Type2_Tsi ;
struct Tsi
{
Type1_Tsi Field1 ;
Type2_Tsi Field2 ;
};
Type1_Tsi Struct1_Tsi ( Typestr_Tsi S)
{
return S->Field1 ;
}
Type2_Tsi Struct2_Tsi ( Typestr_Tsi S)
{
return S->Field2 ;
}
void Ass_struct1_Tsi ( Typestr_Tsi S, Type1_Tsi Val )
{
strcpy( S->Field1 , Val );
}
void Ass_struct2_Tsi ( Typestr_Tsi S, Type2_Tsi Val )
{
S->Field2 = Val ;
}
/** Variables of main
program **/
Typevect_V5_10i Tab;
Typestr_Tsi S;
/** Main program **/
int main(int argc, char *argv[])
{
S = malloc(sizeof(Typestr_Tsi));
S->Field1 = malloc(sizeof(string256));
}
Linked list implementation in C
LET L : LIST ;
/** -Implementation-
**\: LIST Of INTEGERS**/
/** Linked lists **/
typedef int
Typeelem_Li ;
typedef struct Maillon_Li * Pointer_Li ;
struct Maillon_Li
{
Typeelem_Li Val ;
Pointer_Li Next ;
} ;
Pointer_Li Allocate_cell_Li (Pointer_Li *P)
{
*P = (struct Maillon_Li *) malloc( sizeof( struct Maillon_Li)) ;
(*P)->Next = NULL;
}
void Ass_val_Li(Pointer_Li P, Typeelem_Li Val)
{
P->Val = Val ;
}
void Ass_adr_Li( Pointer_Li P, Pointer_Li Q)
{
P->Next = Q ;
}
Pointer_Li Next_Li( Pointer_Li P)
{ return( P->Next ) ; }
Typeelem_Li Cell_value_Li( Pointer_Li P)
{ return( P->Val) ; }
void Free_Li ( Pointer_Li P)
{ free (P);}
/** Variables of main program **/
Pointeur_Li L=NULL;
/** Main program **/
int main(int argc, char *argv[])
{
}
Bidrectional linked list implementation in C
LET L : BILIST ;
/**
-Implementation- **\: BIDIRECTIONAL LIST OF INTEGERS**/
/** Bidirectional linked lists **/
typedef int Typeelem_Ri ;
typedef struct Maillon_Ri * Pointer_Ri ;
struct Maillon_Ri
{
Typeelem_Ri Val ;
Pointer_Ri Next ;
Pointer_Ri Prev ;
} ;
Pointer_Ri Allocate_cell_Ri (Pointer_Ri *P)
{
*P = (struct Maillon_Ri *) malloc( sizeof( struct Maillon_Ri)) ;
(*P)->Next = NULL;
(*P)->Prev = NULL;
}
void Ass_val_Ri(Pointer_Ri P, Typeelem_Ri Val)
{
P->Val = Val ;
}
void Ass_r_adr_Ri( Pointer_Ri P, Pointer_Ri Q)
{ P->Next = Q; }
void Ass_l_adr_Ri( Pointer_Ri P, Pointer_Ri Q)
{ P->Prev = Q; }
Pointer_Ri Next_Ri( Pointer_Ri P)
{ return( P->Next ); }
Pointer_Ri Previous_Ri( Pointer_Ri P)
{ return( P->Prev ); }
Typeelem_Ri Cell_value_Ri( Pointer_Ri P)
{ return( P->Val) ; }
void Free_Ri ( Pointer_Ri P)
{ free (P) ; }
/** Variables of main program **/
Pointeur_Ri L=NULL;
/** Main program **/
int main(int argc, char *argv[])
{
}
Binary search tree implementation in C
LET A : BST ;
/** -Implementation-
**\: BINARY SERACH TREE OF INTEGERS**/
/** Binary search trees **/
typedef int Typeelem_Ai ;
typedef struct Noeud_Ai * Pointer_Ai ;
struct Noeud_Ai
{
Typeelem_Ai Val ;
Pointer_Ai Lc ;
Pointer_Ai Rc ;
Pointer_Ai Parent ;
} ;
Typeelem_Ai Node_value_Ai( Pointer_Ai P )
{ return P->Val; }
Pointer_Ai Lc_Ai( Pointer_Ai P)
{ return P->Lc ; }
Pointer_Ai Rc_Ai( Pointer_Ai P)
{ return P->Rc ; }
Pointer_Ai Parent_Ai( Pointer_Ai P)
{ return P->Parent ; }
void Ass_node_val_Ai ( Pointer_Ai P, Typeelem_Ai Val)
{
P->Val = Val ;
}
void Ass_lc_Ai( Pointer_Ai P, Pointer_Ai Q)
{ P->Lc = Q; }
void Ass_rc_Ai( Pointer_Ai P, Pointer_Ai Q)
{ P->Rc = Q ; }
void Ass_parent_Ai( Pointer_Ai P, Pointer_Ai Q)
{ P->Parent = Q ; }
void Allocate_node_Ai( Pointer_Ai *P)
{
*P = (struct Noeud_Ai *) malloc( sizeof( struct Noeud_Ai)) ;
(*P)->Lc = NULL;
(*P)->Rc = NULL;
(*P)->Parent = NULL;
}
void Free_node_Ai( Pointer_Ai P)
{ free( P ) ; }
/** Variables of main program **/
Pointeur_Ai A=NULL;
/** Main program **/
int main(int argc, char *argv[])
{
}
M-ary search tree implémentation in C
LET M : MST ( 4 ) ;
/** -Implementation-
**\: M-ARY SEARCH TREE OF INTEGERS**/
/** M-ary search trees **/
typedef int Typeelem_M4i ;
typedef struct Noeud_M4i * Pointer_M4i ;
struct Noeud_M4i
{
int Degree ;
Pointer_M4i Child[4] ;
Typeelem_M4i Infor[4] ;
Pointer_M4i Parent ;
} ;
Typeelem_M4i Node_value_mst_M4i(Pointer_M4i P, int I)
{ return P->Infor[I-1] ; }
Pointer_M4i Child_M4i( Pointer_M4i P, int I)
{ return P->Child[I-1] ; }
Pointer_M4i Parent_M4i( Pointer_M4i P)
{ return P->Parent ; }
void Ass_node_val_mst_M4i ( Pointer_M4i P, int I, Typeelem_M4i Val)
{
P->Infor[I-1] = Val ;
}
void Ass_child_M4i( Pointer_M4i P, int I, Pointer_M4i Q)
{ P->Child[I-1] = Q ; }
void Aff_parent_M4i( Pointer_M4i P, Pointer_M4i Q)
{ P->Parent = Q ; }
void Allocate_node_M4i( Pointer_M4i *P )
{
int I ;
*P = (struct Noeud_M4i *) malloc( sizeof( struct Noeud_M4i)) ;
for (I=0; I< 4; ++I) (*P)->Child[I] = NULL;
(*P)->Degree = 0 ;
}
int Degree_M4i ( Pointer_M4i P )
{ return P->Degree ; }
void Ass_degree_M4i ( Pointer_M4i P, int N)
{ P->Degree = N ; }
void Free_node_M4i(Pointer_M4i P)
{ free ( P );}
/** Variables of main program **/
Pointeur_M4i M=NULL;
/** Main program **/
int main(int argc, char *argv[])
{
}
LET P : STACK ;
/** -Implementation-
**\: STACK OF INTEGERS**/
/** Stacks**/
typedef int Typeelem_Pi ;
typedef struct Maillon_Pi * Pointer_Pi ;
typedef Pointer_Pi Typepile_Pi ;
struct Maillon_Pi
{
Typeelem_Pi Val ;
Pointer_Pi Next ;
} ;
void Createstack_Pi( Pointer_Pi *P )
{ *P = NULL ; }
bool Empty_stack_Pi ( Pointer_Pi P )
{ return (P == NULL ); }
void Push_Pi ( Pointer_Pi *P, Typeelem_Pi Val )
{
Pointer_Pi Q;
Q = (struct Maillon_Pi *) malloc( sizeof( struct Maillon_Pi)) ;
Q->Val = Val ;
Q->Next = *P;
*P = Q;
}
void Pop_Pi ( Pointer_Pi *P, Typeelem_Pi *Val )
{
Pointer_Pi Save;
if (! Empty_stack_Pi (*P) )
{
*Val = (*P)->Val ;
Save = *P;
*P = (*P)->Next;
free(Save);
}
else printf ("%s \n", "Stack is empty");
}
/** Variables of main program **/
Pointeur_Pi P=NULL;
/** Main program **/
int main(int argc, char *argv[])
{
}
LET F : QUEUE ;
/** -Implementation-
**\: QUEUE OF INTEGERS**/
/** Queues **/
typedef int Typeelem_Fi ;
typedef struct Filedattente_Fi * Pointer_Fi ;
typedef struct Maillon_Fi * Ptliste_Fi ;
struct Maillon_Fi
{
Typeelem_Fi Val ;
Ptliste_Fi Next ;
};
struct Filedattente_Fi
{
Ptliste_Fi Head, Queue ;
};
void Createqueue_Fi ( Pointer_Fi *Fil )
{
*Fil = (struct Filedattente_Fi *) malloc( sizeof( struct Filedattente_Fi)) ;
(*Fil)->Head = NULL ;
(*Fil)->Queue = NULL ;
}
bool Empty_queue_Fi (Pointer_Fi Fil )
{ return Fil->Head == NULL ;}
void Enqueue_Fi ( Pointer_Fi Fil , Typeelem_Fi Val )
{
Ptliste_Fi Q;
Q = (struct Maillon_Fi *) malloc( sizeof( struct Maillon_Fi)) ;
Q->Val = Val ;
Q->Next = NULL;
if ( ! Empty_queue_Fi(Fil) )
Fil->Queue->Next = Q ;
else Fil->Head = Q;
Fil->Queue = Q;
}
void Dequeue_Fi (Pointer_Fi Fil, Typeelem_Fi *Val )
{
if (! Empty_queue_Fi(Fil) )
{
*Val = Fil->Head->Val ;
Fil->Head = Fil->Head->Next;
}
else printf ("%s \n", "Queue is empty");
}
/** Variables of main program **/
Pointeur_Fi F=NULL;
/** Main program **/
int main(int argc, char *argv[])
{
}
LET F : FILE OF ( STRINGS , INTEGER ) HEADER ( INTEGER , INTEGER ) BUFFER B1 ;
/** -Implementation- **\: FILE
**/
/* Managing open files */
struct _Node
{
FILE * Var_file ;
char * Name_file ;
int Save_pos;
struct _Node *Suiv ;
} ;
typedef struct _Node * _Ptr_Node;
_Ptr_Node _Stack_Opens = NULL;
/* Test if a file is open */
_Ptr_Node _Open ( char * Fp)
{
_Ptr_Node P;
bool Trouv ;
P = _Stack_Opens; Trouv = False ;
while ((P != NULL) && ! Trouv )
if ( strcmp(P->Name_file, Fp) == 0)
Trouv = True;
else P = P->Suiv;
return P;
}
/* Add an open file */
void _Push_Open ( char *Fp, FILE *Fl)
{
_Ptr_Node P ;
P = (_Ptr_Node) malloc( sizeof( struct _Node)) ;
P->Name_file = Fp;
P->Var_file = Fl;
P->Suiv = _Stack_Opens;
_Stack_Opens = P;
}
/* Delete an open file and return its name */
char * _Pop_Open ( FILE *Fl)
{
char * Fp = malloc (100);
_Ptr_Node P, Prec ;
P= _Stack_Opens;
Prec = NULL;
while (P->Var_file != Fl )
{ Prec = P ; P = P->Suiv ;}
strcpy(Fp, P->Name_file);
if (Prec != NULL)
Prec->Suiv = P->Suiv;
else _Stack_Opens = P->Suiv;
free (P);
return Fp ;
}
/** Files **/
typedef char _Tx[255];
/** Types of block fields **/
typedef string255 Typefield1_siEii;
typedef _Tx Typefield1_siEii_Buf ;
typedef int Typefield2_siEii;
/** Types of header fields **/
typedef int Typeentete1_siEii ;
typedef int Typeentete2_siEii ;
/** Type of file data block **/
typedef struct
{
Typefield1_siEii_Buf Field1 ;
Typefield2_siEii Field2 ;
} Typestruct1_siEii_Buf;
/** Type of File data block structure **/
typedef struct
{
Typefield1_siEii Field1 ;
Typefield2_siEii Field2 ;
} Typestruct1_siEii_;
typedef Typestruct1_siEii_ * Typestruct1_siEii ;
/** Type of block of file features **/
typedef struct
{
Typeentete1_siEii Entete1 ;
Typeentete2_siEii Entete2 ;
} Typestruct2_siEii ;
/** Declaration of header block **/
Typestruct2_siEii Bloc_caract_siEii;
/** Operations on files **/
void Open_siEii ( FILE **siEii , char *Fp , char * Mode )
{
_Ptr_Node P = _Open(Fp);
if ( P != NULL )
/* The file is already open */
{
P->Save_pos = ftell (P->Var_file);
fseek( P->Var_file, 0, 0);
fwrite(&Bloc_caract_siEii, sizeof(Typestruct2_siEii), 1, P->Var_file);
fclose(P->Var_file);
}
/* The file is not open */
if ( strcmp(Mode,"A") == 0)
{
*siEii = fopen(Fp, "r+b");
fread(&Bloc_caract_siEii, sizeof(Typestruct2_siEii), 1, *siEii);
}
else
{
*siEii = fopen(Fp, "w+b");
fwrite(&Bloc_caract_siEii, sizeof(Typestruct2_siEii), 1, *siEii) ;
}
_Push_Open( Fp, *siEii);
}
void Close_siEii ( FILE * siEii )
{
char * Fp = malloc(100);
_Ptr_Node P ;
strcpy(Fp, _Pop_Open(siEii));
fseek( siEii, 0, 0);
fwrite(&Bloc_caract_siEii, sizeof(Typestruct2_siEii), 1, siEii);
fclose(siEii) ;
/* Is there a file open with the same name? */
/* If yes, open it again at the saved position */
P = _Open (Fp);
if ( P != NULL)
{
siEii = fopen(P->Name_file, "r+b");
fread(&Bloc_caract_siEii, sizeof(Typestruct2_siEii), 1, siEii);
fseek(siEii, P->Save_pos, 0);
}
}
Typeentete1_siEii Entete1_siEii( FILE * siEii)
{
return Bloc_caract_siEii.Entete1;
}
Typeentete2_siEii Entete2_siEii( FILE * siEii)
{
return Bloc_caract_siEii.Entete2;
}
void Aff_entete1_siEii ( FILE * siEii, Typeentete1_siEii VAL)
{
Bloc_caract_siEii.Entete1 = VAL ;
}
void Aff_entete2_siEii ( FILE * siEii, Typeentete2_siEii VAL)
{
Bloc_caract_siEii.Entete2 = VAL ;
}
void Writeseq_siEii ( FILE * siEii, Typestruct1_siEii Buf )
{
Typestruct1_siEii_Buf Buffer ;
int I, J;
for(J=0; J<= strlen(Buf->Field1); ++J)
Buffer.Field1[J] = Buf->Field1[J];
Buffer.Field2 = Buf->Field2;
fwrite(&Buffer, sizeof( Typestruct1_siEii_Buf), 1, siEii) ;
}
void Writedir_siEii ( FILE * siEii, Typestruct1_siEii Buf, int N )
{
Typestruct1_siEii_Buf Buffer ;
int I, J;
for(J=0; J<= strlen(Buf->Field1); ++J)
Buffer.Field1[J] = Buf->Field1[J];
Buffer.Field2 = Buf->Field2;
fseek(siEii, (long) ((N-1)* sizeof( Typestruct1_siEii_Buf) +
sizeof( Typestruct2_siEii)), 0 );
fwrite(&Buffer, sizeof( Typestruct1_siEii_Buf), 1, siEii) ;
}
void Readseq_siEii ( FILE * siEii, Typestruct1_siEii Buf )
{
Typestruct1_siEii_Buf Buffer ;
int I, J;
if (fread(&Buffer, sizeof( Typestruct1_siEii_Buf), 1, siEii)!=0){
for(J=0; J<= strlen(Buffer.Field1); ++J)
Buf->Field1[J] = Buffer.Field1[J];
Buf->Field2= Buffer.Field2;
}
}
void Readdir_siEii ( FILE * siEii, Typestruct1_siEii Buf, int N)
{
Typestruct1_siEii_Buf Buffer ;
int I, J;
fseek(siEii, (long) ((N-1)* sizeof( Typestruct1_siEii_Buf) +
sizeof( Typestruct2_siEii)), 0 );
fread(&Buffer, sizeof( Typestruct1_siEii_Buf), 1, siEii);
for(J=0; J<= strlen(Buffer.Field1); ++J)
Buf->Field1[J] = Buffer.Field1[J];
Buf->Field2= Buffer.Field2;
}
void Add_siEii ( FILE * siEii, Typestruct1_siEii Buf )
{
Typestruct1_siEii_Buf Buffer ;
int I, J;
for(J=0; J<= strlen(Buf->Field1); ++J)
Buffer.Field1[J] = Buf->Field1[J];
Buffer.Field2 = Buf->Field2;
fseek(siEii, 0, 2); /* End of file */
fwrite(&Buffer, sizeof( Typestruct1_siEii_Buf), 1, siEii) ;
}
bool Endfile_siEii (FILE * siEii)
{
long K = ftell(siEii);
fseek(siEii, 0, 2); /* End of file */
long K2 = ftell(siEii); /* Position from beginning */
if (K==K2)
{ fseek(siEii, K, 0); return 1;}
else
{ fseek(siEii, K, 0); return 0;}
}
int Alloc_block_siEii (FILE * siEii)
{
long K;
fseek(siEii, 0, 2); /* End of file */
K = ftell(siEii); /* Position from beginning */
K = K - sizeof( Typestruct2_siEii); /* Ignore the header */
K = K / sizeof (Typestruct1_siEii_Buf);
K ++;
return(K);
}
/** Variables of main program **/
FILE *F;
Typestruct1_siEii B1 ;
/** Main program **/
int main(int argc, char *argv[])
{
B1 = malloc(sizeof(Typestruct1_siEii));
B1->Field1 = malloc(255 * sizeof(string255));
}