Examples  of Z algorithms

 

1. Creates a list using a macro operation, then traverses it.
2. Builds a block file of n items with a fill rate of 100%, then traverses it.
3. Builds a structure tree using a macro operation, then writes its elements.
4.Creates an m-ary search tree of order 3 of structures using a macro operation and then traverses it inorder.
5. Creates a list of two dynamic arrays using a macro operation, then prints the elements of this list.
6. Creates a binary search tree using a macro operation, then traverses it breadthwise (level by level) using a queue.
7. Fills an array of characters from data read one by one, performs the sorting, then prints the sorted array.
8. Reads a 2-dimensional array globally and then prints it in a single writing order.
9. Reads a complex structure globally and then prints it in a single writing order.

 

 

 


1. Creates a list using a macro operation, then traverses it.
 

LET
    L : LIST;
    P : POINTER TO : LIST;
    I : INTEGER;
BEGIN
    I := 8;
    CREATE_LIST( L, [ I, 2*I, 3*I, 4*I] ) ;
    WRITE('Parcours De La LISTE ');
    P := L;
    WHILE P <> NULL :
        WRITE ( CELL_VALUE(P) );
        P := NEXT (P)
    ENDWHILE;
END



2. Builds a block file of n items with a fill rate of 100%, then traverses it.
  

                                   

