-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZList.c
170 lines (121 loc) · 3.83 KB
/
ZList.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
#include "Functions.h"
ZList ZLCreate(char *zip) { // ZList create
// Allocate space and assign values
ZList l = (ZList) malloc(sizeof(zl));
l->zip = malloc(strlen(zip) + 1);
strcpy(l->zip, zip);
l->count = 1;
l->link = NULL;
return l;
}
void ZLInsert(ZList list, char *zip) { // ZList insertion
ZList temp = list, backup = list;
// Go through the list
while(temp != NULL) {
if(!strcmp(temp->zip, zip)) { // If node has the zip we want
temp->count++;
return;
}
backup = temp;
temp = temp->link;
}
// Here we are at the end of the list
// Allocate new space and assign values
ZList l = ZLCreate(zip);
// Linkage
backup->link = l;
}
int ZLRemove(ZList l, char *zip, ZList *inv) { // Remove the node with the correct 'id'
if(!strcmp(l->zip, zip)) {
// if the first node of the list needs to be removed, we correctly link the inv pointer from the upper level
// We do not need InfoDestroy(l->info), because it will be done on HTRemove()
*inv = l->link;
free(l->zip);
free(l);
return 0;
}
ZList temp = l->link, backup = l;
// Cycle all list nodes
while(temp != NULL) {
if(!strcmp(temp->zip, zip)) {
// We do not need InfoDestroy(temp->info), because it will be done on HTRemove()
backup->link = temp->link; // Link the gap that will be created
free(temp->zip);
free(temp); // Free space allocated for this node
return 0;
}
// Linking
backup = temp;
temp = temp->link;
}
return 1;
}
/* void ZLPrint(ZList list, int rank) { // ZList printing for debugging
ZList temp = list;
int counter = 0, flag = 0;;
// Parse all nodes
while(temp != NULL) {
counter++;
if(counter == rank || flag == temp->count) {
printf("{%s , %d} ", temp->zip, temp->count);
flag = temp->count;
}
temp = temp->link;
}
printf("\n");
} */
void ZLSort(ZList list) {
int a;
char *a2;
// For every node
for(ZList temp1 = list ; temp1 != NULL ; temp1 = temp1->link) {
// For every node after the outer one
for(ZList temp2 = temp1->link ; temp2 != NULL ; temp2 = temp2->link) {
// Sort based on count
if(temp2->count > temp1->count) {
// Swap contents
a = temp1->count;
a2 = temp1->zip;
temp1->count = temp2->count;
temp1->zip = temp2->zip;
temp2->count = a;
temp2->zip = a2;
}
}
}
}
IDList ZLPostal(ZList list, int rank) { // Get the rank-th zip code
// Sort the list for correct searhing
ZLSort(list);
IDList final = NULL;
ZList temp = list;
int counter = 0, flag = 0, previousCount = -1;
// Parse all nodes
while(temp != NULL) {
if(temp->count != previousCount) // This is here so we do not up the counter while we are on the same rank -- count only for distinct values of counters
counter++; // Same counter values have the same rank
// When we find the first item we want to get, we raise the flag and continue adding until the counter value changes for a node
if(counter == rank || flag == temp->count) {
if(flag == 0) // If flag is down create the list since we have not yet started
final = IDLCreate(temp->zip);
else
IDLInsert(final, temp->zip); // Flag is up so add items to the already made list
flag = temp->count; // Raise the flag
}
previousCount = temp->count; // Keep this for correct counting
temp = temp->link;
}
// Return final list
return final;
}
void ZLDestroy(ZList list) { // Free space of a ZList
// Recursively free extra nodes
if(list != NULL && list->link != NULL) // (list != NULL) allows for instant exit after program startup
ZLDestroy(list->link);
// Free current node space
if(list != NULL && list->zip != NULL) // (list != NULL) allows for instant exit after program startup
free(list->zip);
// Free current node
if(list != NULL)
free(list);
}