Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7030727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:42:36+00:00 2026-05-28T00:42:36+00:00

I programmed the below code to implement HashTable by Chains algorithm, but when i

  • 0

I programmed the below code to implement HashTable by Chains algorithm, but when i use it in main function, the result is wrong :

#define _SIZE 1000

class HashEntry
{
    public: int Tel;
            string Name;

    public: HashEntry(){}
    public: HashEntry(int Tel, string Name)
    {
        this->Tel = Tel;
        this->Name = Name;
    }
};

class Link {
    public:
    HashEntry data;
        Link *next;

    public:
        Link() : next(NULL){ }

};

void Insert (Link *head, HashEntry data)
{
    Link *t= new Link;
    t->data=data;

    if(head==NULL)
    {
        head = new Link;
        head->next = NULL;
        head->data = data;
        return;
    }

    Link *head2= head;
    while(head2->next!=NULL)
    {     
        if( head2->data.Name == data.Name)
            return;
        head2=head2->next;
    }

    head2->next=t;
}

class  HashTable {
    public:
    Link **a;
    public:
    HashTable()
        {
            a = new Link *[_SIZE];
            for (int i=0; i<_SIZE; i++)
                a[i]=NULL;
        }

    public:
        int HashFonction (string key)
        {
            int res=0;
            for ( int i = 0; i < key.length(); i ++ )
            {
                char ch = key.at(i);
                if( i%2==0)
                    res+=(int)ch;
                else 
                    res-=(int)ch;
            }

            return (int)fabsf((float)res) % _SIZE;
        }

        void HashInsert (string key, int val){
            int index= HashFonction(key);
            HashEntry s(val, key);
            Insert(a[index],s);
        }    

        int HashSearch(string key)
        {
            int inx=HashFonction(key);
            Link *head=a[inx];

            if(head==NULL){
                return -1;
            }

            while(head!=NULL){
                if(head->data.Name==key)
                    return head->data.Tel;

                head=head->next;
            }

            return -1;
        }
};

After implement this class, i programmed following codes but the result of Search is -1 :-/

HashTable ht;
ht.HashInsert("Hossein", 849348);
ht.HashInsert("Ali", 94343);
ht.HashInsert("Fatemeh", 940343);

cout << ht.HashSearch("Ali") << endl; // output = -1 :-/

Could anyone please explain what is wrong ?

Thanks for your attention

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:42:37+00:00Added an answer on May 28, 2026 at 12:42 am

    You are passing pointer head to function Insert by value, so array “a” in class Hash table is not altered. Passing by reference fixes the problem:

    void Insert (Link *&head, HashEntry data)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use this code to define the APIs that are needed
I have never programmed a gadget for Vista or Seven, but I would like
I have programmed an emulator, but I have some doubts about how to organizate
The code below is really annoying me I have looked on stackoverflow and google
Where is the proper place to perform validation given the following scenario/code below: In
I was wondering why the code below only returns Test four times, instead of
Below is some code from a C++ procedure. The original can be found here;
consider the code below, Am trying to make a Sparse_matrix that contains an array
The code below allows a user to enter user name and password to log
I'm a rather experience C++ programmer, but with (pure!?) C-code I am sometimes struggling,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.