Answer the question
In order to leave comments, you need to log in
One-way hash list?
I'm trying to implement chain hashing on a singly directed list. There was a glitch with the creation of the chain, even it doesn’t work, can you tell me?
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;
//Длинна массива владельцев машин
const int ARR_LEN = 100;
//Структура владельцев машин
struct carOwner
{
int carNumber;
string brand;
string ownersName;
};
//Односвязный список из структуры владельцев машин и указателя на следующий элемент
struct Node
{
carOwner owner;
Node* next = nullptr;
};
//Функция создания хеш-числа
int createHash(int number) {
return number % ARR_LEN;
}
//Функция вывода структуры владельцев машин
void carOwnerOut(carOwner co)
{
cout << "Car number: " << co.carNumber << ", car brand: " << co.brand << ", owner's name: " << co.ownersName << endl;
}
Node* findNodeInPosP(Node* L, int p)
{
Node* q = L;
int i = 1;
while (q != 0 && i < p)
{
i++;
q = q->next;
}
if (p < i)
return 0;
return q;
}
void autoInsert(Node* L, carOwner d, int p)
{
Node* q1;
Node* q = new Node;
q->owner = d;
q->next = 0;
if (L)
{
q1 = findNodeInPosP(L, p);
if (q1 != NULL)
{
if (q1->next != NULL)
{
q->next = q1->next;
q1->next = q;
}
else
q1->next = q;
}
}
}
int main()
{
setlocale(LC_ALL, "russian");
srand(time(0));
carOwner arr[ARR_LEN];
int temp;
// Генерируем структуру владельцев машин
for (int a = 0; a < ARR_LEN; a++)
{
temp = rand() % 9999;
while(temp<1000)
temp = rand() % 9999;
arr[a].carNumber = temp;
arr[a].brand = "SomeBrand" + to_string(a + 1);
arr[a].ownersName = "SomeName" + to_string(a + 1);
carOwnerOut(arr[a]);
}
Node hashArr[ARR_LEN];
int hashNumber;
for (int a = 0; a < ARR_LEN; a++)
{
hashArr[a].owner.carNumber = 0;
}
Node hashOwnerTemp;
//Заполняем односвязный хеш-список
for (int a = 0; a < ARR_LEN; a++) {
hashNumber = createHash(arr[a].carNumber);
if (!hashArr[hashNumber].owner.carNumber)
{
hashArr[hashNumber].owner.carNumber = arr[a].carNumber;
hashArr[hashNumber].owner.brand = arr[a].brand;
hashArr[hashNumber].owner.ownersName = arr[a].ownersName;
cout << hashNumber << endl;
}
else
{
hashOwnerTemp = hashArr[hashNumber].next;
while(hashOwnerApp.next!=nullptr)
{
//Тут происходит затык из-за того, что требует тип указатель
hashOwnerTemp = hashOwnerTemp.next;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question