Exp 9
Hashing
Class student
{
public:
Char * name;
Int mobile_no;
Int link;
};
Class hash_table
{
Student s[10];
Public:
hash_table()
{
For(i=0 to 9)
{
s[i].link=-1;
s[i].mobile_no=0;
}
}
Void insert();
Void update();
Void search(int mob_no);
Void display();
Void with_replacement();
Void without_replacement();
}
Without
replacement:
|
index
|
Student Mobile Number
|
Student Name
|
link
|
|
0
|
8870234560
|
abc
|
-1
|
|
1
|
8923456671
|
xyz
|
-1
|
|
2
|
0
|
|
-1
|
|
3
|
0
|
|
-1
|
|
|
|
|
|
Insert mob no
9980301230 9980301230 %10 =
0 = index
if(s[index].mobile_no!=0) index=s[index].link
|
index
|
Student Mobile Number
|
Student Name
|
link
|
|
0
|
8870234560
|
abc
|
2
|
|
1
|
8923456671
|
xyz
|
-1
|
|
2
|
9980301230
|
pqr
|
-1
|
|
3
|
0
|
|
-1
|
|
|
0
|
|
-1
|
With
replacement:
|
index
|
Student Mobile Number
|
Student Name
|
link
|
|
0
|
8870234560
|
abc
|
2
|
|
1
|
8923456671
|
xyz
|
-1
|
|
2
|
9980301230
|
pqr
|
-1
|
|
3
|
0
|
|
-1
|
|
|
0
|
|
-1
|
Insert mob no
9980301222 9980301222 %10 = 2 = index
|
index
|
Student Mobile Number
|
Student Name
|
link
|
|
0
|
8870234560
|
abc
|
3
|
|
1
|
8923456671
|
xyz
|
-1
|
|
2
|
9980301222
|
ttt
|
-1
|
|
3
|
9980301230
|
pqr
|
-1 replaced from index 2
|
|
|
0
|
|
-1
|
If (s[index]!=0)
h=s[index].mob_no % 10
if ( h!= index) …………………if already existing mob_no do
not hash to the actual index replace it
{
Search for next empty slot
& insert … ( 9980301230 % 10 =
0 its not 2, so replace it to next
S[h].link =newlocation; poisition & change link of its previous data)
S[index].mob_no=mob_no;
S[index].name=name;
}
No comments:
Post a Comment