如何在散列中显示名称以及如何链接列表?

时间:2018-06-04 22:39:16

标签: c++ data-structures hashtable

我在哈希表中创建了11个大小,这包括用户输入,例如1:add,2:remove,3:search和4:display function。 我只对显示功能和链表有问题。

当我添加名为" ben"然后我按数字4这是显示表,它只显示值,而不是字符串或字符。

例如,我将ben的人名添加到哈希表中,然后我显示哈希表,这是我得到的结果。

------------------------------
 Hashing Table
------------------------------
 Index    Value
 0        -2147483648
 1        309
 2        -2147483648
 3        -2147483648
 4        -2147483648
 5        -2147483648
 6        -2147483648
 7        -2147483648
 8        -2147483648
 9        -2147483648
 10       -2147483648
----------------------------
-----------------------------
 Your hash table size is: 11
-----------------------------
 Enter the number below:
 1: Add element
 2: Remove element
 3: Search element
 4: Display all element
 0: Exit

 Your selection:

正如你可以看到上面的调试,索引1保持309值(ben),但这不是我想要的。 我想要下面这样的东西。

------------------------------
 Hashing Table
------------------------------
 Index    Value
 0        -2147483648
 1        ben
 2        -2147483648
 3        -2147483648
 4        -2147483648
 5        -2147483648
 6        -2147483648
 7        -2147483648
 8        -2147483648
 9        -2147483648
 10       -2147483648
----------------------------
-----------------------------
 Your hash table size is: 11
-----------------------------
 Enter the number below:
 1: Add element
 2: Remove element
 3: Search element
 4: Display all element
 0: Exit

 Your selection:

另外,我对如何创建链表非常困惑。我必须添加链表。

这是我真正想要的一个例子,

------------------------------
 Hashing Table
------------------------------
 Index    Value
 0        -2147483648
 1        ben, leon
 2        -2147483648
 3        -2147483648
 4        -2147483648
 5        -2147483648
 6        jame
 7        -2147483648
 8        -2147483648
 9        -2147483648
 10       -2147483648
----------------------------
-----------------------------
 Your hash table size is: 11
-----------------------------
 Enter the number below:
 1: Add element
 2: Remove element
 3: Search element
 4: Display all element
 0: Exit

 Your selection:

正如您可以看到上面的示例调试,我添加ben进入索引1。 另外,如果我添加leon,这应该进入索引1但是没有空间所以我 必须创建一个链表才能在索引1中添加leon。

这是我的代码,如果有人帮忙,我真的很感激。

#include "stdafx.h"
#include<iostream>
#include<limits.h>
#include "string"

using namespace std;

void DisplayHashTable(int Array1[], int Size1)
{
    system("cls"); /* <-- See Line 192 to 197 for detail.*/
    int i;
    cout << "------------------------------\n";
    cout << " Hashing Table\n";
    cout << "------------------------------\n";
    cout << " Index \t  Value\n";

    for (i = 0; i<Size1; i++)
        cout << " " << i << "\t  " << Array1[i] << "\n";
    cout << "----------------------------\n";
}

void LookUpHashTable(int Array2[], int HashFunction2, int Size2)
{
    system("cls");
    int Position2, a, ASCIIsum2 = 0, n = 0;
    string Element2;

    cout << " Enter element or name you want to search: ";
    cin >> Element2;

    for (a = 0; a != Element2.length(); ++a)
    {
        ASCIIsum2 += int(Element2[a]);
    }

    Position2 = ASCIIsum2 % HashFunction2;

    while (n++ != Size2)
    {
        if (Array2[Position2] == ASCIIsum2) {
            cout << "\n###################################\n";
            cout << " NOTE: " << Element2 << " value number is " << ASCIIsum2 << ".\n Element or name found at index " << Position2 << "\n";
            cout << "###################################\n\n";
            break;
        }
        else
            if (Array2[Position2] == INT_MAX || Array2[Position2] != INT_MIN)
                Position2 = (Position2 + 1) % HashFunction2;
    }

    if (--n == Size2)
    {
        cout << "\n################################################\n";
        cout << " NOTE: Element or name not found in hash table\n";
        cout << "################################################\n\n";
    }
}


