-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableChainingC.c
More file actions
82 lines (73 loc) · 1.03 KB
/
HashTableChainingC.c
File metadata and controls
82 lines (73 loc) · 1.03 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
#ifndef Chains_h
#define Chains_h
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
void SortedInsert(struct Node** H, int x)
{
struct Node* t, * q = NULL, * p = *H;
t = (struct Node*)malloc(sizeof(struct Node));
t->data = x;
t->next = NULL;
if (*H == NULL)
*H = t;
else
{
while (p && p->data < x)
{
q = p;
p = p->next;
}
if (p == *H)
{
t->next = *H;
*H = t;
}
else
{
t->next = q->next;
q->next = t;
}
}
}
struct Node* Search(struct Node* p, int key)
{
while (p != NULL)
{
if (key == p->data)
{
return p;
}
p = p->next;
}
return NULL;
}
#endif /* Chains_h */
#include <stdio.h>
#include "Chains.h"
int hash(int key)
{
return key % 10;
}
void Insert(struct Node* H[], int key)
{
int index = hash(key);
SortedInsert(&H[index], key);
}
int main()
{
struct Node* HT[10];
struct Node* temp;
int i;
for (i = 0; i < 10; i++)
HT[i] = NULL;
Insert(HT, 12);
Insert(HT, 22);
Insert(HT, 42);
temp = Search(HT[hash(21)], 21);
printf("%d ", temp->data);
return 0;
}