1+ /* Insert keys into an array with linear probing technique of collision resolution technique. */
2+ /* Data Structures Using C by UDIT AGARWAL */
3+
4+ #include <stdio.h>
5+ #include <conio.h>
6+ #define MAX 10
7+
8+ int a [MAX ];
9+ void lp (int ,int []);
10+ void lpsr (int key ,int a [MAX ]);
11+ void display (int a [MAX ]);
12+
13+ void main ()
14+ {
15+ int i ,key ,ch ;
16+ clrscr ();
17+ for (i = 0 ;i < MAX ;i ++ )
18+ a [i ]= '\0' ;
19+ do {
20+ printf ("\n\n Program for insertion/searching keys with linear probing " );
21+ printf ("\n************************************************************\n\n" );
22+ printf ("\n 1. Insert keys " );
23+ printf ("\n 2. Search key " );
24+ printf ("\n 3. Display keys " );
25+ printf ("\n 4. Exit " );
26+ printf ("\n Select operation " );
27+ scanf ("%d" ,& ch );
28+ switch (ch )
29+ {
30+ case 1 :do {
31+ printf ("\n Enter key value [type -1 for termination] " );
32+ scanf ("%d" ,& key );
33+ if (key != -1 )
34+ lp (key ,a );
35+ }while (key != -1 );
36+ display (a );
37+ break ;
38+ case 2 :printf ("\n Enter search key value " );
39+ scanf ("%d" ,& key );
40+ lpsr (key ,a );
41+ break ;
42+ case 3 :display (a );
43+ break ;
44+ }
45+ }while (ch != 4 );
46+ }
47+
48+ /* function lp find a location for key and insert it */
49+ void lp (int key ,int a [MAX ])
50+ {
51+ int loc ;
52+ loc = key %MAX ;
53+ while (a [loc ]!= '\0' )
54+ loc = ++ loc %MAX ;
55+ a [loc ]= key ;
56+ }
57+
58+ /* function lpsr find a location for a key */
59+ void lpsr (int key ,int a [MAX ])
60+ {
61+ int loc ;
62+ loc = key %MAX ;
63+ while ((a [loc ]!= key )&& (a [loc ]!= '\0' ))
64+ loc = ++ loc %MAX ;
65+ if (a [loc ]!= '\0' )
66+ printf ("\n Search successful at index %d" ,loc );
67+ else
68+ printf ("\n Search unsuccessful " );
69+ }
70+
71+ void display (int a [MAX ])
72+ {
73+ int i ;
74+ printf ("\n List of keys ('0' indicate that the location is empty): \n" );
75+ for (i = 0 ;i < MAX ;i ++ )
76+ printf (" %d " ,a [i ]);
77+ }