-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew Hashing Function.cpp
More file actions
144 lines (128 loc) · 2.81 KB
/
Copy pathNew Hashing Function.cpp
File metadata and controls
144 lines (128 loc) · 2.81 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<iostream>
using namespace std;
const int SIZE=10;
struct Node {
int data;
Node *next;
};
Node* Array[SIZE];
void insertNode(Node *&head, int val) {
Node *node = new Node;
node->data=val;
node->next = NULL;
if(head == NULL) {
head=node;
return;
}
Node *current = head;
while(current->next != NULL) {
current=current->next;
}
current->next=node;
}
void insertHash(int val) {
int hashf=val % SIZE;
Node *HEAD= Array[hashf];
insertNode(HEAD,val);
Array[hashf]=HEAD;
}
void displayHash() {
for(int i=0; i<SIZE; i++) {
cout<<"For i = "<<i<<" : ";
Node *Head=Array[i];
while(Head != NULL) {
cout<<Head->data<<" ";
Head=Head->next;
}
cout<<endl;
}
}
void deleteHash(int item) {
int hashfunc=item % SIZE;
Node *Head= Array[hashfunc];
if(Head == NULL) {
cout<<"Hash Table is empty! So deletion is not possible"<<endl;
return;
}
if(Head->data == item) {
Head = Head->next;
Array[hashfunc]=Head;
cout<<"Element deleted successfully."<<endl;
return;
}
Node *current=Head->next;
Node *pred= Head;
while(current != NULL) {
if(current->data == item){
pred->next = current->next;
cout<<"Element deleted successfully."<<endl;
return;
}
pred=current;
current=current->next;
}
cout<<"Sorry! element not found."<<endl;
return;
}
void searchHash(int item) {
bool check=true;
for(int a=0; a<SIZE; a++) {
if(Array[a] != NULL) {
check = false;
break;
}
}
if(check == true) {
cout<<"Hash Table is empty"<<endl;
return;
}
int hashfu= item % SIZE;
Node *HeaD= Array[hashfu];
while (HeaD != NULL) {
if(HeaD->data == item) {
cout<<"Element is found at Location/Address: "<<HeaD<<endl;
return;
}
HeaD = HeaD->next;
}
cout<<"Unsuccessful Search! Element is not found! "<<endl;
return;
}
int main() {
int value=0, option=0;
do {
cout<<"====== Select a choice from below ======"<<endl;
cout<<"Enter 0. For Exit: "<<endl;
cout<<"Enter 1. For Display Hash Table: "<<endl;
cout<<"Enter 2. For Insertion in Hash Table: "<<endl;
cout<<"Enter 3. For Delete an element from Hash Table: "<<endl;
cout<<"Enter 4. For Search an element in Hash Table: "<<endl;
cout<<"\nEnter a choice: ";
cin>>option;
switch(option) {
case 0:
cout<<"Quit"<<endl;
break;
case 1:
displayHash();
break;
case 2:
cout<<"Enter value: ";
cin>>value;
insertHash(value);
break;
case 3:
cout<<"Enter value to delete: ";
cin>>value;
deleteHash(value);
break;
case 4:
cout<<"Enter value to search: ";
cin>>value;
searchHash(value);
break;
default: cout<<"Please enter a valid choice!"<<endl;
}
} while(option != 0);
return 0;
}