Create a simple Hash table by C language, hashtable

Source: Internet
Author: User

Create a simple Hash table by C language, hashtable

 

1 # define SIZE 1000 // define the initial SIZE of the Hash table 2 struct HashArray 3 {4 int key; 5 int count; 6 struct Array * next; 7} Hash [SIZE]; // initialize 8 void addHash (int num) in the main function // Add data 9 {10 int temp = abs (num % SIZE) in the Hash table ); // The added data can include a negative number of 11 if (Hash [temp]. key = 0) 12 {13 Hash [temp]. key = num; 14 Hash [temp]. count ++; 15} else if (Hash [temp]. key = num) 16 {17 Hash [temp]. count ++; 18} else19 {20 struct HashArray * p = & Hash [temp]; 2 1 while (p-> key! = Num & p-> next! = NULL) 22 {p = p-> next;} 23 if (p-> key = num) 24 {p-> count ++ ;} 25 else26 {27 p-> next = (struct HashArray *) malloc (sizeof (struct HashArray); 28 p = p-> next; 29 p-> key = num; 30 p-> count = 1; 31 p-> next = NULL; 32} 33} 34}

 

Related Article

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.