Assigment on data structure using C++, Assignments of Data Structures and Algorithms

Data structure using C++ assigments are provided by me .This would really be helpful in learning the concepts of the respective language.

Typology: Assignments

2019/2020

Uploaded on 07/23/2020

mayank-raj-5
mayank-raj-5 🇮🇳

5 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Write a C++ program to create a hash table data
structures.
Ans- #include <iostream>
const int T_S = 200;
using namespace std;
struct HashTableEntry {
int v, k;
HashTableEntry *n;
HashTableEntry *p;
HashTableEntry(int k, int v) {
this->k = k;
this->v = v;
this->n = NULL;
}
};
class HashMapTable {
public:
HashTableEntry **ht, **top;
HashMapTable() {
ht = new HashTableEntry*[T_S];
for (int i = 0; i < T_S; i++)
ht[i] = NULL;
}
int HashFunc(int key) {
return key % T_S;
}
void Insert(int k, int v) {
int hash_v = HashFunc(k);
pf3
pf4
pf5

Partial preview of the text

Download Assigment on data structure using C++ and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Write a C++ program to create a hash table data

structures.

Ans- #include

const int T_S = 200; using namespace std; struct HashTableEntry { int v, k; HashTableEntry *n; HashTableEntry *p; HashTableEntry(int k, int v) { this->k = k; this->v = v; this->n = NULL; } }; class HashMapTable { public: HashTableEntry **ht, *top; HashMapTable() { ht = new HashTableEntry[T_S]; for (int i = 0; i < T_S; i++) ht[i] = NULL; } int HashFunc(int key) { return key % T_S; } void Insert(int k, int v) { int hash_v = HashFunc(k);

HashTableEntry* p = NULL; HashTableEntry* en = ht[hash_v]; while (en!= NULL) { p = en; en = en->n; } if (en == NULL) { en = new HashTableEntry(k, v); if (p == NULL) { ht[hash_v] = en; } else { p->n = en; } } else { en->v = v; } } void Remove(int k) { int hash_v = HashFunc(k); HashTableEntry* en = ht[hash_v]; HashTableEntry* p = NULL; if (en == NULL || en->k != k) { cout<<"No Element found at key "<<k<<endl; return; } while (en->n != NULL) { p = en; en = en->n;

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; hash.SearchKey(k); 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; }