This repository has been archived by the owner on Feb 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docente.cpp
217 lines (173 loc) · 5.51 KB
/
docente.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "docente.h"
#include "buffer.h"
#include <iostream>
#include <string.h>
using namespace std;
// Function to do merge sort
docente* orderDocentes(docente* docentes){
if (!docentes || !docentes->next)
return docentes;
docente* second = split(docentes);
// Recur for left and right halves
docentes = orderDocentes(docentes);
second = orderDocentes(second);
// Merge the two sorted halves
return merge(docentes,second);
}
docente* split(docente* docentes){
docente*fast = docentes,*slow = docentes;
while (fast->next && fast->next->next){
fast = fast->next->next;
slow = slow->next;
}
docente*temp = slow->next;
slow->next = NULL;
return temp;
}
// Function to merge two linked lists
docente* merge(docente*first, docente*second){
// If first linked list is empty
if (!first)
return second;
// If second linked list is empty
if (!second)
return first;
// Pick the smaller value
if (first->totalPoints > second->totalPoints) {
first->next = merge(first->next,second);
first->next->prev = first;
first->prev = NULL;
return first;
}else{
second->next = merge(first,second->next);
second->next->prev = second;
second->prev = NULL;
return second;
}
}
/*
docente* orderDocentes(docente* docentes){
docente* ordedDocentes = NULL;
if(docentes != NULL){
docente* i = docentes;
while(i->next != NULL){
i = i->next;
}
}
return orderDocentes;
}
*/
docente* loadAllDocentes(const char* filePath){
//A double linked list of type "docente"
docente* docentes = NULL;
if(filePath != NULL){
//Load the docente.csv file into a Buffer File
character* docenteCSV = createBufferFile(filePath);
//Remove the first line of the text (it is useless)
character* currentLine = removeFirstBufferLine(&docenteCSV);
destroyBufferFile(¤tLine);
//Create each "docente" based on the Buffer File
while(currentLine = removeFirstBufferLine(&docenteCSV)){
//Get docente's ID
long id = stringToLong(getNthColumnData(currentLine, 1));
//Get docente's name
char* name = getNthColumnData(currentLine, 2);
//Destroy the removed line (Only the text inside of it)
destroyBufferFile(¤tLine);
//Create a docente with the given data and add it to a list
addDocente(&docentes, id, name);
}
}
return docentes;
}
docente* createDocente(long id, char* name){
docente* newDocente = NULL;
newDocente = new docente[1];
if(newDocente != NULL){
newDocente->id = id;
newDocente->name = name;
newDocente->points = new int[22];
for(int i=0;i<22;i++)
newDocente->points[i] = 0;
newDocente->next = NULL;
newDocente->last = NULL;
newDocente->prev = NULL;
}
return newDocente;
}
void destroyDocente(docente* docenteToDestroy){
//If the user actually pass a valid "docente"
if(docenteToDestroy != NULL){
delete[]docenteToDestroy->points;
delete[] docenteToDestroy;
}
}
void addDocente(docente** docenteList, long id, char* name){
//If the user actually pass a empty docenteList
if(*docenteList != NULL){
//Create a new docente
docente* newDocente = createDocente(id, name);
//Check if the new docente was allocated
if(newDocente != NULL){
//Check if the last docente was not set yet
if((*docenteList)->last == NULL){
//Iterate through docenteList until the its end
docente* iterator = (*docenteList);
while(iterator->next != NULL){
iterator = iterator->next;
}
//Check if the docenteList is not at the middle of the list
if((*docenteList)->prev == NULL){
//Save the address of the last docente
(*docenteList)->last = iterator;
}else{
//Save the address of the last docente
docente* lastDocenteAddress = iterator;
//Go back until the given docente as parameter
iterator = (*docenteList);
//Iterate through docenteList until the its head
while(iterator->prev != NULL){
iterator = iterator->prev;
}
//Save the address of the last docente
iterator->last = lastDocenteAddress;
}
}
//Insert the new docente at the docenteList
newDocente->id = id;
newDocente->name = name;
newDocente->next = NULL;
//Save the adress of the old last docente
newDocente->prev = (*docenteList)->last;
//Make the old last docente point to the new one
(*docenteList)->last->next = newDocente;
//Update the new last docente
(*docenteList)->last = newDocente;
}
}else{
docente* newDocente = createDocente(id,name);
newDocente->last = newDocente;
*docenteList = newDocente;
}
}
void destroyAllDocentes(docente** docentes){
//If the user actually pass a valid double linked list of docente
if(docentes != NULL){
//Get the address of the first docente
docente* iterator = *docentes;
//Go to the end of the double linked list of docente
while(iterator->next != NULL){
iterator = iterator->next;
}
//Go to the start of the double linked list of docente
while(iterator->prev != NULL){
//Go backwards one docente
iterator = iterator->prev;
//Desalocate the docente that is after the current
destroyDocente(iterator->next);
}
//Destroy the first docente (the only one remaing)
destroyDocente(iterator);
*docentes = NULL;
}
}