-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashing-OpenAddressing.c
95 lines (89 loc) · 1.8 KB
/
Hashing-OpenAddressing.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
#include<stdio.h>
#include<stdlib.h>
#define size 100
int n;
struct node
{
int data;
struct node *next;
};
struct node *head[size] = {NULL}, *c;
int hash(int key)
{
return (key % n);
}
void insert()
{
int i, key;
struct node *newnode;
printf("Enter value to insert into hash table : ");
scanf("%d", &key);
i = hash(key);
newnode = (struct node*)malloc(sizeof(struct node));
newnode -> data = key;
newnode -> next = NULL;
if(head[i] == NULL)
head[i] = newnode;
else
{
c = head[i];
while(c -> next!=NULL)
c = c -> next;
c -> next = newnode;
}
}
void search()
{
int key, i;
printf("Enter element to be searched : ");
scanf("%d", &key);
i = hash(key)
if(head[i] == NULL)
printf("Element not found\n");
else
{
for(c = head[i]; c != NULL; c = c->next)
{
if(c -> data == key)
{
printf("Search element found\n");
return;
}
}
printf("Search element not found\n");
}
}
void display()
{
int i;
for(i = 0; i < n; i++)
{
printf("Entries at index %d : ", i);
if(head[i] == NULL)
printf("No hash entry\n");
else
{
for(c = head[i]; c != NULL; c = c -> next)
printf("%d -> ", c -> data);
printf("\n");
}
}
}
void main()
{
int op, i;
printf("Enter the table size : ");
scanf("%d", &n);
while(1)
{
printf("1.Insert\t2.Display\t3.Search\t4.Exit\n");
scanf("%d", &op);
switch(op)
{
case 1: insert(); display(); break;
case 2: display(); break;
case 3: search(); break;
case 4: exit(0);
}
}
}