-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearhash
114 lines (102 loc) · 1.64 KB
/
Linearhash
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<stdio.h>
#include<stdlib.h>
#define SIZE 10
int array[SIZE];
void insert(int);
void delete(int);
void search(int);
void set();
int main()
{
int choice,n,data;
set();
while(1){
printf("\n1.insert \n2.delete\n3.search\n4.exit\n enter your choice:");
scanf("%d",&n);
switch(n)
{
case 1 :
printf("enter number: ");
scanf("%d",&data);
insert(data);
break;
case 2:
printf("enter number: ");
scanf("%d",&data);
delete(data);
break;
case 3 :
printf("enter number: ");
scanf("%d",&data);
search(data);
break;
case 4:exit(0);
}
}
return(0);
}
void insert(int data){
int key=data%SIZE;
if(array[key]==-1)
{ array[key]=data; }
else{
int temp=(key+1)%SIZE;
while(temp!=key)
{
if(array[temp]==-1)
{array[temp]=data;
break;}
temp=(temp+1)%SIZE;
}
if(temp==key)
{
printf("overflow\n");
}
}
}
void search(int data){
int key=data%SIZE;
if(array[key]==data)
{ printf("element found at %d",key); }
else{
int temp=(key+1)%SIZE;
while(temp!=key)
{
if(array[temp]==data)
{ printf("element found at %d",temp);
break;}
temp=(temp+1)%SIZE;
}
if(temp==key)
{
printf("element not found\n");
}
}
}
void delete(int data)
{
int key=data%SIZE;
if(array[key]==data)
{ array[key]=-1; }
else{
int temp=(key+1)%SIZE;
while(temp!=key)
{
if(array[temp]==data)
{ array[temp]=-1;
break;}
temp=(temp+1)%SIZE;
}
if(temp==key)
{
printf("element not found\n");
}
}
}
void set()
{
for(int i=0;i<SIZE;i++)
{
array[i]=-1;
}
}