/* * * * * * * * * 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