/** * @brief Inserting a new node in the linked list * @param list, The linked list will be inserted * @param id, id number * @param the satellite value of the id **/ intLinkedList_InsertHead(Node *list,int id,int dat) { Node *p =malloc(sizeof(Node *)); if(p ==NULL){ return-1; } p->id = id; p->pNext = list->pNext; list->pNext = p; p->satellite = dat; return1; }
2.1.5 根据id搜索链表
/** * @brief search the id * @param list, The hash table will be searched * @param key, key * @retval the satellite value of the id **/ intLinkedList_Search(Node *list,int id) { Node *p = list->pNext; for(Node *p = list->pNext; p; p = p->pNext){ if(id == p->id){ return p->satellite; } } return0; }
/** * @brief search the key * @param table, The hash table will be searched * @param length, The length of the hash table * @retval the value of the key **/ intHashTable_Search(int key, Node **table,int length) { returnLinkedList_Search(table[HashTable_hashFunc(key, length)], key); } 3 测试例程
#define__MAX_LEN_TABLE_10 intmain(int argc,char*argv[]) { Node **pHashTable; pHashTable =HashTable_Create(__MAX_LEN_TABLE_); if(HashTable_Insert(12,125, pHashTable, __MAX_LEN_TABLE_)){ printf("Insert key = 12, value = 125 successful!\r\n"); }else{ printf("Insert key = 12 fail!\r\n"); } if(HashTable_Insert(22,255, pHashTable, __MAX_LEN_TABLE_)){ printf("Insert key = 22, value = 255 successful!\r\n"); }else{ printf("Insert key = 255 fail!\r\n"); } int value =HashTable_Search(22, pHashTable, __MAX_LEN_TABLE_); printf("value of key=22 is %d\r\n", value); value =HashTable_Search(12, pHashTable, __MAX_LEN_TABLE_); printf("value of key=12 is %d\r\n", value); HashTable_Destroy(pHashTable, __MAX_LEN_TABLE_);