/*
    第2種情報処理技術者試験    平成9年度・春期・午後 問11
                                BohYoh Shibata PREPARATION
*/

#include  <stdio.h>
#include  <string.h>
#define   DMAX   128
struct  DIRLST { int dtype; struct DIRLST *eptr1, *eptr2, *eptr3; char name[9]; };
struct  DIRLST  *recnam( char* );
struct  DIRLST  *recren( char*, char* );
struct  DIRLST  *recgen( int, char*, char* );
static  struct DIRLST lsttbl[DMAX];

struct DIRLST *recnam( char *name )
{
    int i;

    for( i = 0; i < DMAX; i++ )
        if ( lsttbl[i].dtype != 0 )
            if ( strcmp( lsttbl[i].name, name ) == 0 ) return &lsttbl[i];
    return NULL;
}

struct DIRLST *recren( char *name1, char *name2 )
{
    struct  DIRLST *cp;

    cp = recnam( name1 );
    if ( cp == NULL ) return NULL;
    if ( recnam( name2 ) != NULL ) return NULL;
    strcpy( cp->name, name2 );
    return cp;
}

struct DIRLST *recgen( int dtype, char *name1, char *name2 )
{
    struct  DIRLST *cp;
    int     i;

    cp = recnam( name2 );
    if ( cp == NULL ) return NULL;
    if ( ( cp->dtype == 2 ) || ( recnam(name1) != NULL )) return NULL;
    for ( i = 0; i < DMAX; i++ ) {
        if ( lsttbl[i].dtype == 0 ) {
            lsttbl[i].dtype = dtype;
            lsttbl[i].eptr1 = cp;
            lsttbl[i].eptr2 = lsttbl[i].eptr3 = NULL;
            strcpy( lsttbl[i].name, name1 );
            if ( cp->eptr2 == NULL ) cp->eptr2 = &lsttbl[i];
            else {
                cp = cp->eptr2;
                while( cp->eptr3 != NULL ) cp = cp->eptr3;
                cp->eptr3 = &lsttbl[i];
            }
            return cp;
        }
    }
    return NULL;
}