/*open_addressing_hash.c -- 開放定址散列表實現檔案*/<br />#include <stdio.h><br />#include <stdlib.h><br />#include "open_addressing_hash.h"</p><p>/*局部資料類型定義*/<br />struct apple/*我實在想不出別的名字了*/<br />{<br />int full ;<br />int value ;<br />} ;</p><p>/*局部函數定義*/</p><p>int Get_Prime_Value (const int size) ;<br />int Is_An_Prime (const int number) ;<br />struct apple Linear_ (const int i, const int size) ;</p><p>/*介面函數定義*/</p><p>int Hash (const int item, const int size)<br />{<br />int value = item ;</p><p>return value % size ;<br />}</p><p>int InitializeTable (HashTable * const ph, const int size)<br />{<br />int count, temp ;</p><p>(*ph) = (struct hashtable *) malloc (sizeof (struct hashtable)) ;<br />if (NULL == (*ph))<br />{<br />puts ("Out of space[1].") ;<br />return 0 ;<br />}<br />(*ph) -> size = Get_Prime_Value (size) ;<br />(*ph) -> lists = (Cell *) malloc (sizeof (Cell) * (*ph) -> size) ;<br />if (NULL == (*ph) -> lists)<br />{<br />free (*ph) ;<br />puts ("Out of space[2].") ;<br />return 0 ;<br />}<br />temp = (*ph) -> size ;<br />for (count = 0; count < temp; count++)<br />(*ph) -> lists[count].info = Empty ;</p><p>return 1 ;<br />}</p><p>Cell * Find (const HashTable * const ph, const Item item)<br />{<br />struct apple value ;<br />int key, size, index, i = 0 ;</p><p>size = (*ph) -> size ;<br />key = Hash (item, size) ;<br />do<br />{<br />value = Linear_ (i++, size) ;<br />if (value.full)<br />/*此時遍曆了整個表*/<br />break ;<br />index = (key + value.value) % size ;<br />}<br />while (Legitimate == (*ph) -> lists[index].info && (*ph) -> lists[index].item != item)<br />;</p><p>return (*ph) -> lists + index ;<br />}</p><p>int Insert (HashTable * const ph, const Item item)<br />{<br />Cell * position ;</p><p>position = Find (ph, item) ;<br />if (Empty == position -> info || Deleted == position -> info)<br />{<br />position -> info = Legitimate ;<br />position -> item = item ;<br />return 1 ;<br />}</p><p>return 0 ;<br />}</p><p>int Delete (HashTable * const ph, const Item item)<br />{<br />Cell * position ;</p><p>position = Find (ph, item) ;<br />if (Legitimate == position -> info && item == position -> item)<br />{<br />position -> info = Deleted ;<br />return 1 ;<br />}</p><p>return 0 ;<br />}</p><p>void Traversal (const HashTable * const ph, void (* pfun) (const Cell cell))<br />{<br />int count, size = (*ph) -> size ;</p><p>for (count = 0; count < size; count++)<br />(* pfun) ((*ph) -> lists[count]) ;<br />}</p><p>void Release (HashTable * const ph)<br />{<br />free ((*ph) -> lists) ;<br />free (*ph) ;<br />}</p><p>/*局部函數定義*/</p><p>int Get_Prime_Value (const int size)<br />{<br />int prime_value = size ;</p><p>while (!Is_An_Prime (prime_value))<br />prime_value++ ;</p><p>return prime_value ;<br />}</p><p>int Is_An_Prime (const int number)<br />{<br />int count, size ;</p><p>if (number < 2)<br />{<br />puts ("Input wrong.") ;<br />return 0 ;<br />}<br />for (count = 2, size = number / 2 + 1; count < size; count++)<br />if (0 == number % count)<br />return 0 ;</p><p>return 1 ;<br />}</p><p>struct apple Linear_ (const int i, const int size)<br />{<br />struct apple value ;</p><p>if (i < 0)<br />puts ("Wrong element") ;<br />else if (i > size - 1)<br />value.full = 1 ;<br />else/* if (i <= size - 1)*/<br />{<br />value.full = 0 ;<br />value.value = i ;<br />}</p><p>return value ;<br />}