0% found this document useful (0 votes)
51 views3 pages

Assignment No 1 DSA

The document describes an implementation of a hash table in C++ to store contact information. It defines a struct called hashTable to store key-value pairs, with pointers to handle collisions. A Contacts class initializes an array of hashTable, allows insertion via hashing the key, and searching by key. Main tests it by inserting 4 contacts and searching 2 by name.

Uploaded by

Tejas Bhor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views3 pages

Assignment No 1 DSA

The document describes an implementation of a hash table in C++ to store contact information. It defines a struct called hashTable to store key-value pairs, with pointers to handle collisions. A Contacts class initializes an array of hashTable, allows insertion via hashing the key, and searching by key. Main tests it by inserting 4 contacts and searching 2 by name.

Uploaded by

Tejas Bhor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment No.

1
Title: Implementation of hash Table.
Program:
#include<iostream>
#include<cstring>
using namespace std;
struct hashTable{
    string key;
    int64_t value;
    bool isEmpty=true;
    hashTable *next;
};
class Contacts{
    int num;        //number of entries
    hashTable *phoneBook;
public:
    Contacts():phoneBook(NULL){
  }
    void ins(){
        int index;
        int64_t phoneNumber;
        string name;
        cout<<"enter number of entries: ";
        cin>>num;
        phoneBook=new hashTable[num];
        cout<<"Enter name and phone number respectively:"<<endl;
        for(int i=0;i<num;i++){
            cin>>name>>phoneNumber;
            index=hashing(name,num);
            if(phoneBook[index].isEmpty){        //if the slot is empty(no collision)...
                phoneBook[index].key=name;
                phoneBook[index].value=phoneNumber;
                phoneBook[index].isEmpty=false;     //it is now filled...
                phoneBook[index].next=NULL;
      }
            else{           //in case of collision...
                hashTable *temp=&(phoneBook[index]);
                while(temp->next!=NULL){
                    temp=temp->next;
        }
                temp->next=new hashTable;
                temp->next->key=name;
                temp->next->value=phoneNumber;
                temp->next->isEmpty=false;
                temp->next->next=NULL;
      }
    }
  }
    int hashing(string name,int n){
        int len,sum=0;
        //converting string into char array...
        len=name.length();
        char temp[len+1];
        strcpy(temp,name.c_str());
        //calculating hash value...
        for(int i=0;i<len;i++){
            sum=sum+temp[i];
    }
        return (sum%n);
  }
    void searchContact(string name){
        int check;
        check=hashing(name,num);

        if(phoneBook[check].next==NULL){        //if there is no collision seen in the current block..


            if(phoneBook[check].key==name){
                cout<<"Name: "<<phoneBook[check].key<<endl;
                cout<<"Phone number: "<<phoneBook[check].value<<endl;
      }
            else{
                cout<<"Not found"<<endl;
      }
    }

        else{                                   //if collision is seen in the current block...


            hashTable *temp=&(phoneBook[check]);
            while(temp!=NULL){
                if(temp->key==name){
                    cout<<"Name: "<<temp->key<<endl;
                    cout<<"Phone number: "<<temp->value<<endl;
                    break;
        }
                else
                    temp=temp->next;
      }
            if(temp==NULL)
                cout<<"Not found"<<endl;
    }
  }
    ~Contacts(){
        delete []phoneBook;
  }
};
int main(){
    string name;
    int n;
    Contacts d1;
    d1.ins();
    cout<<"Enter number of names you want to search: ";
    cin>>n;
    cout<<"Enter names: "<<endl<<endl;
    for(int i=1;i<=n;i++){
        cin>>name;
        d1.searchContact(name);
        cout<<endl;
  }
}

Output:
PS C:\Users\Tejas> cd "C:\Users\Tejas\AppData\Local\Temp\" ; if ($?) { g++
tempCodeRunnerFile.cpp -o tempCodeRunnerFile } ; if ($?) { .\tempCodeRunnerFile }
enter number of entries: 4
Enter name and phone number respectively:
Tejas 9730775000
Nehal 9420880000
Viraj 9074570000
Keshav 9657540000
Enter number of names you want to search: 2
Enter names:
Nehal
Name: Nehal
Phone number: 9420880000

Tejas
Name: Tejas
Phone number: 9730775000

You might also like