Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <vector>
- using namespace std;
- int hash_(string &c){
- int h = 0;
- for(int i= 0 ; i <= c.length() ; i++){
- h = h + int(c[i]);
- }
- return h;
- }
- int hash_small(string &c){
- return (c.length() % 5)-1;
- }
- struct node{
- node* next;
- string data;
- };
- void insert(node* hash_table[], string data){
- node* nw = new node;
- nw->data = data;
- nw->next = NULL;
- int index = hash_small(data);
- cout << "index = " << index << endl;
- if(hash_table[index] == NULL){
- hash_table[index] = nw;
- cout << "Inserted the data = " << data << endl;
- }else{
- node* temp = hash_table[index];
- while(true){
- if(temp->next == NULL) break;
- temp = temp->next;
- }
- temp->next = nw;
- cout << "The chain is inserted " << endl;
- }
- }
- void print(node* hash_table[], int n){
- for(int i=0; i<n; i++){
- if(hash_table[i]!=NULL) {
- node* item = hash_table[i];
- while(true){
- cout << setw(10) << item->data ;
- if(item->next == NULL) break;
- item = item->next;
- }
- cout << endl;
- }
- }
- }
- int main(){
- string a = "ajay";
- cout << hash_(a) << endl;
- string a1 = "ajay";
- cout << hash_(a1) << endl;
- a = "a";
- cout << hash_(a) << endl;
- a = "a";
- cout << hash_(a) << endl;
- a = "z";
- cout << hash_(a) << endl;
- // insertions
- node* hash_table[100] ;
- insert(hash_table, "ajay");
- insert(hash_table, "avi");
- insert(hash_table, "ajayf");
- insert(hash_table, "ajaywe");
- insert(hash_table, "ajayrw");
- insert(hash_table, "ajaewy");
- insert(hash_table, "ajayewer");
- insert(hash_table, "ajay");
- print(hash_table, 5);
- }
- /** output
- 421
- 421
- 97
- 97
- 122
- index = 3
- Inserted the data = ajay
- index = 2
- Inserted the data = avi
- index = -1
- Inserted the data = ajayf
- index = 0
- Inserted the data = ajaywe
- index = 0
- The chain is inserted
- index = 0
- The chain is inserted
- index = 2
- The chain is inserted
- index = 3
- The chain is inserted
- ajaywe ajayrw ajaewy
- avi ajayewer
- ajay ajay
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement