標籤:雜湊hash
使用雜湊,實現6428633條CSDN賬戶資料的簡單查詢
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>char path[256] = "E:\\Big_Data\\csdn.txt";#define N 6428633unsigned int BKDRHash(char *str);struct beitai{char *pstr;//儲存字串struct beitai *pNext;//下一個節點};struct info{struct beitai *pbt;};struct info *pall = NULL;//插入struct beitai *addstr(struct beitai *phead, char *str){struct beitai *pnew = calloc(1, sizeof(struct beitai));//開闢節點int length = strlen(str);pnew->pstr = calloc(length + 1, sizeof(char));strcpy(pnew->pstr, str);//拷貝pnew->pNext = NULL;if (phead==NULL){phead = pnew;} else{pnew->pNext = phead;phead = pnew;}return phead;}//實現修改,查詢void find(struct beitai *phead, char *findstr){while (phead!=NULL){char*ps = strstr(phead->pstr, findstr);if (ps!=NULL){printf("%s", phead->pstr);//尋找}phead = phead->pNext;}}void changestr(char *str){char *pbak = str;//備份地址//去除‘’字元int i = 0;int j = 0;while ((str[i] = str[j++]) != '\0'){if (str[i] != ' '){i++;}}//截斷char *p1 = strstr(pbak, "#");if (p1 != NULL){*p1 = '\0';}}void init(){pall = malloc(N*sizeof(struct info));memset(pall, 0, N*sizeof(struct info));//清空FILE *pf = fopen(path, "r");for (int i = 0; i < N; i++){char str[100] = { 0 };char strbak[100] = { 0 };//備份fgets(str, 100, pf);//讀取strcpy(strbak, str);//拷貝changestr(str);//字串處理unsigned int data = BKDRHash(str);unsigned int id = data %N;pall[id].pbt = addstr(pall[id].pbt, strbak);//找到鏈表節點,插入}fclose(pf);}unsigned int BKDRHash(char *str){unsigned int seed = 13131313; // 31 131 1313 13131 131313 etc..unsigned int hash = 0;while (*str){hash = hash * seed + (*str++);}return (hash & 0x7FFFFFFF);}int getN(){FILE *pf = fopen(path, "r");if (pf == NULL){return -1;}else{int i = 0;while (!feof(pf)){char str[100] = { 0 };fgets(str, 100, pf);//讀取i++;}fclose(pf);return i;}}//實現查詢有衝突(相同的)資料void main(){printf("此資料一共有:%d行\n", getN());init();while (1){char str[100] = { 0 };scanf("%s", str);unsigned int id = BKDRHash(str) % N;find(pall[id].pbt, str);}system("pause");}
著作權聲明:本文為博主原創文章,歡迎指出代碼不良之處,提出代碼最佳化方案。歡迎指點,黑夜代碼,拚命更新,努力奮鬥中......
雜湊運用於大資料尋找中