用雜湊法組織關鍵字 2

2021-07-08 20:26:39 字數 1287 閱讀 7863

問題及**:

/*  

* 檔名稱: main.cpp

* 完成日期:2023年12月13日

* 版本號:codeblocks

* 問題描述: 用雜湊法組織關鍵字

* 輸入描述: 無

* 程式輸出: 見執行結果

*/

#include #include #include #define n 15

#define m 26

typedef struct node //定義雜湊鍊錶的節點型別

lnode;

typedef struct

httype;

int h(char *s) //實現雜湊函式

//構造雜湊表

void hash(char *s, httype ht)

} }

//輸出雜湊表

void dispht(httype ht)

printf("\n");

} printf("---------------------\n");

}

//求查詢成功情況下的平均查詢長度

double searchlength1(char *s, httype ht)

count+=k;

} return 1.0*count/n; //成功情況僅有n種

}

//求查詢不成功情況下的平均查詢長度

double searchlength2(httype ht)

count+=k;

} return 1.0*count/m; //不成功時,在表長為m的每個位置上均可能發生

} int main()

; hash(s, ht);

dispht(ht);

printf("查詢成功情況下的平均查詢長度 %f\n", searchlength1(s, ht));

printf("查詢不成功情況下的平均查詢長度 %f\n", searchlength2(ht));

return 0;

}

執行結果:

知識點總結:

處理衝突的方法採用鏈位址法

y

專案2 用雜湊法組織關鍵字

檔名稱 main.cpp 完成日期 2015年12月7日 版本號 vc 6.0 問題描述 已知乙個關鍵字序列為if while for case do break else struct union int double float char long bool,共15個字串,雜湊函式h key 為...

十五周專案2 用雜湊法組織關鍵字

2015,煙台大學計算機與控制工程學院 檔名稱 haxibiao.cpp 完成日期 2015年12月13日 問題描述 已知乙個關鍵字序列為if while for case do break else struct union int double float char long bool,共15個...

第十五周專案2 用雜湊法組織關鍵字(2)

include include include define n 15 define m 26 typedef struct node 定義雜湊鍊錶的節點型別 lnode typedef struct httype int h char s 實現雜湊函式 構造雜湊表 void hash char s...