Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C++

C++ में ओपन एड्रेसिंग लीनियर प्रोबिंग के साथ स्वयं का हैश टेबल लागू करना


हैश टेबल एक डेटा स्ट्रक्चर है जिसका उपयोग की-वैल्यू पेयर्स को स्टोर करने के लिए किया जाता है। हैश फंक्शन का उपयोग हैश टेबल द्वारा एक ऐरे में इंडेक्स की गणना करने के लिए किया जाता है जिसमें एक एलिमेंट इन्सर्ट या सर्च किया जाएगा।

लीनियर प्रोबिंग ओपन एड्रेस्ड हैश टेबल्स में कोलिजन रिजॉल्व करने की एक तकनीक है। इस विधि में, हैश टेबल का प्रत्येक सेल एक सिंगल की-वैल्यू पेयर स्टोर करता है। यदि एक नई की को हैश टेबल के उस सेल पर मैप करके कोलिजन होता है जो पहले से ही किसी अन्य की द्वारा कब्जा किया गया है। यह विधि टेबल में अगली निकटतम फ्री लोकेशन खोजती है और नई की को वहां इन्सर्ट करती है।

यह लीनियर प्रोबिंग के साथ हैश टेबल्स को लागू करने के लिए एक C++ प्रोग्राम है।

एल्गोरिदम

Begin    फंक्शन Insert(int k, int v) डिक्लेयर करें       हैश_वैल, इनिट, डेलइंडेक्स को इंटीजर पॉइंटर डिक्लेयर करें।          hash_val = HashFunc(k) इनिशियलाइज़ करें          init = -1 इनिशियलाइज़ करें          delindex = -1 इनिशियलाइज़ करें       while (hash_val != init and (ht[hash_val]          == DelNode::getNode() or ht[hash_val]          != NULL and ht[hash_val]->k != k))             if (init == -1)                init = hash_val             if (ht[hash_val] == DelNode::getNode())                delindex = hash_val             hash_val = HashFunc(hash_val + 1)       if (ht[hash_val] == NULL or hash_val == init)          if(delindex != -1)             ht[delindex] = new HashTable(k, v)          else             ht[hash_val] = new HashTable(k, v)       if(init != hash_val)          if (ht[hash_val] != DelNode::getNode())             if (ht[hash_val] != NULL)                if (ht[hash_val]->k== k)                   ht[hash_val]->v = v          else             ht[hash_val] = new HashTable(k, v) End.

की वैल्यू खोजने के लिए:

