#include #include #include int hash(char *, int); main() { char s1[50], s2[50]; int i,j,k, midterm, final, distance; int finfin, mark, found; int hvalue; typedef struct record { char s[50]; int mid; int fin; struct record *ptr; } Record; Record **ptrA; Record *x, *x1; Record y; // printf("%d\n",sizeof(y)); // printf("%d\n",sizeof(y.s)); // printf("%d\n",sizeof(y.mid)); // printf("%d\n",sizeof(y.fin)); // printf("%d\n",sizeof(y.ptr)); FILE *fp; fp=fopen("records.txt", "r"); if(fp == NULL) { perror("Can not open file"); return(-1); } fgets(s1, sizeof(s1), stdin); sscanf(s1,"%d",&k); ptrA=calloc(k, sizeof(&x)); if(ptrA==NULL) {perror("arrayyyyy"); return(-2);} // printf("Done allocating\n"); for(i=0; is, s1); x->mid=midterm; x->fin=final; x->ptr=NULL; if(ptrA[hvalue] == NULL) ptrA[hvalue]=x; else { x1=ptrA[hvalue]; while(x1->ptr != NULL) x1=x1->ptr; x1->ptr=x; } } fclose(fp); //Now check the array /******************************************************************** for(i=0;is, x->fin); x=x->ptr; } printf("\n"); } // else { // printf("Location %d final is %d\n",i, ptrA[i]->fin); // } } ************************************************************************/ //Now continue to read the input and for every name read, check if //it is in the list or not. while (fgets(s1, sizeof(s1), stdin) != NULL) { //if(strcmp(s1, "\n") ==0) continue; //get rid of new line i=0; while(s1[i] !='\n') i++; s1[i]='\0'; hvalue= hash(s1,k); //printf("hvalue is %d\n",hvalue); //Now check to see if s1 is in ptrA[hvalue] found=0; x=ptrA[hvalue]; distance=0; while( x != NULL) { if(strcmp(s1, x->s) ==0) {found=1; mark = x->mid; finfin=x->fin;break;} x=x->ptr; distance++; } if(found ==0) printf("The name does not exist in the file\n"); else printf("%d %d %d\n",mark,finfin,distance+1); } } int hash(char * p, int k) { int count=0; while (*p != '\0') {count=(count+ (int) *p)%k; p++;} return(count); }