Storage structure generalized list: innate natural data structure - Wu Yuxiong

Generalized table implementation using sequential table structure, not only the n-dimensional array operation (e.g., { 1 , { 2 , { 3 , 4 }}} requires the use of three-dimensional array storage), but also result in waste of storage space.
Using generalized table stored list, first need to determine the list structure nodes. Since generalized table can store two types of sub-atomic and table data, so the list node structure also has two

 

 

Represents atoms node consists of two parts, namely, the value tag and the flag atoms, represents a node sub-table consists of three parts, namely, flag tag, HP pointer and pointer tp.
flag tag is used to distinguish this node atom or a sub-table, tag atoms is usually 0 , tag is a sub-table 1 . Hp node sub-table pointer for linking atom or sub-book table stored in the table, tp generalized table pointer for the next connection atom or a sub-table.
typedef struct GLNode {
     int Tag; // Flag field 
    Union {
         char Atom; // atoms nodes range 
        struct {
             struct GLNode * HP, * TP;
        PTR}; // child node pointer field table, hp point header; footer point TP 
    };
}*Glist;
Generalized table {a, {b, c, d}} is constituted by one atom and a sub-table {b, c, d}, and the sub-table {b, c, d} made up of atoms b, c and d constitute, a linked list stores the generalized table

 

 

CreatGlist GList (GList C) {
     // generalized table C 
    C = (GList) the malloc ( the sizeof (GList));
    C -> = Tag . 1 ;
     // Header atoms 'A' 
    the C-> ptr.hp = (the Glist) the malloc ( the sizeof (the Glist));
    C->ptr.hp->tag=0;
    C -> ptr.hp-> Atom = ' A ' ;
     // table Oko table (b, c, d), a whole is 
    the C-> ptr.tp = (the Glist) the malloc ( the sizeof (the Glist));
    C->ptr.tp->tag=1;
    C->ptr.tp->ptr.hp=(Glist)malloc(sizeof(Glist));
    C -> ptr.tp-> ptr.tp = NULL;
     // next start storing a data element (b, c, d), the header is 'b', the end of the table (C, D) 
    the C-> PTR. the tp-> ptr.hp-> = Tag . 1 ;
    C->ptr.tp->ptr.hp->ptr.hp=(Glist)malloc(sizeof(Glist));
    C->ptr.tp->ptr.hp->ptr.hp->tag=0;
    C->ptr.tp->ptr.hp->ptr.hp->atom='b';
    C -> ptr.tp-> ptr.hp-> ptr.tp = (the Glist) the malloc ( the sizeof (the Glist));
     // store sub-tables (c, d), the header is c, the end of the table D 
    the C- > ptr.tp-> ptr.hp-> ptr.tp-> = Tag . 1 ;
    C->ptr.tp->ptr.hp->ptr.tp->ptr.hp=(Glist)malloc(sizeof(Glist));
    C->ptr.tp->ptr.hp->ptr.tp->ptr.hp->tag=0;
    C->ptr.tp->ptr.hp->ptr.tp->ptr.hp->atom='c';
    C->ptr.tp->ptr.hp->ptr.tp->ptr.tp=(Glist)malloc(sizeof(Glist));
    //存放表尾d
    C->ptr.tp->ptr.hp->ptr.tp->ptr.tp->tag=1;
    C->ptr.tp->ptr.hp->ptr.tp->ptr.tp->ptr.hp=(Glist)malloc(sizeof(Glist));
    C->ptr.tp->ptr.hp->ptr.tp->ptr.tp->ptr.hp->tag=0;
    C->ptr.tp->ptr.hp->ptr.tp->ptr.tp->ptr.hp->atom='d';
    C->ptr.tp->ptr.hp->ptr.tp->ptr.tp->ptr.tp=NULL;
    return C;
}
Another generalized table storage structure

 

 

It represents a node consists atoms flag tag, and the value of atoms constituting the pointer tp, expressed by a node sub-table or the flag tag, HP pointer and pointer tp configuration.
typedef struct GLNode {
     int Tag; // Flag field 
    Union {
         int Atom; // atoms nodes range 
        struct GLNode * HP; // Pointer field of the subtable node, hp header points 
    };
     struct GLNode TP *; // here tp corresponding to the next list pointer, for pointing to the next data element 
} * Glist;

Glist creatGlist(Glist C){
    C=(Glist)malloc(sizeof(Glist));
    C->tag=1;
    C->hp=(Glist)malloc(sizeof(Glist));
    C -> TP = NULL;
     // Header atoms A 
    the C-> HP-> Tag = 0 ;
    C->atom='a';
    C->hp->tp=(Glist)malloc(sizeof(Glist));
    C->hp->tp->tag=1;
    C->hp->tp->hp=(Glist)malloc(sizeof(Glist));
    C->hp->tp->tp=NULL;
    //原子b
    C->hp->tp->hp->tag=0;
    C->hp->tp->hp->atom='b';
    C->hp->tp->hp->tp=(Glist)malloc(sizeof(Glist));
    //原子c
    C->hp->tp->hp->tp->tag=0;
    C->hp->tp->hp->tp->atom='c';
    C->hp->tp->hp->tp->tp=(Glist)malloc(sizeof(Glist));
    //原子d
    C->hp->tp->hp->tp->tp->tag=0;
    C->hp->tp->hp->tp->tp->atom='d';
    C->hp->tp->hp->tp->tp->tp=NULL;
    return C;
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/12232469.html