In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what the hash table of the C language data structure is. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
/ * * Program name: hash.c, this program demonstrates the implementation of a hash table, with a single linked list of data elements leading the node. * * / # include # include # include / / the structure of the data element in the hash table. Typedef struct Element {unsigned int key; / / keyword. Int value; / / data element other data items, which can be any data type. / / char value [1001]; / / other data items of the data element, which can be any data type. } Element; / / single linked list of data elements. Typedef struct Node {Element elem; / / data element. Struct Node * next; / / next pointer. } Node; / / Hash table typedef struct HashTable {struct Node * head; / / data element storage base address, dynamically allocating arrays. The current size of the int tablesize; / / hash table, that is, the table length. The number of data elements in the int count; / / hash table. } HashTable; / / initialize the hash table. Tablesize is the length of the hash table and returns the address of the hash table. HashTable * InitHashTable (const unsigned int tablesize) {/ / assign a hash table. HashTable * hh= (HashTable *) malloc (sizeof (HashTable)); hh- > tablesize=tablesize; / / Hash table length. / / assign and initialize the header node of the single linked list of data elements. Hh- > head= (Node *) malloc ((hh- > tablesize) * sizeof (Node)); memset (hh- > head,0, (hh- > tablesize) * sizeof (Node)); hh- > count=0; / / the number of data elements in the hash table is set to 0. Return hh;} / / Hash function. Unsigned int Hash (HashTable * hh,unsigned int key) {return key%hh- > tablesize; / / A pair of table length remainder. } / / look for keywords in the hash table, successfully return the address of the node in the single-linked table, and return empty if failed. Node * LookUp (HashTable * hh,unsigned int key) {int ii; ii=Hash (hh,key); / / get the hash address of the key key. Node * pp=hh- > head [II]. Next; / traverse the single linked list. While ((pp- > elem.keykeeper null) {pp=pp- > next;} return pp;} / / removes the key and its data from the hash table and returns 1 successfully, or 0 if the keyword does not exist. Int Delete (HashTable * hh,unsigned int key) {int ii; ii=Hash (hh,key); / / get the hash address of the key key. Node * pp=&hh- > head [ii]; / traverse the single linked list, and the pp pointer stays at the node before the key key to be deleted. While ((pp- > nextsteps null) & & (pp- > next- > elem.keykeeper keys) {pp=pp- > next;} if (pp- > next==NULL) return 0; / / failed to find. Node * tmp=pp- > next; / / tmp is the node to be deleted. Pp- > next=pp- > next- > next; / / written as p-> next=tmp- > next is more concise. Free (tmp); / / release the node. The number of elements in the hh- > count--; / / table minus 1. Return 1;} / / inserts a data element into the hash table and returns 1 successfully, or 0 if the data element keyword already exists. Int Insert (HashTable * hh,Element * ee) {/ / finds whether the keyword already exists, and if so, the insertion fails. Node * pp=LookUp (hh,ee- > key); if (invalid null) {printf ("keyword% d already exists.\ n", ee- > key); return 0;} Node * qq= (Node *) malloc (sizeof (Node)); memcpy (& qq- > elem,ee,sizeof (Element)); / / insert new data elements with headers. Int ii=Hash (hh,ee- > key); qq- > next=hh- > head [II]. Next; hh- > head [II]. Next = qq; hh- > count++; / / the number of elements in the table plus 1. Return 1;} / destroy the hash table void FreeHashTable (HashTable * hh) {int ii; Node * pp,*qq; / / release all single linked lists. For (ii=0;iitablesize;ii++) {pp=hh- > head [II]. Next; while (pp) {qq=pp- > next; free (pp); pp=qq;}} / / releases the header node array of all single linked lists. Free (hh- > head); free (hh); / / release the hash table. } / / print the hash table. Void PrintTable (HashTable * hh) {int ii; for (ii=0;iitablesize;ii++) {Node * pp=hh- > head [II] .next; while (pp) {printf ("[% dmurt% d]", pp- > elem.key,pp- > elem.value); / / printf ("[% dmurt% s]", pp- > elem.key,pp- > elem.value); pp=pp- > next;} printf ("^\ n") } printf ("\ n");} int main () {/ / initialize the hash table. HashTable * hh=InitHashTable (10); Element ee; / / insert data elements with keywords from 10 to 20. Ee.key=10; ee.value=110; Insert (hh,&ee); ee.key=11; ee.value=111; Insert (hh,&ee); ee.key=12; ee.value=112; Insert (hh,&ee); ee.key=13; ee.value=113; Insert (hh,&ee); ee.key=14; ee.value=114; Insert (hh,&ee); ee.key=15; ee.value=115; Insert (hh,&ee); ee.key=16; ee.value=116; Insert (hh,&ee); ee.key=17; ee.value=117 Insert (hh,&ee); ee.key=18; ee.value=118; Insert (hh,&ee); ee.key=19; ee.value=119; Insert (hh,&ee); / / insert data elements with keywords from 20 to 30. Ee.key=20; ee.value=120; Insert (hh,&ee); ee.key=21; ee.value=121; Insert (hh,&ee); ee.key=22; ee.value=122; Insert (hh,&ee); ee.key=23; ee.value=123; Insert (hh,&ee); ee.key=24; ee.value=124; Insert (hh,&ee); ee.key=25; ee.value=125; Insert (hh,&ee); ee.key=26; ee.value=126; Insert (hh,&ee); ee.key=27; ee.value=127 Insert (hh,&ee); ee.key=28; ee.value=128; Insert (hh,&ee); ee.key=29; ee.value=129; Insert (hh,&ee); / / insert data elements with keywords from 30 to 32. Ee.key=30; ee.value=130; Insert (hh,&ee); ee.key=31; ee.value=131; Insert (hh,&ee); ee.key=32; ee.value=132; Insert (hh,&ee); printf ("count=%d\ n", hh- > count); PrintTable (hh); / / print hash table Delete (hh,12); / / delete the data element with the key 12 in the hash table. Printf ("count=%d\ n", hh- > count); PrintTable (hh); / / print hash table / / find keyword 18 in the hash table. Node * pp=LookUp (hh,18); if (pp==0) printf ("LookUp (18) failed.\ n"); else printf ("key=18,value=%d.\ n", pp- > elem.value); / / ee.key=10; strcpy (ee.value, "00010 Beauty of the West"); Insert (hh,&ee); / / PrintTable (hh); / / print hash table FreeHashTable (hh); / / destroy hash table return 0 } this is the end of the article on "what is the hash table of C language data structure". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.