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