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

C++ में लिस्ट हेड्स के साथ हैश टेबल्स चेनिंग का कार्यान्वयन


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

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

एल्गोरिथम

सम्मिलन के लिए:

Begin    Declare function Insert(int k, int v)       int hash_v = HashFunc(k)       if (ht[hash_v] == NULL)          ht[hash_v] = new ListHead(k, v)       else          ListHead *en = ht[hash_v]          while (en->n != NULL)             en = en->n          if (en->k == k)             en->v = v          else             en->n= new ListHead(k, v) End.

कुंजी मूल्य खोजने के लिए:

Begin    Decla Function SearchKey(int k)       int hash_v = HashFunc(k)       if (ht[hash_v] == NULL)          return -1       else          ListHead *en = ht[hash_v]          while (en != NULL and en->k != k)             en= en->n          if (en== NULL)             return -1          else             return en->v End

हटाने के लिए:

Begin    Declare Function Remove(int k)       int hash_v = HashFunc(k)       if (ht[hash_v] != NULL)          ListHead *en = ht[hash_v];          ListHead *p= NULL;          while (en->n != NULL and en->k != k)             p = en             en = en->n          if (en->k== k)             if (p == NULL)                ListHead *n= en->n                delete en;                ht[hash_v] = n             else                ListHead *n= en->n                delete en                p->n = n End.


उदाहरण कोड

#include <iostream> using namespace std; const int T_S = 20; class ListHead {    public:       int k, v;       ListHead *n;       ListHead(int k, int v) {          this->k = k;          this->v = v;          this->n = NULL;       } }; class HashMapTable {    private:       ListHead **ht;    public:       HashMapTable() {          ht = new ListHead*[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_v = HashFunc(k);          if (ht[hash_v] == NULL)             ht[hash_v] = new ListHead(k, v);          else {             ListHead *en = ht[hash_v];             while (en->n != NULL)                en = en->n;             if (en->k == k)                en->v = v;             else                en->n= new ListHead(k, v);          }       }       int SearchKey(int k) {          int hash_v = HashFunc(k);          if (ht[hash_v] == NULL)             return -1;          else {             ListHead *en = ht[hash_v];             while (en != NULL && en->k != k)                en= en->n;             if (en == NULL)                return -1;             else                return en->v;          }       }       void Remove(int k) {          int hash_v = HashFunc(k);          if (ht[hash_v] != NULL) {             ListHead *en = ht[hash_v];             ListHead *p = NULL;             while (en->n != NULL && en->k != k) {                p = en;                en = en->n;             }             if (en->k == k) {                if (p == NULL) {                   ListHead *n= en->n;                   delete en;                   ht[hash_v] = n;                }                else {                   ListHead *n = en->n;                   delete en;                   p->n = n;                }             }          }       }       ~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;             else {                cout<<"Elements at key "<<k<<" : ";                cout<<hash.SearchKey(k)<<endl;             }          break;          case 3:             cout<<"Enter key of the element to be deleted: ";             cin>>k;             if (hash.SearchKey(k) == -1)                cout<<"Key "<<k<<" is empty"<<endl;             else {                hash.Remove(k);                cout<<"Entry Removed"<<endl;             }          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: 1 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: 10 Enter key at which element to be inserted: 1 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: 12 Enter key at which element to be inserted: 4 1.Insert element into the table 2.Search element from the key 3.Delete element at a key 4.Exit Enter your choice: 30 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: 30 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: 2 Enter key of the element to be searched: 6 Elements 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: 1 Entry Removed 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 Elements 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: 4



  1. C++ प्रोग्राम: एडजेसेंसी लिस्ट इम्प्लीमेंट करना C++ प्रोग्राम: एडजेसेंसी लिस्ट इम्प्लीमेंट करना

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

  1. - C++ प्रोग्राम: सर्कुलर सिंगली लिंक्ड लिस्ट कार्यान्वयन

    सर्कुलर सिंगली लिंक्ड लिस्ट अंतिम नोड पहले नोड (head) को इंगित करता है, जिससे एक वृत्त बनता है। केवल head संदर्भ से पूरी सूची एक्सेस की जा सकती है। C++ प्रोग्राम उदाहरण next; } while (ptr != head); } int main() { insert(3); insert(1); insert(7); insert(2); insert(9); c

  1. - C++ प्रोग्राम: सिंगली लिंक्ड लिस्ट कार्यान्वयन

    सिंगली लिंक्ड लिस्ट प्रत्येक नोड में डेटा और अगले नोड का संदर्भ होता है। अंतिम नोड NULL इंगित करता है। केवल head से पूरी सूची एक्सेस की जा सकती है। C++ प्रोग्राम उदाहरण next; } } int main() { insert(3); insert(1); insert(7); insert(2); insert(9); cout << The link