Full C code for find, insert, and delete operations for a closed hash list

Source: Internet
Author: User

/* Build, find, INSERT, delete//#include <stdio.h> #define NIL-1//Assume that the keyword is a non-negative integer # define del-2typedef int KeyType;  KeyType hashtable[13]; Easy to verify the algorithm, the number of keywords is assumed to be not more than 13, the hash table is set to 13//keyword insertion function void inserthashtable (KeyType k) {for (int i=0; i<13; i++) if (NIL = = HashTable [(K%13+i)%13] | | DEL = = hashtable[(k%13+i)%13]) {hashtable[(k%13+i)%13] = K;break;}} The lookup operation of the hash table, the lookup succeeds returns the following table, otherwise returns -1int Hashsearch (KeyType k) {int i = 0;while (i<13) {if (k = = hashtable[((k%13) +i)%13]) retur N ((k%13) +i)%13;else if (NIL = = hashtable[((k%13) +i)%13]) return-1;i++;} if (= = i) return-1;} Create hash table void Createhashtable () {int n; KeyType key;for (int i=0; i<13; i++) Hashtable[i] = nil;printf ("Please enter the number of keywords: \ n"), scanf ("%d", &n);p rintf ("Please enter% The value of the D keyword: \ n ", N), for (i=0; i<n; i++) {scanf ("%d ", &key), if (-1 = = Hashsearch (key)) inserthashtable (key);}} Delete operation for hash table void Deletehashtable (KeyType k) {int index = Hashsearch (k); if (-1 = = index) printf ("No this keyword!\n"); elsehashtable[i Ndex] = DEL;} Print hash table void printhashtable (void) {printf ("current hash table saveThe key word for the storage is: \ n "), for (int i=0; i<13; i++) printf ("%d ", Hashtable[i]);p rintf (" \ n ");} int main () {KeyType k; Createhashtable (); Printhashtable ();p rintf ("Please enter the keyword to insert: \ n"); scanf ("%d", &k); inserthashtable (k); Printhashtable ();p rintf ("Please enter the keyword to delete: \ n"); scanf ("%d", &k);D eletehashtable (k); Printhashtable ();p rintf ("Enter the keyword to find: \ n"), scanf ("%d", &k), and if (-1! = Hashsearch (k)) printf ("Find the keyword!\n at position%d of the current table", Hashsearch (k) +1); elseprintf ("Without this keyword!\n"); return 0;}

Full C code for find, insert, and delete operations for a closed hash list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.