-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_hashing.cpp
More file actions
114 lines (104 loc) · 1.97 KB
/
open_hashing.cpp
File metadata and controls
114 lines (104 loc) · 1.97 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
const int hs = 10;
Node *hush[hs];
void insert(Node** point, int x){
Node* temp; Node* r;
if (*point == NULL){
temp = new Node;
temp->data = x;
temp->next = NULL;
}
else{
temp = *point;
while ((*point)->next != NULL)
*point = (*point)->next;
r = new Node;
r->data = x;
r->next = NULL;
(*point)->next = r;
}
*point = temp;
}
void display(Node* head){
while (head!=NULL){
cout << head->data << "\n";
head = head->next;
}
}
int key(int num,int s){
return num%s;
}
void hashing(int key,int val){
Node* temp; Node* r;
if (hush[key] == NULL){
temp = new Node;
temp->data = val;
temp->next = NULL;
hush[key] = temp;
}
else{
r = hush[key];
while (hush[key]->next != NULL){
hush[key] = hush[key]->next;
}
temp = new Node;
temp->data = val;
temp->next = NULL;
hush[key]->next = temp;
hush[key] = r;
}
}
void searchHash(int val){
int si = key(val, hs); int flag = 0; int count = 0;
Node *r = hush[si];
while (hush[si] != NULL){
count++;
if (hush[si]->data == val){
flag = 1;
hush[si] = r;
cout << "found" << " after " << count << " iterations\n";
break;
}
hush[si] = hush[si]->next;
}
if (!flag)
cout << "\nnot found " << " after " << count << " iterations\n";
}
void reverse_stack(int val,Node** temp){
Node* t = new Node();
t->data = val;
t->next = (*temp);
(*temp) = t;
}
int main(){
Node* head = NULL;
insert(&head, 4);
insert(&head, 6);
insert(&head, 3);
insert(&head, 7);
cout << "bofore reverse";
display(head);
Node* temp = NULL;
while (head != NULL){
reverse_stack(head->data,&temp);
head = head->next;
}
head = temp;
cout << "after reverse";
display(head);
cout << "Hashing start\n";
int data[] = { 5,1, 8, 2, 3, 0, 10,21,11,28,35,13,6,99,101 };
for (int i = 0; i < 15; i++){
hashing(key(data[i], hs), data[i]);
}
//searchHash(data[1]);
searchHash(31);
//cout << hush[1]->data;
getchar();
return 0;
}