資料結構 c語言 hash查詢 鏈位址法實現

2021-08-04 15:11:54 字數 2088 閱讀 7593

一.t標頭檔案hash.h的實現

#ifndef __hash_h__

#define __hash_h__

#include

#include

#include

#include

#define n 11

typedef int datatype_el;

typedef struct listnode

list_node,*list_pnode;

typedef list_pnode datatype;

typedef struct hastbl

hash_tbl,*hash_tp;

extern void init_hash(hash_tp *hp,int m);

extern void create_hash(hash_tp hp,datatype_el *a);

extern list_pnode hash_search(hash_tp hp,int key);

extern void hash_show(hash_tp hp,int hash_val,list_pnode new);

#endif

二.函式實現hash.c

#include"hash.h"

int p;

void init_hash(hash_tp *hp,int m)

(*hp)->length = m;

(*hp)->h =(datatype*)malloc((*hp)->length * sizeof(datatype));

if(null == (*hp)->h)

for(int i = 0;i <(*hp)->length;i++)

(*hp)->h[i] = null;

}int fun(int m)

if(i >= m)

return m;

}return -1;

}void create_hash(hash_tp hp,datatype_el *a)

new -> data = a[i];

//3.將記錄儲存在hash_val的位置

new ->next = hp ->h[hash_val];

hp->h[hash_val] = new;

hash_show(hp,hash_val,new);

sleep(1);}}

list_pnode hash_search(hash_tp hp,int key)

void hash_show(hash_tp hp,int hash_val,list_pnode new)

if(i == hash_val)

printf("    正在插入%d",new->data);

puts("");

}printf("***************************************");

}三包含主函式的test.c檔案

#include"hash.h"

int main()

;//1.根據記錄個數得到hash表的長度

m =(int) ceil(n/0.75); //向下取整

//2,初始化哈係表空間

init_hash(&hp,m);

//3.建立hash表

create_hash(hp,a);

表查詢while(1)

else

printf("continue?(y/y)");

while(getchar() != '\n');

scanf("%c",&ch);

if(ch == 'y' || ch == 'y')

continue;

else

break;

}return 0;

}四.makefile 檔案

cc = gcc                                    

cflags= -wall -g -o0

test:hash.c test.c

$(cc) $(cflags) -o $@ $^

clean:

$(rm) hash_search .*.sw?

資料結構 C語言 資料結構 查詢

二 查詢演算法的效能分析 三 基於線性表的查詢 四 基於樹的查詢 五 基於雜湊表的查詢 文章索引 分類typedef struct elemtype typedef struct sstable 從表中第一條 最後一條記錄開始,逐個進行記錄的關鍵字與給定值的比較,若某個記錄的關鍵字和給定值比較相等,...

C語言資料結構 鏈佇列

鏈佇列與鍊錶相似,不同的是它具有對列的運算方法,儲存結構和鍊錶相同,下面是其結構示意圖 這裡的 q類似於鍊錶的頭節點 head 它包含兩個指向佇列節點的指標front和rear 當front和rear都為 時隊列為空 front指向佇列第乙個節點,rear指向最後乙個節點,當兩者指向同乙個節點說明佇...

鏈棧 資料結構 c語言

這是棧的結構 進棧示意 出棧示意 這是乙個鏈棧,注意與順序棧的區分 include stdio.h include stdlib.h define maxsize 100 typedef struct snode link void menu void link initialize void 初始...