Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct node {
- int index;
- string name;
- node* next;
- };
- class HashTable {
- public:
- node* table;
- int size=1000;
- HashTable() {
- table = new node[size];
- }
- int hash_function(int key) {
- return key % 1000;
- }
- void add(int key, string name) {
- int index = hash_function(key);
- node* temp = new node;
- temp->index = key;
- temp->name = name;
- if(table[index].next == NULL) {
- table[index].next = temp;
- } else {
- node* temp2 = table[index].next;
- while(temp2->next != NULL) {
- temp2 = temp2->next;
- }
- temp2->next = temp;
- }
- }
- node get(int key,string name) {
- int index = hash_function(key);
- node* temp = table[index].next;
- while(temp != NULL) {
- temp = temp->next;
- if(temp->name== name) {
- return *temp;
- }
- }
- node nn = {-1, "NOT FOUND", NULL};
- return nn;
- }
- };
- void print_node(node n) {
- cout << n.index << " " << n.name << endl;
- }
- int main()
- {
- HashTable ht;
- ht.add(1, "John");
- ht.add(1, "Jane");
- ht.add(1, "Jack");
- ht.add(4, "Jill");
- print_node(ht.get(1, "Jack"));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement