-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
149 lines (136 loc) · 3.46 KB
/
hash.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int size=0;
int ele =0;
struct node{
int id;
int age;
char name[100];
int marker;
};
struct node *hash=NULL;
void insert(int id,char name[100],int age){
int hashindex = id%size;
while (hash[hashindex].marker == 1) {
hashindex = (hashindex+1)%size;
}
hash[hashindex].marker = 1;
hash[hashindex].id = id;
hash[hashindex].age = age;
strcpy(hash[hashindex].name,name);
ele++;
}
void delete(int id){
if (ele ==0 ) {
printf("there are no elements to delete\n");
return;
}
int hashindex = id % size;
int count =0;
int flag=0;
while (count <= ele && hash[hashindex].marker!= 0) {
if (hash[hashindex].id == id) {
hash[hashindex].marker = -1;
hash[hashindex].id = 0;
hash[hashindex].age = 0;
strcpy(hash[hashindex].name,"none");
ele--;
flag=1;
break;
}
hashindex = (hashindex+1)%size;
count++;
}
if (flag == 1) {
printf("item deleated\n");
}
else{
printf("item not found\n");
}
}
void search(int id){
if (ele ==0 ) {
printf("there are no elements to search\n");
return;
}
int hashindex = id % size;
int count =0;
int flag=0;
while (count <= ele && hash[hashindex].marker!= 0) {
if (hash[hashindex].id == id) {
printf("ID=%d",hash[hashindex].id);
printf(" AGE=%d",hash[hashindex].age);
printf(" NAME=%s\n",hash[hashindex].name);
flag=1;
break;
}
hashindex = (hashindex+1)%size;
count++;
}
if (flag != 1) {
printf("item not present\n");
}
}
void display(){
if (ele == 0 ) {
printf("there are no elements to display\n");
return;
}
for (int i=0; i<size; i++) {
if (hash[i].marker == 1) {
printf("INDEX = %d\n",i);
printf(" ID = %d",hash[i].id);
printf(" AGE = %d",hash[i].age);
printf(" NAME = %s\n",hash[i].name);
}
}
}
int main(){
int id;
int age;
char name[100];
int s,i=0;
char c;
printf("enter the size of hash table\n");
scanf("%d",&size);
hash = (struct node *)calloc(size, sizeof(struct node));
while (1) {
printf("1 . insertion\n");
printf("2 . deletion\n");
printf("3 . search\n");
printf("4 . display\n");
printf("5 . exit\n");
scanf("%d",&s);
switch(s){
case 1:
printf("enter the id \n");
scanf("%d",&id);
printf("enter age \n");
scanf("%d",&age);
printf("enter name \n");
c = getchar();
fgets(name, 100, stdin);
name[strlen(name) - 1] = '\0';
insert(id,name,age);
break;
case 2:
printf("enter the id to be deleted\n");
scanf("%d",&id);
delete(id);
break;
case 3:
printf("enter the id to be searched\n");
scanf("%d",&id);
search(id);
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("enter correct integer\n");
}
}
}