-
Notifications
You must be signed in to change notification settings - Fork 0
/
dslab12.c
115 lines (85 loc) · 1.83 KB
/
dslab12.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
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
//HASH TABLE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_SIZE 5
typedef struct employee{
int id;
char name[10];
}EMP;
void init(EMP a[]){
int i;
for(i=0;i<HASH_SIZE;i++){
a[i].id = 0;
}
}
int H(int k){
return k%HASH_SIZE;
}
void insert_hash(int id, char name[],EMP a[]){
int i,hvalue,index;
hvalue = H(id);
for(i=0;i<HASH_SIZE;i++){
index = (hvalue+i)%HASH_SIZE;
if(a[index].id == 0){
a[index].id = id;
strcpy(a[index].name,name);
break;
}
}
}
int search_hash(EMP a[], int key){
int i,hvalue,index;
hvalue = H(key);
for(i=0;i<HASH_SIZE;i++){
index = (hvalue+i)%HASH_SIZE;
if(a[index].id == key) return 1;
if(a[index].id == 0) return 0;
}
if(i== HASH_SIZE){
return 0;
}
}
void display_table(EMP a[]){
int i;
for(i=0;i<HASH_SIZE;i++){
if(a[i].id != 0)
printf("id = %d \n Name: %s \n",a[i].id,a[i].name);
}
}
int main(){
EMP a[10];
char name[10];
int i,key=0,id,ch,n,flag=0;
while(1){
printf("\t MENU \n");
printf("1. 1 Insert \n 2. Search \n 3. Display\n");
printf("Enter Your Choice:\n");
scanf("%d",&ch);
switch(ch){
case 1: printf("Enter No of Employee ? \n");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter id:\n");
scanf("%d",&id);
printf("Enter Name:\n");
scanf("%s",name);
insert_hash(id,name,a);
}
break;
case 2: printf("Enter Id to search:\n");
scanf("%d",&key);
flag = search_hash(a,key);
if(flag == 1){
printf("Employee Found on The Table.\n");
}else {
printf("Employee Not Found.\n");
}
break;
case 3: display_table(a);
break;
case 4: exit(0);
// default:printf("Invalid Choice.\n");
}
}
}