#include #include typedef struct _Node Node; typedef struct _Block Block; typedef struct _List List; struct _Node { // (could use an union to store the first 2 fields) char * name; // IF a "simple node": it has just a name, no block ptr Block * bptr; // OTHERWISE: has no name, but a pointer to a (named) block Node * next; }; struct _Block { char * name; // the name common to the opening and closing tags int level; // store the level in the tree; mostly for tracing purpose List * list; // the content of the block }; struct _List { Node * first; Node * last; }; List * newList( ) { List * list = malloc(sizeof(List)); list->first = list->last = 0; return list; } Node * newSimpleNode( char * name ) { Node *nptr = (Node *)malloc(sizeof(Node)); nptr->name = name; nptr->bptr = 0; // no block pointer nptr->next = 0; return nptr; } Node * newNodeWithABlock( Block * bptr ) { Node *nptr = (Node *)malloc(sizeof(Node)); nptr->bptr = bptr; nptr->name = 0; // no name nptr->next = 0; return nptr; } Block * newBlock( char * nm, List * lptr , int level ) { Block * bptr = (Block *) malloc(sizeof(Block)); bptr->name = nm; bptr->level = level; bptr->list = lptr; return bptr; } void appendNodeToList( Node * nptr, List * lptr ) { // If appended node is simple, it has a name; // otherwise it points at a block which has one char * s = nptr->name; if( s==0 ) s = nptr->bptr->name; if ( lptr->first == 0 ) { lptr->first = lptr->last = nptr; } else { lptr->last->next = nptr; lptr->last = nptr; } } void blockList( Block * b ) { Node * nd; printf("Bloc %s l=%d\n",b->name,b->level); fflush(stdout); nd = b->list->first; while( nd != 0 ) { if( nd->name != 0 ) { printf("%s ",nd->name); fflush(stdout); } else { blockList(nd->bptr); } nd = nd->next; } } /* // Exemple construisant un arbre simple // A // |_______B // |_______C // /_______D main( ) { Block * bp0, * bp1; List * lp0 = newList(); List * lp1 = newList(); appendNodeToList( newSimpleNode("C"), lp1 ); appendNodeToList( newSimpleNode("D"), lp1 ); bp1 = newBlock( "B", lp1, 1); appendNodeToList( newNodeWithABlock( bp1 ), lp0 ); bp0 = newBlock( "A", lp0, 0 ); printf("\n"); blockList( bp0 ); printf("\n"); } */