forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearprobing.C
77 lines (71 loc) · 1.67 KB
/
Linearprobing.C
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
/* Insert keys into an array with linear probing technique of collision resolution technique. */
/* Data Structures Using C by UDIT AGARWAL */
#include<stdio.h>
#include<conio.h>
#define MAX 10
int a[MAX];
void lp(int , int[]);
void lpsr(int key, int a[MAX]);
void display(int a[MAX]);
void main()
{
int i, key,ch ;
clrscr();
for(i=0;i<MAX;i++)
a[i] = '\0';
do{
printf("\n\n Program for insertion/searching keys with linear probing ");
printf("\n************************************************************\n\n");
printf("\n 1. Insert keys ");
printf("\n 2. Search key ");
printf("\n 3. Display keys ");
printf("\n 4. Exit ");
printf("\n Select operation ");
scanf("%d",&ch);
switch(ch)
{
case 1: do{
printf("\n Enter key value [type -1 for termination] ");
scanf("%d",&key);
if (key != -1)
lp(key,a);
}while(key!=-1);
display(a);
break;
case 2: printf("\n Enter search key value ");
scanf("%d",&key);
lpsr(key,a);
break;
case 3: display(a);
break;
}
}while(ch!=4);
}
/* function lp find a location for key and insert it */
void lp(int key, int a[MAX])
{
int loc;
loc = key % MAX;
while (a[loc] !='\0')
loc = ++loc % MAX;
a[loc] = key;
}
/* function lpsr find a location for a key */
void lpsr(int key, int a[MAX])
{
int loc;
loc = key % MAX;
while ((a[loc] != key) && (a[loc] !='\0'))
loc=++loc%MAX;
if (a[loc] != '\0')
printf("\n Search successful at index %d",loc);
else
printf("\n Search unsuccessful ");
}
void display(int a[MAX])
{
int i;
printf("\n List of keys ('0' indicate that the location is empty): \n");
for (i=0;i<MAX;i++)
printf(" %d ",a[i]);
}