Requisito previo: Introducción al hash , Hashtable usando una lista enlazada individualmente e implementando nuestra propia tabla hash con enstringmiento separado en Java Implementar una tabla hash usando enstringmiento a través de una lista doblemente enlazada es similar a implementar Hashtable usando una lista enlazada individualmente . La única diferencia es que cada Node de la lista enlazada tiene la dirección tanto del Node siguiente como del anterior. Esto acelerará el proceso de agregar y eliminar elementos de la lista, por lo que la complejidad del tiempo se reducirá drásticamente. Ejemplo:
Si tenemos una lista enlazada individualmente:
1->2->3->4Si estamos en 3 y es necesario eliminarlo, entonces 2 debe vincularse con 4 y, a partir de 3, no se puede acceder a 2 ya que es una lista vinculada individualmente. Por lo tanto, la lista debe recorrerse nuevamente, es decir, O (n), pero si tenemos una lista doblemente enlazada, es decir,
1<->2<->3<->4Se puede acceder a 2 y 4 desde 3, por lo tanto, en O(1), 3 se puede eliminar.
A continuación se muestra la implementación del enfoque anterior:
CPP
// C++ implementation of Hashtable // using doubly linked list #include <bits/stdc++.h> using namespace std; const int tablesize = 25; // declaration of node struct hash_node { int val, key; hash_node* next; hash_node* prev; }; // hashmap's declaration class HashMap { public: hash_node **hashtable, **top; // constructor HashMap() { // create a empty hashtable hashtable = new hash_node*[tablesize]; top = new hash_node*[tablesize]; for (int i = 0; i < tablesize; i++) { hashtable[i] = NULL; top[i] = NULL; } } // destructor ~HashMap() { delete[] hashtable; } // hash function definition int HashFunc(int key) { return key % tablesize; } // searching method void find(int key) { // Applying hashFunc to find // index for given key int hash_val = HashFunc(key); bool flag = false; hash_node* entry = hashtable[hash_val]; // if hashtable at that index has some // values stored if (entry != NULL) { while (entry != NULL) { if (entry->key == key) { flag = true; } if (flag) { cout << "Element found at key " << key << ": "; cout << entry->val << endl; } entry = entry->next; } } if (!flag) cout << "No Element found at key " << key << endl; } // removing an element void remove(int key) { // Applying hashFunc to find // index for given key int hash_val = HashFunc(key); hash_node* entry = hashtable[hash_val]; if (entry->key != key || entry == NULL) { cout << "Couldn't find any element at this key " << key << endl; return; } // if some values are present at that key & // traversing the list and removing all values while (entry != NULL) { if (entry->next == NULL) { if (entry->prev == NULL) { hashtable[hash_val] = NULL; top[hash_val] = NULL; delete entry; break; } else { top[hash_val] = entry->prev; top[hash_val]->next = NULL; delete entry; entry = top[hash_val]; } } entry = entry->next; } cout << "Element was successfully removed at the key " << key << endl; } // inserting method void add(int key, int value) { // Applying hashFunc to find // index for given key int hash_val = HashFunc(key); hash_node* entry = hashtable[hash_val]; // if key has no value stored if (entry == NULL) { // creating new node entry = new hash_node; entry->val = value; entry->key = key; entry->next = NULL; entry->prev = NULL; hashtable[hash_val] = entry; top[hash_val] = entry; } // if some values are present else { // traversing till the end of // the list while (entry != NULL) entry = entry->next; // creating the new node entry = new hash_node; entry->val = value; entry->key = key; entry->next = NULL; entry->prev = top[hash_val]; top[hash_val]->next = entry; top[hash_val] = entry; } cout << "Value " << value << " was successfully" " added at key " << key << endl; } }; // Driver Code int main() { HashMap hash; hash.add(4, 5); hash.find(4); hash.remove(4); return 0; }
Java
// Java implementation of Hashtable // using doubly linked list class GFG { static final int tablesize = 25; // declaration of node static class hash_node { int val, key; hash_node next; hash_node prev; } // hashmap's declaration static class HashMap { hash_node hashtable[], top[]; // constructor HashMap() { // create a empty hashtable hashtable = new hash_node[tablesize]; top = new hash_node[tablesize]; for (int i = 0; i < tablesize; i++) { hashtable[i] = null; top[i] = null; } } // hash function definition int HashFunc(int key) { return key % tablesize; } // searching method void find(int key) { // Applying hashFunc to find // index for given key int hash_val = HashFunc(key); boolean flag = false; hash_node entry = hashtable[hash_val]; // if hashtable at that index has some // values stored if (entry != null) { while (entry != null) { if (entry.key == key) { flag = true; } if (flag) { System.out.println( "Element found at key " + key + ": " + entry.val); } entry = entry.next; } } if (!flag) System.out.println( "No Element found at key " + key); } // removing an element void remove(int key) { // Applying hashFunc to find // index for given key int hash_val = HashFunc(key); hash_node entry = hashtable[hash_val]; if (entry.key != key || entry == null) { System.out.println( "Couldn't find any element at this key " + key); return; } // if some values are present at that key & // traversing the list and removing all values while (entry != null) { if (entry.next == null) { if (entry.prev == null) { hashtable[hash_val] = null; top[hash_val] = null; break; } else { top[hash_val] = entry.prev; top[hash_val].next = null; entry = top[hash_val]; } } entry = entry.next; } System.out.println( "Element was successfully removed at the key " + key); } // inserting method void add(int key, int value) { // Applying hashFunc to find // index for given key int hash_val = HashFunc(key); hash_node entry = hashtable[hash_val]; // if key has no value stored if (entry == null) { // creating new node entry = new hash_node(); entry.val = value; entry.key = key; entry.next = null; entry.prev = null; hashtable[hash_val] = entry; top[hash_val] = entry; } // if some values are present else { // traversing till the end of // the list while (entry != null) entry = entry.next; // creating the new node entry = new hash_node(); entry.val = value; entry.key = key; entry.next = null; entry.prev = top[hash_val]; top[hash_val].next = entry; top[hash_val] = entry; } System.out.println( "Value " + value + " was successfully added at key " + key); } } // Driver Code public static void main(String[] args) { HashMap hash = new HashMap(); hash.add(4, 5); hash.find(4); hash.remove(4); } } // This code is contributed by Lovely Jain
Value 5 was successfully added at key 4 Element found at key 4: 5 Element was successfully removed at the key 4
Publicación traducida automáticamente
Artículo escrito por Sanjay Khadda y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA