開放定址散列表(線性探測法(雙散列))實現檔案C語言

來源:互聯網
上載者:User

/*open_addressing_double_hash.c -- 開放定址散列表雙散列實現檔案*/<br />#include <stdio.h><br />#include <stdlib.h><br />#include "open_addressing_double_hash.h"</p><p>/*局部函式宣告*/<br />static int Get_Prime_Value (const int size) ;<br />static int Is_A_Prime (const int number) ;<br />static int Leaner_ (const int i, const int item, const int prime) ;</p><p>/*介面函數定義*/</p><p>int Hash (const int item, const int size)<br />{<br />return item % size ;<br />}</p><p>int Hash2 (const int item, const int prime)<br />{<br />return prime - (item % prime) ;<br />}</p><p>int InitializeTable (HashTable * const ph, const int size)<br />{<br />int temp, count ;</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 />temp = (*ph) -> size = Get_Prime_Value (size) ;<br />(*ph) -> lists = (Cell *) malloc (sizeof (Cell) * temp) ;<br />if (NULL == (*ph) -> lists)<br />{<br />puts ("Out of space.[2]") ;<br />free (*ph) ;<br />return 0 ;<br />}<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 />int size, key, index, i = 0 ;</p><p>size = (*ph) -> size ;<br />key = Hash (item, size) ;<br />do<br />{<br />if (i < size)<br />index = (key + Leaner_ (i++, item, PRIME)) % size ;<br />else<br />break ;<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 (const HashTable * const ph, const Item item)<br />{<br />Cell * position ;</p><p>position = Find (ph, item) ;<br />if (position -> info != Legitimate)<br />{<br />position -> info = Legitimate ;<br />position -> item = item ;<br />return 1 ;<br />}</p><p>return 0 ;<br />}</p><p>int Delete (const 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 ;</p><p>size = (*ph) -> size ;<br />for (count = 0; count < size; count++)<br />(* pfun) ((*ph) -> lists[count]) ;<br />}</p><p>void Release (const HashTable * const ph)<br />{<br />free ((*ph) -> lists) ;<br />free (*ph) ;<br />}</p><p>/*局部函數定義*/<br />static int Get_Prime_Value (const int size)<br />{<br />int real_value = size ;</p><p>while (!Is_A_Prime (real_value))<br />real_value++ ;</p><p>return real_value ;<br />}</p><p>static int Is_A_Prime (const int number)<br />{<br />int count, temp ;</p><p>if (number < 2)<br />{<br />puts ("Wrong number.") ;<br />return 0 ;<br />}<br />for (count = 2, temp = number / 2 + 1; count <= temp; count++)<br />if (0 == number % count)<br />return 0 ;</p><p>return 1 ;<br />}</p><p>static int Leaner_ (const int i, const int item, const int prime)<br />{<br />return i * Hash2 (item, prime) ;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.