forked from ShivamDubey7/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 315
/
Copy pathChaining
79 lines (67 loc) · 1.12 KB
/
Chaining
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
using namespace std;
struct node
{
node *next;
int data;
};
node* create(int data)
{
node* newnode = new node;
newnode->next = NULL;
newnode->data = data;
return newnode;
}
void display(node* current, int index) {
cout << "Data of Index: " << index << endl;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
int hashfun(int y, int size)
{
int m = y % size;
return m;
}
node **head;
int main()
{
int size, size1, index;
int data;
cout << "Enter size of array:" << endl;
cin >> size;
head = new node *[size];
for (int i = 0; i < size; i++)
{
head[i] = nullptr;
}
cout << "How Much Data You Want to Enter\n";
cin >> size1;
for (int i = 0; i < size1; i++)
{
cout << "Enter Data to Store: ";
cin >> data;
index = hashfun(data, size);
cout << index;
if (head[index] == NULL)
{
head[index] = create(data);
}
else
{
node *temp = head[index];
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = create(data);
}
}
for (int i = 0; i < size; i++)
{
display(head[i], i);
}
system("pause");
}