#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "stdlib.h"

#define YES 1
#define NO  0

#define LIST struct list
LIST {
        char *name;
        char *tel;
        LIST *next;
};

LIST *node(LIST *rp, char *nnam, char *ntel);
int inp(char *inam, char *itel);
LIST *nalloc();
char *strsv(char *sl);
void prn(LIST *pp);
LIST *srch(LIST *sp, char *snam);
LIST *modi(LIST *mp, char *mnam);
LIST *dele(LIST *dp, char *dnam);


int _tmain(int argc, _TCHAR* argv[])
{
        LIST *root, *node(), *srch(), *modi(), *dele();
        char name[15], tel[15];
        char cname[15];
        int key;
        root = NULL;
        do {
                printf("\n\n input-1  display-2 search-3  update-4  delete-5 end-6 \n");
                printf("\n key -->");
                scanf("%d", &key);
                if (key == 1) {
                        printf("\n -----------  input  -----------  \n");
                        while (inp(name, tel) == NO)
                                root = node(root, name, tel);
                }
                if (key == 2) {
                        printf("\n ----------  display -----------  \n");
                        printf("\nname                      tel \n\n");
                        prn(root);
                }
                if (key == 3) {
                        printf("\n ----------  search  -----------  \n");
                        printf(" name --> ");
                        scanf("%s",cname);
                        srch(root, cname);
                }
                if (key == 4) {
                        printf("\n ----------  update  -----------  \n");
                        printf(" name --> ");
                        scanf("%s",cname);
                        modi(root,cname);
                }
                if (key == 5) {
                        printf("\n ----------  delete  -----------  \n");
                        printf(" name --> ");
                        scanf("%s",cname);
                        dele(root,cname);
                }
                if (key == 6) {
                        printf("\n -----------   end   -----------  \n");
                }
        } while (key < 6);
}

LIST *node(LIST *rp, char *nnam, char *ntel)
{
        LIST *nalloc();
        int cmp;
        if (rp == NULL) {
                rp = nalloc();
                rp->name = strsv(nnam);
                rp->tel = strsv(ntel);
                rp->next = NULL;
        }
        else if ((cmp = strcmp(nnam, rp->name)) == 0) {
                printf("\n ------------------------------------------- \n");
                printf("    NAME = %-15s  TEL = %-13s \n", rp->name, rp->tel);
                printf("\n ------------------------------------------- \n");
        }
        else
                rp->next = node(rp->next, nnam, ntel);
        return(rp);
}

int inp(char *inam, char *itel)
{
        printf("\n -------------------------------- \n");
        printf("\n --- If name is q then exit.. --- \n");
        printf("\n -------------------------------- \n");
        printf("\n  name --> ");
        scanf("%s", inam);
        if (*inam == 'q' || *inam == 'Q')
                return(YES);
        printf("\n  tel  --> ");
        scanf("%s", itel);
        return(NO);
}

LIST *nalloc()
{
        return((LIST*) malloc(sizeof(LIST)));
}


char *strsv(char *sl)
{
        char *p;
        if ((p = (char*)malloc(strlen(sl) + 1)) != NULL)
                strcpy(p, sl);
        return(p);
}

void prn(LIST *pp)
{
        if (pp != NULL) {
                printf("%-13s %15s \n", pp->name, pp->tel);
                prn(pp->next);
        }
}

LIST *srch(LIST *sp, char *snam)
{
        int cmp;
        if ((cmp = strcmp(snam, sp->name)) == 0) {
                printf("\nname                      tel \n\n");
                printf("%-13s %15s \n", sp->name, sp->tel);
        }
        else
                sp->next = srch(sp->next, snam);
        return(sp);
}


LIST *modi(LIST *mp, char *mnam)
{
        char mtel[15];
        int cmp=1;
        if ((cmp = strcmp(mnam, mp->name)) == 0) {
                printf(" tel --> ");
                scanf("%s",mtel);
                strcpy(mp->name, mnam);
                strcpy(mp->tel, mtel);
        }
        else
                mp->next = modi(mp->next, mnam);
        return(mp);
}

LIST *dele(LIST *dp, char *dnam)
{
        int cmp=1;
        if ((cmp = strcmp(dnam, dp->name)) == 0) {
                dp->tel = "               ";
                dp->name = "                    ";
                printf(" Delete data -->  %s", dnam);
        }
        else
                dp->next = dele(dp->next, dnam);
        return(dp);
}




#include "stdlib.h" 이거 해줘야 malloc이 나오지

char *malloc(); 이렇게 만들어주면 뭐  어쩌자는거야;;;