雜湊運用於大資料尋找中

來源:互聯網
上載者:User

標籤:雜湊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");}


著作權聲明:本文為博主原創文章,歡迎指出代碼不良之處,提出代碼最佳化方案。歡迎指點,黑夜代碼,拚命更新,努力奮鬥中......

雜湊運用於大資料尋找中

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.