雙鏈表的查詢只能從頭往尾找,或者從尾往頭找, 效率低下, 所以就有了雜湊表。雜湊表是將雙鏈表通過乙個hash函式分類,分成若干類,形成乙個索引,然後查詢的時候就可以快速的定位到某一類,減少查詢的時間。下面是用雜湊表的底層使用核心鍊錶實現的。
.h檔案
#pragma once
#include "list.h"
struct hash_table ;
int hash_table_init(struct hash_table *phtable, size_t nmemb,
size_t (*hash)(const struct list_head *node),
int (*hash_cmp)(const struct list_head *a, const struct list_head *b));
int hash_table_destroy(struct hash_table *phtable);
.c檔案
測試#include #include "hash_table.h"
#include #include static int hash_del(struct hash_table *phtable, struct list_head *node)
} return -1;
}static int hash_push(struct hash_table *phtable, struct list_head *node)
static struct list_head *hash_search(const struct hash_table *phtable, const struct list_head *key)
list_for_each_continue(cur, phtable->tab + v)
} return null;
}int hash_table_init(struct hash_table *phtable, size_t nmemb,
size_t (*hash)(const struct list_head *node),
int (*hash_cmp)(const struct list_head *a, const struct list_head *b))
phtable->nmemb = nmemb;
phtable->hash = hash;
phtable->hash_cmp = hash_cmp;
phtable->search = hash_search;
phtable->push = hash_push;
phtable->del = hash_del;
return 0;
}int hash_table_destroy(struct hash_table *phtable)
} free(phtable->tab);
return 0;
}
#include #include "hash_table.h"
#include #include #include struct data_info ;
size_t hash(const struct list_head *node)
int hash_cmp(const struct list_head *a, const struct list_head *b)
int main(void)
, ,
, ,
, };
struct hash_table *phtable = (struct hash_table *)malloc(sizeof(*phtable));
assert(phtable != null);
hash_table_init(phtable, 3, hash, hash_cmp);
int i = 0;
for(; i < sizeof(s) / sizeof(s[0]); i++)
struct data_info person = ;
phtable->del(phtable, &(person.list));
struct list_head *pa = phtable->search(phtable, &(person.list));
if (pa == null) else
hash_table_destroy(phtable);
free(phtable);
return 0;
}
學習筆記 資料結構 雜湊
雜湊表的特點 什麼是完全雜湊 雜湊方式 直接定址表的特點 雜湊表的特點,可以解決什麼問題 什麼是衝突 如何避免衝突 鏈結法的特點,插入,刪除,查詢的時間複雜度是多少 開放定址方法的原理 什麼是裝載因子 雜湊函式有什麼 同義詞的概念 聚集 堆積 現象 單向雜湊表的特徵 如何提高雜湊表的查詢效率 通過一...
學習筆記 資料結構
一 常用的資料結構 1 線性資料結構 元素之間一般存在元素之間存在一對一關係,是最常用的一類資料結構,典型的有 陣列 棧 佇列和線性表 2 樹形結構 結點間具有層次關係,每一層的乙個結點能且只能和上一層的乙個結點相關,但同時可以和下一層的多個結點相關,稱為 一對多 關係,常見型別有 樹 堆 3 圖形...
資料結構學習筆記 雜湊表
1.什麼是雜湊表 1 雜湊表是從乙個集合a到另乙個集合b的對映。2 集合a中的元素稱為鍵值,集合b中的元素稱為hash值。3 對映在數學上相當於乙個函式,集合a中的元素如何對映到集合b,hash函式決定。4 若兩個不同的鍵值對應同乙個hash值,這種情況為hash碰撞。2.雜湊與查詢 設集合a為查詢...