-
Notifications
You must be signed in to change notification settings - Fork 0
/
postingList.c
95 lines (77 loc) · 1.61 KB
/
postingList.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
#include "postingList.h"
#include <stdlib.h>
#include <stdio.h>
struct postingList{
int textIndex;
int appearanceCount;
postingList *next;
};
postingList *createPL(int textIndex, int count){
postingList *pl = malloc(sizeof(postingList));
if(pl == NULL){
return NULL;
}
pl->textIndex = textIndex;
pl->appearanceCount = count;
pl->next = NULL;
return pl;
}
int addAppearancePL(postingList *pl, int textIndex){
//TODO: Check for errors if adding node at the start of the list
postingList *temp;
while(textIndex > pl->textIndex && pl->next != NULL){
//Go to the node with the same index or the end of the list
pl = pl->next;
}
if(pl->textIndex == textIndex){
pl->appearanceCount += 1;
}
else{
temp = pl->next;
pl->next = createPL(textIndex, 1);
if(pl->next == NULL){
return 0;
}
pl->next->next = temp;
}
return 1;
}
void deletePL(postingList *pl){
if(pl->next != NULL){
deletePL(pl->next);
}
free(pl);
}
postingList *getNextPL(postingList *pl){
return pl->next;
}
int getIndexPL(postingList *pl){
return pl->textIndex;
}
int getCountPL(postingList *pl){
return pl->appearanceCount;
}
int getSizePL(postingList *pl){
int size = 1;
while(pl->next != NULL){
pl = pl->next;
size++;
}
return size;
}
int getTotalAppearancesPL(postingList *pl){
int total = 0;
while(pl != NULL){
total += getCountPL(pl);
pl = getNextPL(pl);
}
return total;
}
void printPL(postingList *pl){
printf("[ ");
while(pl != NULL){
printf("(%d,%d) ", pl->textIndex, pl->appearanceCount);
pl = pl->next;
}
printf("]");
}