void AddElementHashTable(int Array3[], int HashFunction3, int Size3)
{
    system("cls");
    int  Position3, a, ASCIIsum3 = 0, n = 0;
    string Element3;

    cout << " Enter new element or name: ";
    cin >> Element3;

    for (a = 0; a != Element3.length(); ++a)
    {
        ASCIIsum3 += int(Element3[a]);
    }

    Position3 = ASCIIsum3 % HashFunction3;

    while (Array3[Position3] != INT_MIN) {  // INT_MIN and INT_MAX indicates that cell is empty. So if cell is empty loop will break and goto bottom of the loop to insert element
        if (Array3[Position3] == INT_MAX)
            break;
        Position3 = (Position3 + 1) % HashFunction3;
        n++;

        if (n == Size3)
            break;      // If table is full we should break, if not check this, loop will go to infinite loop.
    }

    if (n == Size3)
    {
        cout << "\n##############################################\n";
        cout << " NOTE: Hash table is full of elements or name\n No Place to insert this element or name\n";
        cout << "##############################################\n\n";
    }
    else
    {
        Array3[Position3] = ASCIIsum3;    //Inserting element
        cout << "\n===========================================\n";
        cout << " NOTE: " << Element3 << " value number is " << ASCIIsum3 << ".\n Element or name added successfully\n";
        cout << "===========================================\n";
    }
}


void RemoveElementHashTable(int Array4[], int HashFunction4, int Size4)
{
    system("cls");
    int Position4, a, ASCIIsum4 = 0, n = 0;

    string Element4;

    cout << " Enter element or name to remove: ";
    cin >> Element4;

    for (a = 0; a != Element4.length(); ++a)
    {
        ASCIIsum4 += int(Element4[a]);
    }

    Position4 = ASCIIsum4 % HashFunction4;

    while (n++ != Size4) {
        if (Array4[Position4] == INT_MIN) {
            cout << "\n################################################\n";
            cout << " NOTE: Element or name not found in hash table\n";
            cout << "################################################\n\n";
            break;
        }
        else if (Array4[Position4] == ASCIIsum4) {
            Array4[Position4] = INT_MAX;
            cout << "\n===========================================\n";
            cout << " NOTE: " << Element4 << " value number is " << ASCIIsum4 << ".\n Element removed successfully\n";
            cout << "===========================================\n";
            break;
        }
        else {
            Position4 = (Position4 + 1) % HashFunction4;
        }
    }
    if (--n == Size4)
    {
        cout << "\n################################################\n";
        cout << " NOTE: Element or name not found in hash table\n";
        cout << "################################################\n\n";
    }
}

int main() {
    int Size5, HashFunction5, i, UserInput;

    Size5 = 11;
    HashFunction5 = 11;

    int Array5[11];
    system("cls");

    for (i = 0; i < Size5; i++)
        Array5[i] = INT_MIN; //Assigning INT_MIN indicates that cell is empty

    do {
        cout << "-----------------------------\n";
        cout << " Your hash table size is: " << Size5 << "\n";
        cout << "-----------------------------\n";
        cout << " Enter the number below:\n";
        cout << " 1: Add element\n";
        cout << " 2: Remove element\n";
        cout << " 3: Search element\n";
        cout << " 4: Display all element\n"; /* <-- See Line 192 to 197 for detail.*/
        cout << " 0: Exit\n";
        cout << " \n Your selection: ";
        cin >> UserInput;

        switch (UserInput) 
        {
            case 0:
                system("cls");
                cout << " You quit the application\n\n";
                system("pause");
                exit;
                break;
            case 1:
                AddElementHashTable(Array5, HashFunction5, Size5);
                break;
            case 2:
                RemoveElementHashTable(Array5, HashFunction5, Size5);
                break;
            case 3:
                LookUpHashTable(Array5, HashFunction5, Size5);
                break;
            case 4:                                 
                DisplayHashTable(Array5, Size5);    
                break;                              
            default:
                cout << " \nYou entering incorrect choice. Please enter choice above!\n\n";
                system("pause");
                system("cls");
                break;
        }

    } while (UserInput);

    return 0;
}

0 个答案:

没有答案