Begin    फंक्शन SearchKey(int k) डिक्लेयर करें       हैश_वैल, इनिट को इंटीजर डेटाटाइप डिक्लेयर करें।          hash_val = HashFunc(k) इनिशियलाइज़ करें          init = -1 इनिशियलाइज़ करें       while (hash_val != init and (ht[hash_val]          == DelNode::getNode() or ht[hash_val]          != NULL and ht[hash_val]->k!= k             if (init == -1)                init = hash_val             hash_val = HashFunc(hash_val + 1)          if (ht[hash_val] == NULL or hash_val == init)             return -1          else             return ht[hash_val]->v End.

डिलीट के लिए:

Begin    फंक्शन Remove(int k) डिक्लेयर करें       हैश_वैल, इनिट को इंटीजर डेटाटाइप डिक्लेयर करें।          hash_val = HashFunc(k) इनिशियलाइज़ करें          init = -1 इनिशियलाइज़ करें       while (hash_val != init and (ht[hash_val]          == DelNode::getNode() or ht[hash_val]          != NULL and ht[hash_val]->k!= k))             if (init == -1)                init = hash_val             hash_val = HashFunc(hash_val + 1)          if (hash_val != init and ht[hash_val] != NULL)             delete ht[hash_val]             ht[hash_val] = DelNode::getNode() End.

उदाहरण कोड

#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; const int T_S = 5; class HashTable {    public:       int k;       int v;       HashTable(int k, int v) {          this->k = k;          this->v = v;       } }; class DelNode:public HashTable {    private:       static DelNode *en;       DelNode():HashTable(-1, -1) {}    public:       static DelNode *getNode() {          if (en == NULL)             en = new DelNode();          return en;       } }; DelNode *DelNode::en = NULL; class HashMapTable {    private:       HashTable **ht;    public:       HashMapTable() {          ht = new HashTable* [T_S];          for (int i = 0; i < T_S; i++) {             ht[i] = NULL;          }       }       int HashFunc(int k) {          return k % T_S;       }       void Insert(int k, int v) {          int hash_val = HashFunc(k);          int init = -1;          int delindex = -1;          while (hash_val != init && (ht[hash_val] == DelNode::getNode() || ht[hash_val] != NULL && ht[hash_val]->k != k)) {             if (init == -1)                init = hash_val;             if (ht[hash_val] == DelNode::getNode())                delindex = hash_val;                hash_val = HashFunc(hash_val + 1);          }          if (ht[hash_val] == NULL || hash_val == init) {             if(delindex != -1)                ht[delindex] = new HashTable(k, v);             else                ht[hash_val] = new HashTable(k, v);          }          if(init != hash_val) {             if (ht[hash_val] != DelNode::getNode()) {                if (ht[hash_val] != NULL) {                   if (ht[hash_val]->k== k)                      ht[hash_val]->v = v;                }             } else                ht[hash_val] = new HashTable(k, v);          }       }       int SearchKey(int k) {          int hash_val = HashFunc(k);          int init = -1;          while (hash_val != init && (ht[hash_val] == DelNode::getNode() || ht[hash_val] != NULL && ht[hash_val]->k!= k)) {             if (init == -1)                init = hash_val;                hash_val = HashFunc(hash_val + 1);          }          if (ht[hash_val] == NULL || hash_val == init)             return -1;          else             return ht[hash_val]->v;       }       void Remove(int k) {          int hash_val = HashFunc(k);          int init = -1;          while (hash_val != init && (ht[hash_val] == DelNode::getNode() || ht[hash_val] != NULL && ht[hash_val]->k!= k)) {             if (init == -1)                init = hash_val;                hash_val = HashFunc(hash_val + 1);          }          if (hash_val != init && ht[hash_val] != NULL) {             delete ht[hash_val];             ht[hash_val] = DelNode::getNode();          }       }       ~HashMapTable() {          delete[] ht;       } }; int main() {    HashMapTable hash;    int k, v;    int c;    while(1) {       cout<<"1.Insert element into the table"<<endl;       cout<<"2.Search element from the key"<<endl;       cout<<"3.Delete element at a key"<<endl;       cout<<"4.Exit"<<endl;       cout<<"Enter your choice: ";       cin>>c;       switch(c) {          case 1:             cout<<"Enter element to be inserted: ";             cin>>v;             cout<<"Enter key at which element to be inserted: ";             cin>>k;             hash.Insert(k, v);          break;          case 2:             cout<<"Enter key of the element to be searched: ";             cin>>k;             if(hash.SearchKey(k) == -1) {                cout<<"No element found at key "<<k<<endl;                continue;             } else {                cout<<"Element at key "<<k<<" : ";                cout<<hash.SearchKey(k)<<endl;             }          break;          case 3:             cout<<"Enter key of the element to be deleted: ";             cin>>k;             hash.Remove(k);          break;          case 4:             exit(1);          default:             cout<<"\nEnter correct option\n";       }    }    return 0; }

आउटपुट

1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 10 Enter key at which element to be inserted: 2 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 7 Enter key at which element to be inserted: 6 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 4 Enter key at which element to be inserted: 5 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 12 Enter key at which element to be inserted: 3 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 15 Enter correct option 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 1 Enter element to be inserted: 15 Enter key at which element to be inserted: 8 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 2 Enter key of the element to be searched: 6 Element at key 6 : 7 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 3 Enter key of the element to be deleted: 2 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 2 Enter key of the element to be searched: 2 No element found at key 2 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 4

  1. C++ में उदाहरण के साथ एक्सप्रेशन ट्री C++ में उदाहरण के साथ एक्सप्रेशन ट्री

    एक एक्सप्रेशन ट्री एक विशेष प्रकार का बाइनरी ट्री है जिसमें ट्री का प्रत्येक नोड या तो एक ऑपरेटर या ऑपरेंड से बना होता है। लीफ नोड्स ट्री के एक ऑपरेंड को दर्शाते हैं। नॉन-लीफ नोड्स ट्री के एक ऑपरेटर को दर्शाते हैं। उदाहरण: इनफिक्स एक्सप्रेशन प्राप्त करने के लिए जिसे आसानी से हल किया जा सकता है, हम

  1. - C++ में की के साथ पर्मुटेशन पर क्वेरी

    मान लीजिए हमारे पास 1 और m के बीच के पॉजिटिव इंटीजर्स का एक ऐरे queries है, हमें सभी क्वेरी को प्रोसेस करना है, queries[i] (i=0 से n तक, n queries का आकार - 1 है) निम्न नियमों के अनुसार:शुरुआत में, हमारे पास पर्मुटेशन P=[1,2,3,...,m] है।वर्तमान i के लिए, पर्मुटेशन P में queries[i] की स्थिति ढूंढें (

  1. C++ में 3n स्लाइस वाली पिज्जा C++ में 3n स्लाइस वाली पिज्जा

    समस्या विवरण मान लीजिए 3n स्लाइस वाली एक पिज्जा है जिसके स्लाइस विभिन्न आकार के हैं। मैं और मेरे दो दोस्त पिज्जा के स्लाइस निम्नानुसार लेंगे: मैं कोई भी पिज्जा स्लाइस चुनूंगा। मेरा दोस्त अमल मेरे चुने हुए स्लाइस के एंटी-क्लॉकवाइज दिशा में अगला स्लाइस लेगा। मेरा दोस्त बिमल मेरे चुने हुए स्लाइस के क