LET
   F : FILE OF ( INTEGER , ARRAY ( 5 ) OF STRINGS )
   HEADER ( INTEGER , INTEGER ) BUFFER B1 ;
  { fichier de blocs contenant le nombre d'articles et un tableau d'articles}
  { entête : nombre d'articles, nombre de blocs}
  Creer , Imprimer : ACTIONS ;
BEGIN
   CALL Creer ;
   CALL Imprimer ;
END

/***** Chargement de n articles avec un chargement ... 100% ****/
ACTION Creer ;
LET
   I , K , N , Nbblocs : INTEGERS ;
BEGIN
   OPEN ( F , 'f.pas' , 'N' ) ;
   I := 0 ;
   Nbblocs := 0 ;
   N := 500 ;
   ASS_HEADER ( F , 1 , N ) ;
   WH I < N :
      K := 0 ;
      WH ( K < 5 ) AND ( I < N )
          K := K + 1 ;
          I := I + 1 ;
          ASS_ELEMENT ( STRUCT ( B1 , 2 ) [ K ] , RANDSTRING (10) )
      EWH ;
      ASS_STRUCT ( B1 , 1 , K ) ;
      Nbblocs := Nbblocs + 1 ;
      WRITESEQ ( F , B1 ) ;
   EWH ;
   ASS_HEADER ( F , 2 , Nbblocs ) ;
   CLOSE ( F ) ;
END

/***** Impression des articles du fichier ****/
ACTION Imprimer ;
LET
   I , K : INTEGERS ;
BEGIN
   I := 0 ;
  OPEN ( F , 'f.pas' , 'A' ) ;
  WH NOT ENDFILE ( F )
      I := I + 1 ;
      WRITE ( 'B L O C nø ' , I ) ;
      READSEQ ( F , B1 ) ;
      FOR K := 1 , STRUCT ( B1 , 1 )
          WRITE ( ELEMENT ( STRUCT ( B1 , 2 ) [ K ] ) )
      EFOR
  EWH;
END



3. Builds a structure tree using a macro operation, then writes its elements.



 LET
    A : BST OF ( STRING , INTEGER ) ;
    V : ( STRING , INTEGER ) ;
BEGIN
    INIT_STRUCT ( V , [ 'xyz' , 1001 ] ) ;
    CREATE_BST ( A , [ [ 'x' , 23 ] , V , [ 'y' , 123 ] , [ 'z' , 23 ] ] ) ;
    WRITE ( NODE_VALUE ( A ) ) ;
    WRITE ( NODE_VALUE ( RC ( A ) ) ) ;
    WRITE ( NODE_VALUE ( RC ( RC ( A ) ) ) ) ;
END



4. Crée un arbre de recherche m-aire d'ordre 3 de structures à l'aide d'une macro-opération puis le parcours en inordre. 

 
                                                                               

 LET
    M : MST ( 3 ) OF ( STRING , INTEGER ) ;
    Inordre : ACTION ;
BEGIN
    CREATE_MST ( M , [ [ 'x' , 3 ] , [ 'a' , 3 ] , [ 'y' , 3 ] , [ 'z' , 3 ] ,
                                         [ 'h' , 3 ] , [ 'e' , 3 ] , [ 'd' , 3 ] ] ) ;
    CALL Inordre ( M ) ;
END


ACTION Inordre ( Q )
LET
    Q : MST ( 3 ) OF ( STRING , INTEGER ) ;
    I : INTEGER ;
BEGIN
    IF Q <> NULL
        FOR I := 1 , DEGREE ( Q )
           CALL Inordre ( CHILD ( Q , I ) ) ;
           WRITE ( NODE_VALUE_MST ( Q , I ) )
        ENDFOR ;
        CALL Inordre ( CHILD ( Q , DEGREE ( Q ) + 1 ) )
    ENDIF
END

5. Creates a list of two dynamic arrays using a macro operation, then prints the elements of this list. 

                         
 LET
     Lv : LIST OF ARRAYS ( 5 ) DYNAMIC OF INTEGERS ;
 LET
     V1 , V2 , V : ARRAYS ( 5 ) DYNAMIC OF INTEGERS ;
     I : INTEGER ;
BEGIN
    ALLOC_ARRAY ( V ) ;
    V1 := V ;
    INIT_VECT ( V1 , [ 2 , 4 , 8 , 9 , 56 ] ) ;
    ALLOC_ARRAY ( V ) ;
    V2 := V ;
    INIT_VECT ( V2 , [ 12 , 14 , 18 , 19 , 156 ] ) ;
    CREATE_LIST ( Lv , [ V1 , V2 ] ) ;
    WH Lv <> NULL :
         V := CELL_VALUE ( Lv ) ;
         FOR I := 1 , 4 :
             WRITE ( ELEMENT ( V [ I ] ) )
         EFOR ;
         Lv := NEXT ( Lv ) ;
    EWH
END


6. Creates a binary search tree using a macro operation, then traverses it breadthwise (level by level) using a queue.  

 LET
     F : QUEUE OF BST ;
     A : BST ;
     M : POINTER TO BST ;
BEGIN
     CREATE_BST( A , [ 23 , 45 , 65 , 55 , 33 , 3 , 56 , 76 , 231 , 5 , 45 , 56 ]);
     CREATEQUEUE ( F ) ;
     M := A ;
     ENQUEUE ( F , A ) ;
     WH NOT EMPTY_QUEUE ( F )
         DEQUEUE ( F , M ) ;
         WRITE ( NODE_VALUE ( M ) ) ;
         IF LC ( M ) # NULL 
             ENQUEUE ( F , LC ( M ) )
         ENDIF ;
         IF RC ( M ) # NULL :
            ENQUEUE ( F , RC ( M ) )
         ENDIF
     EWH
END



7. Fills an array of characters from data read one by one, performs the sorting, then prints the sorted array.


 LET
     V : ARRAY ( 10 ) OF CHAR ;
     I , J : INTEGERS ;
     Val , Temp : CHAR ;
BEGIN
     FOR I := 1 , 10
         READ ( Val ) ;
         ASS_ELEMENT ( V [ I ] , Val )
     ENDFOR ;
     FOR I := 1 , 9
          FOR J := 10 , I + 1 , - 1
              IF ELEMENT ( V [ J - 1 ] ) > ELEMENT ( V [ J ] )
                   Temp := ELEMENT ( V [ J - 1 ] ) ;
                   ASS_ELEMENT ( V [ J - 1 ] , ELEMENT ( V [ J ] ) ) ;
                   ASS_ELEMENT ( V [ J ] , Temp )
              ENDIF
          ENDFOR
      ENDFOR ;
      WRITE ( 'Tri Termin‚e' ) ;
      FOR I := 1 , 10
           WRITE ( ELEMENT ( V [ I ] ) )
      ENDFOR ;
END


8. Reads a 2-dimensional array globally and then prints it in a single writing order.


 LET
      V : ARRAY ( 3 , 2 ) OF STRINGS ;
 BEGIN
      READ ( V ) ;
      WRITE ( V ) ;
  END

 

9. Reads a complex structure globally and then prints it in a single writing order.

  LET
      S2 : (
                ARRAY ( 5 ) OF STRINGS ,
                ARRAY ( 5 ) OF INTEGERS ,
                STRING
              ) ;
  BEGIN
      READ ( S2 ) ;
      WRITE ( S2 ) ;
  END