-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab2.c
177 lines (150 loc) · 4.27 KB
/
lab2.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 50
#define MAX_PHONE_LENGTH 15
#define ALPHABET_SIZE 26
struct People{
char name[MAX_NAME_LENGTH];
char phone[MAX_PHONE_LENGTH];
struct People* next;
};
struct PeopleList{
struct People* head;
};
struct PhoneBook{
struct PeopleList alphabet[ALPHABET_SIZE];
};
void initPhoneBook(struct PhoneBook* phonebook){
int i;
for (i = 0; i< ALPHABET_SIZE; i++){
phonebook->alphabet[i].head = NULL;
}
}
void addPeople(struct PhoneBook* phonebook, const char* name, const char* phone){
struct PeopleList* list;
int index = name[0] - 65;
list = &phonebook->alphabet[index];
struct People* newPeople = (struct People*)malloc(sizeof(struct People));
if (newPeople == NULL){
printf("Memory allocation error");
return;
}
strcpy(newPeople->name,name);
strcpy(newPeople->phone, phone);
newPeople->next = list->head;
list->head = newPeople;
}
struct People* findPeople(struct PhoneBook* phonebook, const char* name){
struct PeopleList* list;
int index = name[0] - 65;
list = &phonebook->alphabet[index];
struct People* current = list->head;
while(current!= NULL){
if (strcmp(current->name, name) == 0){
return current;
}
current = current->next;
}
return NULL;
}
void deletePeople(struct PhoneBook* phonebook, const char* name){
struct PeopleList* list;
int index = name[0] - 65;
list = &phonebook->alphabet[index];
struct People* current = list->head;
struct People* prev = NULL;
while(current!=NULL){
if (strcmp(current->name,name)==0){
if(prev != NULL){
prev->next = current->next;
}else{
list->head = current->next;
}
free(current);
return;
}
prev = current;
current = current->next;
}
}
void sortPhoneBook(struct PhoneBook* phonebook) {
struct PeopleList* list;
int i;
for(i=0; i<ALPHABET_SIZE; i++){
list = &phonebook->alphabet[i];
struct People* current = list->head;
struct People* sortedList = NULL;
struct People* next;
while (current != NULL) {
next = current->next;
insertPeopleSorted(&sortedList, current);
current = next;
}
list->head = sortedList;
}
}
void insertPeopleSorted(struct People** sortedList, struct People* newPeople) {
struct People* current;
if (*sortedList == NULL || strcmp(newPeople->name, (*sortedList)->name) <= 0) {
newPeople->next = *sortedList;
*sortedList = newPeople;
} else {
current = *sortedList;
while (current->next != NULL && strcmp(newPeople->name, current->next->name) > 0) {
current = current->next;
}
newPeople->next = current->next;
current->next = newPeople;
}
}
void savePhoneBook(struct PhoneBook* phonebook, const char* filename){
FILE* file = fopen(filename,"wb");
if (file == NULL){
printf("Failed to open file");
return;
}
struct PeopleList* list;
struct People* current;
int i;
for(i=0; i<ALPHABET_SIZE;i++){
list = &phonebook->alphabet[i];
current = list->head;
while(current != NULL){
fwrite(current, sizeof(struct People), 1, file);
current = current->next;
}
}
fclose(file);
}
void loadPhoneBook(struct PhoneBook* phonebook, const char* filename){
FILE* file = fopen(filename,"rb");
if (file == NULL){
printf("Failed to open file");
exit(1);
}
initPhoneBook(phonebook);
struct People people;
while (fread(&people, sizeof(struct People), 1, file) == 1) {
char first = people.name[0];
int index = first - 65;
addPeople(&phonebook->alphabet[index],people.name,people.phone);
}
fclose(file);
}
int main(){
struct PhoneBook phonebook;
initPhoneBook(&phonebook);
loadPhoneBook(&phonebook, "phonebook.txt");
addPeople(&phonebook, "Trinh", "09227282882");
struct People* foundPeople = findPeople(&phonebook, "Trinh");
if (foundPeople != NULL) {
printf("Found: %s - %s\n", foundPeople->name, foundPeople->phone);
} else {
printf("Not found.\n");
}
deletePeople(&phonebook, "Trinh");
sortPhoneBook(&phonebook);
savePhoneBook(&phonebook, "phonebook.txt");
return 0;
}