Introduction to Algorithms chapter 11th hash Table 11.1 Direct addressing tables

Source: Internet
Author: User

/* * * * * * * * * Created on:feb ia_11.1directaddresstables.cpp * Author:sunyj */#include <stdint.h> #include <iostream> #include <string.h>//direct-address-search (t, K)//return t[k]//Direct-address-insert (t, x)//    T[x.key] = x//Direct-address-delete (T, x)//T[x.kdy] = Nilclass DataType {public:datatype (): Key (0), data (' + ') {} DataType (int64_t const k, char const D): Key (k), data (d) {} int64_t key;    Key belongs to a small range like [0, 1, 2, 7, 99 ....] char data;}; Class Directaddresstable {public:directaddresstable (int64_t const N): Size (n) {data = new Datatype[n * siz        EOF (DataType)];    memset (data,-1, n * sizeof (DataType)); } DataType Search (int64_t key) {if ( -1 = = Data[key].key) {std::cout << "not found S        Earch failed "<< Std::endl;    } return Data[key]; } void Insert (DataType x) {if ( -1! = Data[x.key].key) {std::cout << "X.key already exist, insert failed" << Std::endl;        return;    } Data[x.key] = x; } void del (DataType x) {if ( -1 = = Data[x.key].key) {std::cout << "X.key not exist,            Delete Failed "<< Std::endl;        return;    } Data[x.key].key =-1;    }private:datatype* data; int64_t size;};    int main () {DataType A (2, ' B ');    DataType Q (3, ' 8 ');    DataType c (, ' Q ');    DataType W (9, ' N ');    Assume there is at the most DataType element, directaddresstable table (100);    Table.insert (a);    Table.insert (q);    Table.insert (c);    DataType tmp;    Tmp.key = 3;    TMP = Table.search (Tmp.key);    Std::cout << tmp.data << Std::endl;    Tmp.key = 4;    TMP = Table.search (Tmp.key);    Std::cout << tmp.data << Std::endl; Table.insert (a);    Insert failed Table.del (a); Table.insert (a);    OK tmp = Table.search (2); Std::cout << tmp.data << std:: Endl; return 0;}

Introduction to Algorithms chapter 11th hash Table 11.1 Direct addressing tables

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.