Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #define SIZE 20
- using namespace std;
- struct HashItems {
- int key;
- int value;
- HashItems *next;
- HashItems(int k, int val): key(k), value(val), next(NULL) {};
- };
- struct HashTable {
- HashItems *item;
- };
- HashTable htable[SIZE];
- int get_simple_hash(int key) {
- return key % SIZE;
- }
- void insert(int value) {
- int hashkey = get_simple_hash(value);
- HashTable ht = htable[hashkey];
- HashItems *head = ht.item;
- if(head == NULL) {
- cout<<"No item at this key"<<endl;
- HashItems *firstItem;
- firstItem = new HashItems(hashkey, value);
- head = firstItem;
- ht.item = head;
- htable[hashkey] = ht;
- }
- else {
- cout<< "key already exists: avoid collision"<<endl;
- HashItems *node = head;
- while(node->next != NULL) {
- node = node->next;
- }
- HashItems *nthitem;
- nthitem = new HashItems(hashkey, value);
- node->next = nthitem;
- ht.item = head;
- htable[hashkey] = ht;
- }
- }
- void find(int value) {
- int hashkey = get_simple_hash(value);
- HashTable ht = htable[hashkey];
- HashItems *head = ht.item;
- if(head != NULL) {
- while(head!=NULL) {
- if(head->value == value) {
- cout<<"Found "<<value<<endl;
- return;
- }
- head = head->next;
- }
- }
- else {
- cout<<"not found" <<endl;
- }
- }
- int main() {
- insert(12);
- insert(90);
- insert(77);
- insert(32);
- find(90);
- find(32);
- find(44);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement