Introduction to Algorithms 11.2 Hash list Hash tables chain method to solve collision

Source: Internet
Author: User

/* * ia_11.2chainedhash.cpp * * Created on:feb, * author:sunyj */#include <stdint.h> #include <ios tream> #include <string.h>//chained-hash-insert (T, x)//INSERT X at the head of List t[h (X.key)]//chained-hash-s Earch (t, K)//search for a element with key K in list T[h (k)]//Chained-hash-delete (t, x)//DELETE x from the list t[h (x. Key)]class ListNode {public:listnode (): Key (0), data (0), prev (nullptr), Next (nullptr) {std::cout << Lis    Tnode () "<< Std::endl;    } listnode (int64_t const K, int64_t D): Key (k), data (d), Prev (nullptr), Next (nullptr) {} int64_t key;    int64_t data;    listnode* prev; listnode* next;};/ /List-search (l, K)//x = l.nil.next//while (x! = L.nil and X.key! = k)//x = x.next//return x//list-insert (l, x)/  /X.next = l.nil.next//L.nil.next.prev = x//L.nil.next = x//X.prev = l.nil//list-delete (L, x)//X.prev.next = x.next// X.next.prev = X.prevclass LinkedList {public:linkedlist (): Nil (&m_nil) {std::cout << "LinkedList ()" << Std::endl;        Nil->prev = nil;        Nil->next = nil; Nil->data =-1;        } listnode* Search (int64_t const k) {listnode* x = nil->next;        while (x! = Nil && k! = X->key) {x = x->next;    } return x;        } void Insert (listnode* x) {x->next = nil->next;        Nil->next->prev = x;        Nil->next = x;    X->prev = nil;        } void del (listnode* x) {x->prev->next = x->next;    X->next->prev = x->prev;        } void Print () {listnode* x = nil->next;            while (nil! = x) {std::cout << x->key << "";        x = x->next;    } std::cout << Std::endl; }private:listnode M_nil;    Empty list has Noe node, pointer nill points to it. listnode* Nil;}; Class Chainedhashtable {PUBlic:chainedhashtable (int64_t const N): Size (n) {data = new Linkedlist[n] ();    } ~chainedhashtable () {} int64_t Hashfunc (int64_t const key) {return key% size;    } listnode* Search (int64_t const key) {return Data[hashfunc (key)].search (key);    } void Insert (listnode* x) {(Data[hashfunc (X->key)]). Insert (x);    } void del (listnode* x) {data[hashfunc (X->key)].del (x);    } void Print (int64_t key) {Data[hashfunc (key)].print ();    }private:linkedlist* data; int64_t size;}; int main () {linkedlist a;/* * a prime not too close to an exact power of 2 are often a good choice for M. Forexample, Suppos  E We wish to allocate a hash table, with collisions resolved bychaining, to-hold roughly n = character strings, where A character has 8 bits. We don ' t mind examining an average of 3 elements in an unsuccessful search, ANDSO we allocate a hash table of size M = 701 . We could choose 701 Becauseit is a prime near 2000=3 butNot near any power of 2. */chainedhashtable table (701);    The Division method, ListNode Node1 (1, 100);    ListNode Node4 (4, 400);    ListNode Node16 (16, 1600);    ListNode Node9 (9, 900);    Table.insert (&AMP;NODE1);    Table.insert (&AMP;NODE4);    Table.insert (&AMP;NODE16);    Table.insert (&AMP;NODE9);    Table.print (4);    ListNode Node25 (25, 2500);    Table.insert (&AMP;NODE25);    Table.print (16);    Table.del (&AMP;NODE1);    Table.print (9);    listnode* tmp;    TMP = Table.search (9);    Table.del (TMP);    Table.print (9); return 0;}

Introduction to Algorithms 11.2 Hash list Hash tables chain method to solve collision

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.