雜湊表(一)(雜湊)分離連結法實現
阿新 • • 發佈:2019-01-10
編譯環境:vs2015
實現100以內完全平方數的雜湊表建立,當然更多數也是可以的……基本是例題難度,寫了好大一天。
定義結構體:
主要實現函式:
// ConsoleApplication5.cpp : Defines the entry point for the console application. // #include "stdafx.h" // // main.c // t12_23-c // // Created by 04 on 16/12/22. // Copyright 2016年 feng. All rights reserved. // #include <stdio.h> #include<stdlib.h> #include <math.h> struct ListNode; typedef struct ListNode *Position; struct HashTb1; typedef struct HashTb1 *HashTable; typedef int ElementType; int nextPrime(int x); //表節點 Next塞入第一個元素 struct ListNode { ElementType element; Position next; }; typedef Position List; struct HashTb1 { int tableSize; List * TheList; }; //初始化 HashTable InitializeTable(int TableSize); //銷燬表 void DestroyTable(HashTable H); //找到表節點 Position Find(ElementType key, HashTable H); //插入元素 void Insert(ElementType key, HashTable H); //找到首個表節點的位置 int Hash(ElementType key, int HashSize); //遍歷 void visited(HashTable H); int main(int argc, const char * argv[]) { //test int TableSize = 10; HashTable H = InitializeTable(TableSize); for (int i = 0; i <= 10; i++) { int j = i*i; Insert(j, H); } visited(H); DestroyTable(H); visited(H); DestroyTable(H); } HashTable InitializeTable(int TableSize) { //尋找質數 HashTable H; H = (HashTable)malloc(sizeof(struct HashTb1)); if (H == NULL) { printf("Out of space!"); } H->tableSize = nextPrime(TableSize); H->TheList = (List *)malloc(H->tableSize*(sizeof(struct ListNode))); for (int i = 0; i < H->tableSize; i++) { H->TheList[i] = (struct ListNode*)malloc(sizeof(struct ListNode)); if (H->TheList[i] == NULL) { printf("不存在!\n"); } else { H->TheList[i]->element = i; H->TheList[i]->next = NULL; } } return H; } void DestroyTable(HashTable H) { if (H->tableSize == 0) { printf("無需銷燬,表不存在!\n"); } else { for (int i = 0; i < H->tableSize; i++) { Position p = H->TheList[i]; Position q; while (p->next != NULL) { q = p; p = p->next; free(q); } } H->tableSize = 0; H->TheList = NULL; } } Position Find(ElementType key, HashTable H) { Position p = H->TheList[Hash(key, H->tableSize)]; while (p != NULL) { if (p->element != key) { p = p->next; } else { break; } } if (p == NULL) { return NULL; } else { return p; } } //插入 void Insert(ElementType key, HashTable H) { if (H->tableSize != 0) { Position p = Find(key, H); Position NewCell; List L = H->TheList[Hash(key, H->tableSize)]; if (p == NULL) { NewCell = (struct ListNode*)malloc(sizeof(struct ListNode)); NewCell->next = L->next; NewCell->element = key; L->next = NewCell; } else { p->next = (struct ListNode *)malloc(sizeof(struct ListNode)); p->next->element = p->element; p->next->next = NULL; } } else { printf("沒東西.\n"); } } void visited(HashTable H) { if (H->tableSize == 0) { printf("無表!\n"); } else { for (int i = 0; i < H->tableSize; i++) { Position p = H->TheList[i]; while (p != NULL) { printf("%d\t", p->element); p = p->next; } printf("\n"); } } } int Hash(ElementType key, int HashSize) { int NewKey = key%HashSize; return NewKey; } //求下一個質數 質數大小是比較易存的 int nextPrime(int x) { int flag = 0; for (int i = x; flag == 0; i++) { int j = 2; for (; j <= sqrt(i); j++) { if (i%j == 0) { break; } } if (j == (int)sqrt(i) + 1) { flag = i; } } return flag; }