Step by Step write algorithm (the hash table)

Source: Internet
Author: User

"Disclaimer: Copyright All, welcome reprint, do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Hash tables, sometimes called hash lists. Personally, a hash table is an intermediate structure between a linked list and a two-fork tree. The use of linked lists is very convenient, but the data lookup is very troublesome; the data in the binary tree is strictly ordered, but this is the result of taking one more pointer as the price. Hash table not only satisfies the data search convenience, does not occupy too much content space at the same time, the use is also very convenient.

For example, all the data is like a lot of books. Let's say that these books are stacked up like a linked list or a linear table, the whole data will be very disorderly and messy, you have to go through a lot of inquiry before you find the book you need, and suppose you numbered all the books and arranged the books in order. So suppose you are looking for a book number is n, then after two points to find, you will find the books you need very quickly, but if you are not very much of each kind of books, then you can classify these books, which are literature, which are art, which are engineering, which are science, If you simply categorize these books, then finding a book can be very easy, for example, assuming that the book you're looking for is a computer-based book, then you'll be looking for it in the engineering category, which can look like a hassle.

I do not know this example you know no, the above-mentioned classification method is in fact the essence of the hash table. Below we can write a simple hash operation code.

a) define hash table and basic data node

typedef struct _NODE{INT data;struct _node* next;} node;typedef struct _hash_table{node* value[10];} hash_table;

b) Create a hash table

hash_table* create_hash_table () {hash_table* phashtbl = (hash_table*) malloc (sizeof (hash_table)); memset (phashtbl, 0, sizeof (hash_table)); return phashtbl;}

c) Find data in the hash table

node* Find_data_in_hash (hash_table* phashtbl, int data) {node* pnode;if (null = =  phashtbl) return null;if (null = = ( Pnode = phashtbl->value[data)) return Null;while (Pnode) {if (data = = Pnode->data) return Pnode;pnode = pnode- >next;} return NULL;}

d) Insert data into the hash table

STATUS Insert_data_into_hash (hash_table* phashtbl, int data) {node* pnode;if (null = = PHASHTBL) return false;if (null = = Phashtbl->value[data%]) {Pnode = (node*) malloc (sizeof (node)), memset (pnode, 0, sizeof (node));p Node->data = Data;phashtbl->value[data%] = Pnode;return TRUE;} if (null! = Find_data_in_hash (phashtbl, data)) return False;pnode = phashtbl->value[data% 10];while (null! = pnode-> Next) Pnode = Pnode->next;pnode->next = (node*) malloc (sizeof (node)), memset (pnode->next, 0, sizeof (node)); Pnode->next->data = Data;return TRUE;}

e) Delete data from the hash table

STATUS Delete_data_from_hash (hash_table* phashtbl, int data) {node* phead; node* pnode;if (NULL = = Phashtbl | | Null = = phashtbl->value[data%]) return false;if (null = = (Pnode = Find_data_in_hash (phashtbl, data)) return false;if (Pnode = = phashtbl->value[data% 10]) {Phashtbl->value[data%] = Pnode->next;goto final;} Phead = phashtbl->value[data% 10];while (pnode! = Phead->next) phead = Phead->next;phead->next = PNode->ne Xt;final:free (Pnode); return TRUE;}

Summary:

1, the hash table is not complex, we also often use in the development, suggest friends to master well;

2, hash table can be formed with two fork tree composite structure, as for why, suggest friends think about it?


Step by Step write algorithm (the hash table)

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.