One-step write algorithm (hash table)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

A hash table is also called a hash table. In my opinion, a hash table is an intermediate structure between a linked list and a binary tree. Linked lists are easy to use, but data query is very troublesome. Data in Binary Trees is strictly ordered, but this result is at the cost of one more pointer. The hash table is convenient to search for data without occupying too much content space.

 

For example, all the data is like a lot of books. If these books are piled up, like linked lists or linear tables, the data will appear unordered and messy. Before you find the books you need, you have to go through many query processes. If you number all the books and sort them in order, if the number of the books you are looking for is n, after a binary search, you will soon find the books you need. However, if there are not many types of books, you can classify these books and what are literature, what are arts, what are engineering, and what are science, as long as you simply classify these books, it will become very easy to find a book, for example, if the book you are looking for is a computer book, you will go to the engineering class to search for it, which will also become troublesome.

 

I don't know if this is the case. The classification method mentioned above is actually the essence of the hash table. Below we can write a simple hash operation code.

 

A) Define hash tables and basic data nodes

 

Typedef struct _ NODE

{

Int data;

Struct _ NODE * next;

} NODE;

 

Typedef struct _ HASH_TABLE

{

NODE * value [10];

} HASH_TABLE;

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;

}

HASH_TABLE * create_hash_table ()

{

HASH_TABLE * pHashTbl = (HASH_TABLE *) malloc (sizeof (HASH_TABLE ));

Memset (pHashTbl, 0, sizeof (HASH_TABLE ));

Return pHashTbl;

}

C) Search for 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 % 10])

Return NULL;

 

While (pNode ){

If (data = pNode-> data)

Return pNode;

PNode = pNode-> next;

}

Return NULL;

}

NODE * find_data_in_hash (HASH_TABLE * pHashTbl, int data)

{

NODE * pNode;

If (NULL = pHashTbl)

Return NULL;

 

If (NULL = (pNode = pHashTbl-> value [data % 10])

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_1__hash (HASH_TABLE * pHashTbl, int data)

{

NODE * pNode;

If (NULL = pHashTbl)

Return FALSE;

 

If (NULL = pHashTbl-> value [data % 10]) {

PNode = (NODE *) malloc (sizeof (NODE ));

Memset (pNode, 0, sizeof (NODE ));

PNode-> data = data;

PHashTbl-> value [data % 10] = 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;

}

STATUS insert_data_1__hash (HASH_TABLE * pHashTbl, int data)

{

NODE * pNode;

If (NULL = pHashTbl)

Return FALSE;

 

If (NULL = pHashTbl-> value [data % 10]) {

PNode = (NODE *) malloc (sizeof (NODE ));

Memset (pNode, 0, sizeof (NODE ));

PNode-> data = data;

PHashTbl-> value [data % 10] = 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 % 10])

Return FALSE;

 

If (NULL = (pNode = find_data_in_hash (pHashTbl, data )))

Return FALSE;

 

If (pNode = pHashTbl-> value [data % 10]) {

PHashTbl-> value [data % 10] = pNode-> next;

Goto final;

}

 

PHead = pHashTbl-> value [data % 10];

While (pNode! = PHead-> next)

PHead = pHead-> next;

PHead-> next = pNode-> next;

 

Final:

Free (pNode );

Return TRUE;

}

STATUS delete_data_from_hash (HASH_TABLE * pHashTbl, int data)

{

NODE * pHead;

NODE * pNode;

If (NULL = pHashTbl | NULL = pHashTbl-> value [data % 10])

Return FALSE;

 

If (NULL = (pNode = find_data_in_hash (pHashTbl, data )))

Return FALSE;

 

If (pNode = pHashTbl-> value [data % 10]) {

PHashTbl-> value [data % 10] = pNode-> next;

Goto final;

}

 

PHead = pHashTbl-> value [data % 10];

While (pNode! = PHead-> next)

PHead = pHead-> next;

PHead-> next = pNode-> next;

 

Final:

Free (pNode );

Return TRUE;

}

Summary:

 

1. hash Tables are not complex and we often use them during development. We recommend that you master them well;

 

2. the hash table can form a composite structure with a binary tree. As for why, I suggest you think about it?

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.