entry *append(char lastName[], entry *e) { #ifdef NOMAL e->pNext = (entry *) malloc(sizeof(entry)); e = e->pNext; strcpy(e->lastName, lastName); e->pNext = NULL; return e; #endif #ifdef AVL return e = insert(lastName,e); #endif #ifdef HASH int hash_num = hash31(lastName); entry *tmp = (entry *)malloc(sizeof(entry)); strcpy(tmp->lastName,lastName); if(hash_table[hash_num]==NULL){ hash_table[hash_num] = tmp; return e; } else{ tmp->pNext = hash_table[hash_num]; hash_table[hash_num] = tmp; return e; } #endif #ifdef BST if(e==NULL){ e = (entry *)malloc(sizeof(entry)); strcpy(e->lastName,lastName); e->pR = NULL; e->pL = NULL; return e; } entry *t,*tmp = e; while(tmp!=NULL){ if(strcasecmp(tmp->lastName,lastName) < 0 ){ t = tmp; tmp = tmp->pR; } else if(strcasecmp(tmp->lastName,lastName) > 0 ){ t = tmp; tmp = tmp->pL; } } if(strcasecmp(t->lastName,lastName) > 0){ tmp = (entry *)malloc(sizeof(entry)); strcpy(tmp->lastName,lastName); t->pL = tmp; } else{ tmp = (entry *)malloc(sizeof(entry)); strcpy(tmp->lastName,lastName); t->pR = tmp; } return e; #endif }
/* FILL YOUR OWN IMPLEMENTATION HERE! */ entry *findName(char lastname[], entry *pHead) { #ifdef NOMAL while (pHead != NULL) { if (strcasecmp(lastname, pHead->lastName) == 0) return pHead; pHead = pHead->pNext; } /* TODO: implement */ return NULL; #endif #ifdef HASH int hash_num = hash31(lastname); entry *t = hash_table[hash_num]; while (t != NULL) { if (strcasecmp(lastname, t->lastName) == 0) return t; t = t->pNext; } return NULL; #endif #ifdef BST while(pHead!=NULL){ if(strcmp(pHead->lastName,lastname) > 0 ) pHead = pHead->pL; else if(strcmp(pHead->lastName,lastname) < 0 ) pHead = pHead->pR; else return pHead; } return NULL; #endif }
// ----------------------------------------------------- // Compute hash value in range [0..nBuckets-1] from string s static int hash( symtab* tab, int item) { return hash31(tab->a,tab->b,item) % tab->size; }