算法导论第11章散列表11.1直接寻址表
2015-02-11 19:58
239 查看
一种动态集合结构,只支持,插入,查找,删除三种操作。
直接寻址:
在空间允许的情况下,提供一个数组为每个可能的关键字保留一个位置。
首先直接寻址技术不是散列表的一种,它是类似数组的一种数据结构,第11章,名字叫散列表三个字对应Hash Tables
散列表比直接存储的好处是,节省空间
直接寻址:
在空间允许的情况下,提供一个数组为每个可能的关键字保留一个位置。
首先直接寻址技术不是散列表的一种,它是类似数组的一种数据结构,第11章,名字叫散列表三个字对应Hash Tables
散列表比直接存储的好处是,节省空间
/* * IA_11.1DirectAddressTables.cpp * * Created on: Feb 11, 2015 * 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] = NIL template <class Type> class ElemType { public: ElemType() : key(0), data('\0') { } ElemType(int64_t const k, Type const d) : key(k), data(d) { } int64_t key; // key belongs to a small range like [0, 1, 2, 7, 99.... ] Type data; }; template <class Type> class DirectAddressTable { public: DirectAddressTable(int64_t const n) : size(n) { data = new ElemType<Type> ; memset(data, -1, n * sizeof(ElemType<char>)); } ~DirectAddressTable() { delete [] data; } ElemType<Type> search(int64_t const key) { if (-1 == data[key].key) { std::cout << "not found search failed" << std::endl; } return data[key]; } void insert(ElemType<Type> x) { if (-1 != data[x.key].key) { std::cout << "x.key already exist, insert failed" << std::endl; return ; } data[x.key] = x; } void del(ElemType<Type> x) { if (-1 == data[x.key].key) { std::cout << "x.key not exist, delete failed" << std::endl; return ; } data[x.key].key = -1; } private: ElemType<Type>* data; int64_t size; }; int main() { ElemType<char> a(2, 'b'); ElemType<char> q(3, '8'); ElemType<char> c(22, 'q'); ElemType<char> w(9, 'N'); // Assume there are at most 100 ElemType element, DirectAddressTable<char> table(100); table.insert(a); table.insert(q); table.insert(c); ElemType<char> 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; }
相关文章推荐
- 《算法导论》笔记 第11章 11.1 直接寻址表
- 算法导论-第11章-散列表-11.1-4 大数组的直接寻址表
- 《算法导论》笔记 第11章 11.1 直接寻址表
- 《算法导论》第11章 散列表 (1)直接寻址表
- 《算法导论》第11章 散列表 (1)直接寻址表
- 算法导论 11.1-4 大数组的直接寻址表
- 算法导论11.1直接寻址表 练习总结
- 《算法导论》笔记 第11章 11.5 完全散列
- 《算法导论》笔记 第11章 11.5 完全散列
- 《算法导论》第11章 散列表 (2)散列表
- 《算法导论》第11章 散列表 (2)散列表
- 《算法导论》第11章 散列表 (1)直接寻址表
- 《算法导论》第11章 散列表 (2)散列表
- 算法导论 11.1-4 大数组的直接寻址表
- 《算法导论》第11章 散列表 (1)直接寻址表
- 《算法导论》第11章 散列表 (2)散列表
- 《算法导论》11、散列表相关知识
- 算法导论 散列表 11.1答案以及乘法散列法
- 算法导论第十一章习题11.1-4
- 算法导论第十一(11)章散列(Hash)表知识点梳理及详细课后答案