#include #define RECLEN 80 typedef struct noderec { char teldir[81]; struct noderec *fw_next; struct noderec *bw_next; } node; node *link; node *head,*tail,*newp,*temp,*prep,*scan; int nc,i,loop; char nteldir[81]; w_data(ptr) node * ptr; { int j,i=0; if (ptr == head) j=1; else j=2; while (ptr != NULL) { i += 1; printf("%4d. ",i); fputs(ptr->teldir,stdout); printf("\n"); if( (i % 20) == 0) { printf("\n\n\t\t\t\t\t.... press return to continue"); getchar(); } if (j == 1) ptr = ptr->fw_next; else ptr = ptr->bw_next; } if((i%20) != 0) { printf("\n\n\t\t\t\t\t.... press return to continue"); getchar(); } } build_double_link_list() { int c; printf("\n Please keyin customer records (to exit press ^d) \n"); while (1) { for(nc=0;(c=nteldir[nc]=getchar()) != '\n' && c != EOF; nc++); if (c == EOF) break; nteldir[nc] = '\0'; insert(); } } read_in_tel_dir() { int c,i=1; FILE *fp; head = tail = NULL; fp = fopen("teldir.out","r+"); while (1) { for(nc=0;(c=nteldir[nc]=fgetc(fp)) != '\n' && c != EOF; nc++); if (c == EOF) break; nteldir[nc] = '\0'; newp = (node *) malloc(sizeof(node)); if (i==1) head = newp; for(c=0;c<=nc;c++) newp->teldir[c] = nteldir[c]; newp->bw_next = tail; if(i>1) tail->fw_next = newp; newp->fw_next = NULL; tail = newp; i++; } fclose(fp); } insert_a_record() { printf("\n Please keyin the new customer record :\n"); for(nc=0;(nteldir[nc]=getchar()) != '\n'; nc++); nteldir[nc] = '\0'; insert(); } insert() { int j; scan = head; newp = (node *) malloc(sizeof(node)); if (head == NULL) { head = newp; tail = newp; for(j=0;j<=nc;j++) newp->teldir[j] = nteldir[j]; return(1); } while ((j=strncmp(nteldir,scan->teldir,nc)) > 0 ) { scan = scan->fw_next; if (scan == NULL) { tail->fw_next = newp; newp->bw_next = tail; newp->fw_next = NULL; for(j=0;j<=nc;j++) newp->teldir[j] = nteldir[j]; tail = newp; return(1); } } if (scan == head) { head = newp; scan->bw_next = newp; for (j=0;j<=nc;j++) newp->teldir[j] = nteldir[j]; newp->fw_next = scan; newp->bw_next = NULL; return(1); } temp = scan->bw_next; newp->bw_next = temp; newp->fw_next = scan; temp->fw_next = newp; scan->bw_next = newp; for(j=0; j<=nc;j++) newp->teldir[j] = nteldir[j]; } delete_a_record() { int j; scan = head; printf("\n Please keyin a customer name to be deleted \n"); for (nc=0; (nteldir[nc]=getchar()) != '\n'; nc++); nteldir[nc] = '\0'; while ( (scan != NULL) && ((j=strncmp(nteldir,scan->teldir,nc)) > 0)) scan = scan -> fw_next; if (scan == NULL || j < 0) { printf("\n%s not found, please check and try again\n",nteldir); return(1); } if ((scan->fw_next == NULL) && (scan->bw_next == NULL)) { printf("\nOops, you have deleted the last record in the list\n"); head = NULL; tail = NULL; return(1); } if (scan->fw_next == NULL) { printf("\nOops, You have deleted the last record in the list\n"); temp = scan->bw_next; tail = temp; temp->fw_next = NULL; return(1); } if (scan == head) { printf("\nOops, You have deleted the first record in the list\n"); temp = scan->fw_next; head = temp; temp->bw_next = NULL; } else { temp = scan->bw_next; temp->fw_next = scan->fw_next; temp = scan->fw_next; temp->bw_next = scan->bw_next; } } main() { FILE *fp; node *ptr; int option=0; do { printf("\n\n\n\n"); printf("\n Welcome to Telephone Directory Utility \n\n"); printf("\n 1. Build (or append to) the double link list"); printf("\n 2. Read in teldir.out to build the double link list"); printf("\n 3. Insert a record"); printf("\n 4. Delete a record"); printf("\n 5. print out directory listing in forward order"); printf("\n 6. Print out directory listing in backward order"); printf("\n 7. Exit\n\n"); printf("\n Select an option : "); scanf("%d",&option); getchar(); if (option == 1) build_double_link_list(); if (option == 2) read_in_tel_dir(); if (option == 3) insert_a_record(); if (option == 4) delete_a_record(); if (option == 5) w_data(head); if (option == 6) w_data(tail); } while (option != 7); fp = fopen("teldir.out","w+"); ptr = head; while(ptr != NULL) { fputs(ptr->teldir,fp); fputs("\n",fp); ptr = ptr->fw_next; } fclose(fp); }