forked from Dipak3007/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table.c
More file actions
83 lines (63 loc) · 1.22 KB
/
hash_table.c
File metadata and controls
83 lines (63 loc) · 1.22 KB
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
/*To change hash table size change in MACRO part currently it is 10*/
#include<stdio.h>
#include<stdlib.h>
#define size 10
typedef struct mynode
{
int data;
struct mynode *next;
}node;
node *chain[size];
void initialize()
{
for(int i = 0; i < size; i++)
chain[i] = NULL;
}
void insertNode(int value)
{
node *newNode = malloc(sizeof(node));
newNode->data = value;
newNode->next = NULL;
int key = value % size;
if(chain[key] == NULL)
chain[key] = newNode;
else
{
node *temp = chain[key];
while(temp->next)
{
temp = temp->next;
}
temp->next = newNode;
}
}
void printHashTable()
{
int i;
for(i = 0; i < size; i++)
{
node *temp = chain[i];
printf("chain[%d]-->",i);
while(temp)
{
printf("%d -->",temp->data);
temp = temp->next;
}
printf("NULL\n");
}
}
int main()
{
initialize();
int n,temp;
printf("Enter the no. of elements: ");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
printf("Enter element %d : ",i);
scanf("%d",&temp);
insertNode(temp);
}
printHashTable();
return 0;
}