哈希表C ++的链表数组

时间:2018-05-11 16:27:03

标签: c++ hash linked-list

我试图将哈希函数h(x) = x % 127实现为链接列表数组。目前我已经创建了一个添加功能,我将新节点添加到链接列表中,但是我不明白为什么没有任何内容写入任何链接列表。我正在读取文件中的输入数字,并将其作为输入%127发送到链接列表。

#include<iostream>
#include<cstdlib>
#include<fstream>
//#define prim 127
using namespace std;

ifstream f("hashuri.in");
ofstream g("hashuri.out");

struct nod {
    int info;
    nod *next;
};

void add_nod(nod *p, nod *&u, int info) {
    if (p == NULL) {
        p = new nod;
        p->info = info;
        u = p;
        u->next = NULL;
    }
    else {
        nod *c = new nod;
        c->info = info;
        u->next = c;
        u = c;
        u->next = NULL;
    }
}

int main()
{
    int nr_op, op, x, prim = 127;
    nod * p[127], *u[127];
    for (int i = 0; i < prim; i++) {
        p[i] = u[i] = NULL;
    }

    f >> nr_op;
    while (!f.eof()) {
        f >> op >> x;
        if (op == 1) {
            //delete_nod(p[x%prim], u[x%prim], x);
        }
        if (op == 2) {
            add_nod(p[x%prim], u[x%prim], x);
        }
        if (op == 3) {
            //search_nod(p[x%prim], u[x%prim], x);
        }
    }

    //printing an array of linked lists
    for (int i = 0; i < prim; i++) {
        if (p[i] != NULL) {
            nod *c;
            c = p[i];
            cout << i << ": ";
            while (c != NULL) {
                cout << c->info << " ";
                c = c->next;
            }
        }
    }

    system("Pause");
    return 0;
}

我的输入文件permutari.in包含:

7
1 3
1 20
2 7
3 4
3 20
2 20
3 20

0 个答案:

没有答案