-
Notifications
You must be signed in to change notification settings - Fork 0
/
inverted_list.c
107 lines (102 loc) · 3.37 KB
/
inverted_list.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
#include "inverted_list.h"
#include <stdio.h>
#include <stdlib.h>
void print_inverted_list(struct NodeOfInvertedLinkedList *InvertedList)
{
struct NodeOfInvertedLinkedList *curr_node = InvertedList;
int i = 0;
int j = 0;
while (curr_node != NULL)
{
if (curr_node->TK == -1)
{
break;
}
else
{
printf("%d %d i ", curr_node->TK, i);
struct ListOfVoters *curr_list = curr_node->LofV;
while (curr_list != NULL)
{
printf("%s %d j\n", curr_list->v->name, j);
curr_list = curr_list->next;
j++;
}
}
curr_node = curr_node->next;
i++;
j = 0;
}
}
void free_inverted_list(struct NodeOfInvertedLinkedList *InvertedList)
{
struct NodeOfInvertedLinkedList *currentNode = InvertedList;
while (currentNode != NULL)
{
struct NodeOfInvertedLinkedList *nextNode = currentNode->next;
struct ListOfVoters *currentList = currentNode->LofV;
while (currentList != NULL)
{
struct ListOfVoters *nextList = currentList->next;
free(currentList); // free ListOfVoters node
currentList = nextList;
}
free(currentNode); // free InvertedLinkedList node
currentNode = nextNode;
}
}
struct NodeOfInvertedLinkedList *fill_inverted_list(struct NodeOfInvertedLinkedList *InvertedList, struct voter *v, int TK)
{
struct NodeOfInvertedLinkedList *originalInvertedList = InvertedList; // Store the original pointer
while (InvertedList != NULL)
{
if (InvertedList->TK == -1) // there are no TK in this node so fill this
{
InvertedList->TK = TK;
InvertedList->LofV = (struct ListOfVoters *)malloc(sizeof(struct ListOfVoters));
if (InvertedList->LofV == NULL)
{
printf("Error malloc\n");
exit(-1);
}
InvertedList->LofV->next = NULL;
InvertedList->LofV->v = v;
struct NodeOfInvertedLinkedList *new_node = init_inverted_list();
InvertedList->next = new_node;
break;
}
else if (InvertedList->TK == TK) // if the TK already exists in the inverted list
{
struct ListOfVoters *curr_lofv = InvertedList->LofV;
while (curr_lofv->next != NULL) // find the last inserted voter with this TK
{
curr_lofv = curr_lofv->next;
}
struct ListOfVoters *new_lofv = (struct ListOfVoters *)malloc(sizeof(struct ListOfVoters));
if (new_lofv == NULL)
{
printf("Error malloc\n");
exit(-1);
}
curr_lofv->next = new_lofv;
new_lofv->next = NULL;
new_lofv->v = v;
break;
}
InvertedList = InvertedList->next;
}
return originalInvertedList; // Return the original pointer
}
struct NodeOfInvertedLinkedList *init_inverted_list(void)
{
struct NodeOfInvertedLinkedList *invertedList = (struct NodeOfInvertedLinkedList *)malloc(sizeof(struct NodeOfInvertedLinkedList));
if (invertedList == NULL)
{
printf("Error malloc\n");
exit(-1);
}
invertedList->TK = -1;
invertedList->LofV = NULL;
invertedList->next = NULL;
return invertedList;
}