01-(2) Data structure-step-by-step write algorithm (hash table)

Source: Internet
Author: User

hash tables, sometimes called hash lists. Personally, the hash table is an intermediate structure between a linked list and a two-fork tree. It is very convenient to use the linked list, but the data lookup is very troublesome; the data in the binary tree is strictly ordered, but this is the result of the cost of one more pointer.    Hash table not only satisfies the data search convenience, at the same time does not occupy too much content space, the use is also very convenient. For example, all data is like a lot of books. If these books are piled up like a linked list or a linear table, the whole data will be very disorderly and messy, and you will have to go through a lot of queries before you find the book you need, and if you number all the books and arrange them in order, So if you are looking for a book number is n, then after two points to find, you will soon find the books you need, but if you are not a lot of books, then you can categorize 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 simple, for example, if the book you are looking for is a computer-based book, then you will find it in the engineering category and it will look like trouble. I do not know this example you know no, the above-mentioned classification method is actually the essence of the hash table.    Below we can write a simple hash operation code. A) Define hash table and basic data node [CPP] View plain copytypedefstruct_node {intdata; struct_node*Next;    }node; typedefstruct_hash_table {NODE* value[Ten];      }hash_table; b) Create hash table [CPP] View plain copyhash_table*create_hash_table () {hash_table* Phashtbl = (hash_table*) malloc (sizeof(hash_table)); memset (PHASHTBL,0,sizeof(hash_table)); returnphashtbl; } c) Looking for data in the hash table [CPP] View plain Copynode* Find_data_in_hash (hash_table* phashtbl,intdata) {NODE*Pnode; if(NULL = =phashtbl)returnNULL; if(NULL = = (Pnode = phashtbl->value[data%Ten]))          returnNULL;  while(pnode) {if(Data = = Pnode->data)returnPnode; Pnode= pnode->Next; }      returnNULL; } d) Insert data into the hash table [CPP] View plain copystatus insert_data_into_hash (hash_table* Phashtbl,intdata) {NODE*Pnode; if(NULL = =phashtbl)returnFALSE; if(NULL = = phashtbl->value[data%Ten]) {Pnode= (node*) malloc (sizeof(NODE)); memset (Pnode,0,sizeof(NODE)); Pnode->data =data; Phashtbl->value[data%Ten] =Pnode; returnTRUE; }        if(NULL! =Find_data_in_hash (PHASHTBL, data))returnFALSE; Pnode= phashtbl->value[data%Ten];  while(NULL! = pnode->next) Pnode= pnode->Next; Pnode->next = (node*) malloc (sizeof(NODE)); memset (Pnode->next,0,sizeof(NODE)); Pnode->next->data =data; returnTRUE; } e) Delete data from the hash table [CPP] View plain copystatus delete_data_from_hash (hash_table* Phashtbl,intdata) {NODE*Phead; NODE*Pnode; if(NULL = = Phashtbl | | NULL = = Phashtbl->value[data%Ten])          returnFALSE; if(NULL = = (Pnode =Find_data_in_hash (PHASHTBL, data))) returnFALSE; if(Pnode = = phashtbl->value[data%Ten]) {phashtbl->value[data%Ten] = pnode->Next; Gotofinal; } phead= phashtbl->value[data%Ten];  while(Pnode! = Pheadnext) Phead= phead->Next; Phead->next = pnode->Next;      Final:free (Pnode); returnTRUE; }  

01-(2) Data structure-step-by-step write algorithm (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.