1
+ //
1
2
#include < iostream>
2
3
#include < math.h>
3
4
using namespace std ;
4
- struct node {
5
+
6
+ struct Node {
5
7
int data;
6
- struct node *next;
8
+ struct Node *next;
7
9
} *head[100 ],*curr;
8
10
9
- void init (){
11
+ void init () {
10
12
for (int i=0 ;i<100 ;i++)
11
13
head[i]=NULL ;
12
14
}
13
15
14
- void add (int x,int h){
15
- struct node *temp = new node ;
16
+ void add (int x,int h) {
17
+ struct Node *temp = new Node ;
16
18
temp->data = x;
17
19
temp->next = NULL ;
18
- if (head[h]== NULL ) {
20
+ if (! head[h]) {
19
21
head[h] = temp;
20
- curr= head[h];
22
+ curr = head[h];
21
23
}
22
- else {
24
+ else {
23
25
curr=head[h];
24
- while (curr->next != NULL )
25
- curr= curr->next ;
26
+ while (curr->next )
27
+ curr = curr->next ;
26
28
curr->next = temp;
27
29
}
28
30
}
29
31
30
- void display (int mod){
31
- struct node *temp;
32
+ void display (int mod) {
33
+ struct Node *temp;
32
34
int i;
33
- for (i=0 ;i<mod;i++){
34
- if (head[i]== NULL ) {
35
- cout<<" Key = " <<i<<" is empty" <<endl;
35
+ for (i=0 ;i<mod;i++) {
36
+ if (! head[i]) {
37
+ cout<<" Key " <<i<<" is empty" <<endl;
36
38
}
37
- else {
38
- cout<<" Key = " <<i<<" Value = " ;
39
+ else {
40
+ cout<<" Key " <<i<<" has values = " ;
39
41
temp = head[i];
40
- while (temp->next != NULL ) {
42
+ while (temp->next ) {
41
43
cout<<temp->data <<" " ;
42
44
temp=temp->next ;
43
45
}
@@ -47,39 +49,35 @@ void display(int mod){
47
49
}
48
50
}
49
51
50
- int hash (int x,int mod){
52
+ int hash (int x,int mod) {
51
53
return x%mod;
52
54
}
53
55
54
- void find (int x,int h){
55
- struct node *temp =head[h];
56
- if (head[h]== NULL ) {
56
+ void find (int x,int h) {
57
+ struct Node *temp =head[h];
58
+ if (! head[h]) {
57
59
cout<<" Element not found" ;
58
60
return ;
59
61
}
60
- while (1 ){
61
- if (temp->data ==x)
62
- break ;
63
- else if (temp->next ==NULL )
64
- break ;
62
+ while (temp->data !=x && temp->next )
65
63
temp=temp->next ;
66
- }
67
- if (temp->next !=NULL )
64
+ if (temp->next )
68
65
cout<<" Element found" ;
69
66
else {
70
- if (temp->data == x)
67
+ if (temp->data == x)
71
68
cout<<" Element found" ;
72
69
else
73
- cout<<" Element not found" ;
70
+ cout<< " Element not found" ;
74
71
}
75
72
}
76
73
77
- int main (void ){
74
+ int main (void ) {
78
75
init ();
79
- int c,x,mod;
76
+ int c,x,mod,h ;
80
77
cout<<" Enter the size of Hash Table. = " ;
81
78
cin>>mod;
82
- while (1 ){
79
+ bool loop = true ;
80
+ while (loop) {
83
81
cout<<endl;
84
82
cout<<" PLEASE CHOOSE -" <<endl;
85
83
cout<<" 1. Add element." <<endl;
@@ -88,38 +86,32 @@ int main(void){
88
86
cout<<" 4. Display Hash table." <<endl;
89
87
cout<<" 5. Exit." <<endl;
90
88
cin>>c;
91
- switch (c){
92
- case 1 :{
89
+ switch (c) {
90
+ case 1 :
93
91
cout<<" Enter element to add = " ;
94
92
cin>>x;
95
- int h = hash (x,mod);
96
- if (h<0 )
97
- h=fabs (h);
93
+ h = hash (x,mod);
94
+ h = fabs (h);
98
95
add (x,h);
99
96
break ;
100
- }
101
- case 2 :{
97
+ case 2 :
102
98
cout<<" Enter element to search = " ;
103
99
cin>>x;
104
- int h= hash (x,mod);
100
+ h = hash (x,mod);
105
101
find (x,h);
106
102
break ;
107
- }
108
- case 3 :{
103
+ case 3 :
109
104
cout<<" Enter element to generate hash = " ;
110
105
cin>>x;
111
106
cout<<" Hash of " <<x<<" is = " <<hash (x,mod);
112
107
break ;
113
- }
114
- case 4 :{
108
+ case 4 :
115
109
display (mod);
116
- }
117
- default :{
118
110
break ;
119
- }
111
+ default :
112
+ loop = false ;
113
+ break ;
120
114
}
121
- if (c>4 )
122
- break ;
123
115
cout<<endl;
124
116
}
125
117
/* add(1,&head1);
0 commit comments