-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP8_Hashing_a.c
136 lines (111 loc) · 3.54 KB
/
P8_Hashing_a.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 100
struct Employee {
char phoneNumber[15];
char information[50];
};
struct Node {
struct Employee data;
struct Node* next;
};
struct HashTable {
struct Node* table[TABLE_SIZE];
};
int hashFunction(char* phoneNumber) {
int sum = 0;
for (int i = 0; phoneNumber[i] != '\0'; i++) {
sum += phoneNumber[i];
}
return sum % TABLE_SIZE;
}
void insertRecord(struct HashTable* hashTable, struct Employee employee) {
int index = hashFunction(employee.phoneNumber);
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = employee;
newNode->next = NULL;
if (hashTable->table[index] == NULL) {
hashTable->table[index] = newNode;
} else {
newNode->next = hashTable->table[index];
hashTable->table[index] = newNode;
}
printf("Record inserted successfully.\n");
}
void searchRecord(struct HashTable* hashTable, char* phoneNumber) {
int index = hashFunction(phoneNumber);
struct Node* current = hashTable->table[index];
while (current != NULL) {
if (strcmp(current->data.phoneNumber, phoneNumber) == 0) {
printf("Phone Number: %s\n", current->data.phoneNumber);
printf("Information: %s\n", current->data.information);
return;
}
current = current->next;
}
printf("Record not found for phone number %s.\n", phoneNumber);
}
void deleteRecord(struct HashTable* hashTable, char* phoneNumber) {
int index = hashFunction(phoneNumber);
struct Node* current = hashTable->table[index];
struct Node* prev = NULL;
while (current != NULL) {
if (strcmp(current->data.phoneNumber, phoneNumber) == 0) {
if (prev == NULL) {
hashTable->table[index] = current->next;
} else {
prev->next = current->next;
}
free(current);
printf("Record deleted successfully.\n");
return;
}
prev = current;
current = current->next;
}
printf("Record not found for phone number %s.\n", phoneNumber);
}
int main() {
struct HashTable hashTable;
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable.table[i] = NULL;
}
int choice;
struct Employee employee;
char phoneNumber[15];
do {
printf("\nMenu:\n");
printf("1. Insert Record\n");
printf("2. Search Record\n");
printf("3. Delete Record\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter phone number: ");
scanf("%s", employee.phoneNumber);
printf("Enter information: ");
scanf(" %[^\n]s", employee.information);
insertRecord(&hashTable, employee);
break;
case 2:
printf("Enter phone number to search: ");
scanf("%s", phoneNumber);
searchRecord(&hashTable, phoneNumber);
break;
case 3:
printf("Enter phone number to delete: ");
scanf("%s", phoneNumber);
deleteRecord(&hashTable, phoneNumber);
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}