sorting fixed

This commit is contained in:
2025-06-12 11:31:39 +02:00
parent c06268383f
commit c57dbe080b

38
main.c
View File

@@ -86,17 +86,20 @@ void sorted_count_insert(person **head, person *node) {
person *p = *head;
person *p_prev = NULL;
int cmp = p->count - node->count;
while (p != NULL && cmp < 0) {
while (p->next != NULL && cmp > 0) {
p_prev = p;
cmp = p->count - node->count;
p = p->next;
cmp = p->count - node->count;
}
if (p_prev == NULL) {
node->next = *head;
*head = node;
} else {
} else if (p->next != NULL && cmp < 0) {
node->next = p;
p_prev->next = node;
} else {
p->next = node;
node->next = NULL;
}
}
}
@@ -124,20 +127,20 @@ void hm_insert(person **hashmap, char *name) {
sorted_name_insert(&hashmap[hash_value], name);
}
char *parse_line(char *line) {
void parse_line(char *line, char *name) {
char *line_it = line;
if (*line_it == '<') {
line_it++;
char *tagname = malloc(sizeof(char) * TAG_MAX_LENGTH);
size_t i = 0;
while (i < TAG_MAX_LENGTH-1 && *line_it != ' ' && *line_it != '>' && *line_it != '\0' && *line_it != '\n') {
while (i < TAG_MAX_LENGTH - 1 && *line_it != ' ' && *line_it != '>' &&
*line_it != '\0' && *line_it != '\n') {
tagname[i] = *line_it;
line_it++;
i++;
}
tagname[i] = '\0';
if (strcmp(tagname, "author") == 0 || strcmp(tagname, "editor") == 0) {
free(tagname);
while (*line_it != '>') {
line_it++;
}
@@ -157,7 +160,6 @@ char *parse_line(char *line) {
break;
}
}
char *name = malloc(sizeof(char) * NAME_MAX_LENGTH);
i = 0;
line_it = last_space + 1;
while (i < NAME_MAX_LENGTH - 1 && *line_it != '<' && *line_it != ' ') {
@@ -166,11 +168,9 @@ char *parse_line(char *line) {
i++;
}
name[i] = '\0';
return name;
}
free(tagname);
}
return NULL;
}
void make_list(person **hashmap, person **list, const int min_count) {
@@ -203,7 +203,7 @@ void clean_list(person *list) {
void clean_memory(person **hashmap) {
for (int i = 0; i < HASH_BUCKETS; i++) {
person *p = hashmap[i];
while (p != nullptr) {
while (p != NULL) {
person *next = p->next;
free(p);
p = next;
@@ -219,14 +219,26 @@ int main(void) {
}
FILE *fp = fopen("dblp.xml", "r");
char line[1024];
while (fgets(line, 1024, fp) != NULL) {
char *name = parse_line(line);
if (fp) {
while (fgets(line, sizeof(line), fp) != NULL) {
char *name = (char *)malloc(sizeof(char) * NAME_MAX_LENGTH);
parse_line(line, name);
if (name != NULL) {
hm_insert(hashmap, name);
}
free(name);
}
fclose(fp);
} else {
while (fgets(line, sizeof(line), stdin) != NULL) {
char *name = (char *)malloc(sizeof(char) * NAME_MAX_LENGTH);
parse_line(line, name);
if (name != NULL) {
hm_insert(hashmap, name);
}
free(name);
}
}
fclose(fp);
printf("Done parsing!\n");
person *list = NULL;
make_list(hashmap, &list, MIN_COUNT);