#include stdio.h#include string.h#include stdlib.h#include stdbool.h#define SIZE 20
编程学习网为您整理以下代码实例,主要实现:C语言数据结构哈希表,希望可以帮到各位朋友。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 20
struct DataItem {
   int data;   
   int key;
};
struct DataItem* hashArray[SIZE]; 
struct DataItem* dummyItem;
struct DataItem* item;
int hashCode(int key) {
   return key % SIZE;
}
struct DataItem *search(int key) {
   //get the hash 
   int hashIndex = hashCode(key);  
   //move in array until an empty 
   while(hashArray[hashIndex] != NulL) {
      if(hashArray[hashIndex]->key == key)
         return hashArray[hashIndex]; 
      //go to next cell
      ++hashIndex;
      //wrap around the table
      hashIndex %= SIZE;
   }        
   return NulL;        
}
voID insert(int key,int data) {
   struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
   item->data = data;  
   item->key = key;
   //get the hash 
   int hashIndex = hashCode(key);
   //move in array until an empty or deleted cell
   while(hashArray[hashIndex] != NulL && hashArray[hashIndex]->key != -1) {
      //go to next cell
      ++hashIndex;
      //wrap around the table
      hashIndex %= SIZE;
   }
   hashArray[hashIndex] = item;
}
struct DataItem* delete(struct DataItem* item) {
   int key = item->key;
   //get the hash 
   int hashIndex = hashCode(key);
   //move in array until an empty
   while(hashArray[hashIndex] != NulL) {
      if(hashArray[hashIndex]->key == key) {
         struct DataItem* temp = hashArray[hashIndex]; 
         //assign a dummy item at deleted position
         hashArray[hashIndex] = dummyItem; 
         return temp;
      }
      //go to next cell
      ++hashIndex;
      //wrap around the table
      hashIndex %= SIZE;
   }      
   return NulL;        
}
voID display() {
   int i = 0;
   for(i = 0; i<SIZE; i++) {
      if(hashArray[i] != NulL)
         printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
      else
         printf(" ~~ ");
   }
   printf("\n");
}
int main() {
   dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
   dummyItem->data = -1;  
   dummyItem->key = -1; 
   insert(1, 20);
   insert(2, 70);
   insert(42, 80);
   insert(4, 25);
   insert(12, 44);
   insert(14, 32);
   insert(17, 11);
   insert(13, 78);
   insert(37, 97);
   display();
   item = search(37);
   if(item != NulL) {
      printf("Element found: %d\n", item->data);
   } else {
      printf("Element not found\n");
   }
   delete(item);
   item = search(37);
   if(item != NulL) {
      printf("Element found: %d\n", item->data);
   } else {
      printf("Element not found\n");
   }
}
				 沃梦达教程
				
			本文标题为:C语言数据结构哈希表
				
        
 
            
        基础教程推荐
             猜你喜欢
        
	     - C语言数组 1970-01-01
 - 明确指定任何或所有枚举数的整数值 1970-01-01
 - 对 STL 容器的安全并行只读访问 2022-10-25
 - 用指数格式表示浮点数 1970-01-01
 - C++:为什么结构类需要一个虚拟方法才能成为多态? 2022-10-19
 - C语言3个整数的数组 1970-01-01
 - 向量<unique_ptr<A>>使用初始化列表 2022-10-23
 - 迭代std :: bitset中真实位的有效方法? 2022-10-18
 - 总计将在节日礼物上花多少钱 1970-01-01
 - C++多态 1970-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				