-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash chaing techniques.cpp
153 lines (104 loc) · 2.09 KB
/
hash chaing techniques.cpp
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
145
146
147
148
149
150
151
152
153
#include<iostream>
using namespace std;
//hash index function
int hash_index(int key)
{
return (key % 10);
}
/////////////////////////////////////link list functions////////////////////////////////////////////////////////////
struct node
{
int data;
node *next ;
};
//insert and delete work is remaining
void link_insert(node **list,int x)
{
node *t=new node;
t->data=x;
t->next =0;
node *p= *list;
if(*list == 0)
*list=t;
else
{
while(p->next)
p=p->next;
p->next =t;
}
}
int link_delet(node**list,int key )
{
int x = -1;
node *p= *list;
node *q=0;
while( p)
{
if( p->data != key)
{
q=p;
p=p->next;
}
else
break;
}
if(p == 0)
cout<<"Key not present ";
else
{
if( p == *list) //if first node
{
x=(*list)->data;
p=*list;
*list=(*list)->next;
delete p;
}
else //other than first node
{
q->next=p->next;
x=p->data;
delete p;
}
}
return x;
}
int link_search(node *p,int key)
{
while(p)
{
if(p->data == key)
{
cout<<"Key is present at index : ";
return (hash_index(key));
}
else
p=p->next;
}
cout<<"Key not found : ";
return -1 ;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define size 10
int main()
{
int i,key,temp;
node **ht=new node*[size]; //note:node **ht
for(i=0;i<10;i++) //initializing to null
ht[i]=0;
cout<<"Enter the how many keys to insert in hash";
cin>>temp;
for(i=0;i<temp;i++)
{
cout<<"Enter the key "<< i+1<<": ";
cin>>key;
link_insert(&ht[ hash_index(key) ],key);
}
cout<<"Enter key to search key : ";
cin >> key;
temp=link_search(ht[ hash_index(key) ],key);
cout<<temp<<endl ;
cout<<"Enter key to delete key : ";
cin >> key;
temp=link_delet(&ht[ hash_index(key) ],key);
cout<<"the deleted element is : "<<temp;
}