Hi,
I've written this function for a phone directory and it is essential that it is good programming code..but for some reason its not really working..the names are not being stored where they should be and thats frustrating me..please help..Thanks
Please ignore the deleteNode function..but look at the rest..thanks
A
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char names[20]; } KEY; typedef struct { KEY k; char phone[10]; } DATA; typedef struct nodetag { DATA directory; struct nodetag *next; } NODE; int getOption(); void options(); void errorMsg(); char anotherContact(); void createList(NODE *pList, NODE *pPre, NODE *pNew, DATA directory); int searchList(NODE *pList, NODE **pCur, NODE **pPre, KEY k); int cmp(KEY k, KEY k2); NODE *insertNode(NODE *pList, NODE *pPre, DATA directory); NODE *deleteNode(NODE *pList, NODE *pPre, NODE *pCur); DATA getDetails(); void printNode(NODE *pCur); void printList(NODE *pList); int main(void) { int num = 1; DATA directory; NODE *pList; NODE *pPre; NODE *pNew; NODE *pCur; int temp; KEY k; options(); num = getOption(); pPre = pList = pNew = pCur = NULL; while(num) { switch(num) { case 1: createList(pList, pPre, pNew, directory); break; case 2: printf("Enter the first name of contact you wish to search: "); scanf("%s", &k.names); temp = searchList(pList, &pCur, &pPre, k); if(temp == 1) { printf("Directory listing:\t\n"); printf("NAME\t\t\tPHONE #\n"); printNode(pCur); } else { errorMsg(); } break; case 3: directory = getDetails(); pList = insertNode(pList, pPre, directory); break; case 4: deleteNode(pList, pPre, pCur); break; case 5: printList(pList); break; case 6: printf("\nBye Bye Now!!\n\n"); return 0; } printf("\nWhat would you like to do next: "); num = getOption(); } return 0; } /**************************getOption*********************** */ int getOption() { int x; scanf("%d", &x); while(x <= 0 || x > 6) { printf("Invalid Input\n"); printf("Please enter a number from the menu above: "); scanf("%d", &x); } return x; } /********************options************************************* */ void options() { printf("What would you like to do today?\n"); printf("1. Create a phone list\n"); printf("2. Search for a phone number by name\n"); printf("3. Add a new name and phone number to the list\n"); printf("4. Delete a name from the list\n"); printf("5. Print the list\n"); printf("6. Quit\n"); printf("Choose an option from the menu above: "); return; } /************************errorMsg******************************* */ void errorMsg() { printf("ERROR: Name not found!! Please try again"); } /**************************anotherContact*********************** */ char anotherContact() { char cont; printf("\nWould You Like To Add Another Contact (y/n)?"); scanf("%c", &cont); return cont; } /***********************createList******************************* */ void createList(NODE *pList, NODE *pPre, NODE *pNew, DATA directory) { char cont = 'y'; int locn; NODE *pCur; pList = NULL; while(cont == 'y' || cont == 'Y') { directory = getDetails(); locn = searchList(pList, &pCur, &pPre, directory.k); if(locn == 0) { pList = insertNode(pList, pPre, directory); cont = anotherContact(); } else { printf("\a\nName already exists in directory!!!!\n"); cont = anotherContact(); } } } /*************************searchList***************************** */ int searchList(NODE *pList, NODE **pCur, NODE **pPre, KEY k) { int found = 0; // initialize *pPre = NULL; *pCur = pList; //Start search while(*pCur != NULL && (cmp(k, (*pCur)->directory.k) > 0)) { *pPre = *pCur; *pCur = (*pCur)-> next; } if(*pCur && (cmp(k, (*pCur)->directory.k) == 0)) { found = 1; } return found; } /*******************compare************************************** */ int cmp(KEY k, KEY k2) { int result; int cmp; cmp = strcmp(k.names, k2.names); if(cmp > 0) { result = 1; } else if(cmp == 0) { result = 0; } else { result = -1; } return result; } /************************insertNode****************************** */ NODE *insertNode(NODE *pList, NODE *pPre, DATA directory) { NODE *pNew; DATA d; if(!(pNew = (NODE*)malloc(sizeof(NODE)))) printf("\aMemory Overflow in insert\n"), exit (100); pNew->next = NULL; if(pPre == NULL) { pNew->next = pList; pList = pNew; } else { pNew->next = pPre->next; pPre->next = pNew; } return pList; } /************************deleteNode****************************** */ NODE *deleteNode(NODE *pList, NODE *pPre, NODE *pCur) { if(pPre == NULL) pList = pCur ->next; else pPre->next = pCur->next; free(pCur); return pList; } /*********************getDetails********************************* */ DATA getDetails() { DATA d; fflush(stdin); printf("\nPlease enter name of contact you wish to add:"); gets(d.k.names); printf("Please enter phone number:"); gets(d.phone); return d; } /**********************printNode********************************** */ void printNode(NODE *pCur) { printf("%-20s\t",pCur->directory.k.names); printf("%10s", pCur->directory.phone); printf("\n"); return; } /************************printList*********************************** */ void printList(NODE *pList) { NODE *pWalker; pWalker = pList; if(pWalker == NULL) printf("\n\aWARNING: List Currently Empty!!!\n "); else { printf("\n\nPhone Directory contains:\n\n"); printf("\nNAME\t\tPHONE #\n"); while(pWalker) { printNode(pWalker); pWalker = pWalker->next; } printf("\n"); } return; }



LinkBack URL
About LinkBacks




