-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfNode.c
More file actions
77 lines (68 loc) · 1.77 KB
/
fNode.c
File metadata and controls
77 lines (68 loc) · 1.77 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fNode.h"
int startFNode (fNode* fNodeHead) {
fNodeHead = NULL;
return EXIT_SUCCESS;
}
int insertFNode(fNode** fNodeHead, Node** head, char* filename, int wordCount) {
fNode* newNode = malloc(sizeof(fNode));
newNode->head = *head;
int len = strlen(filename) + 1;
newNode->filename = malloc(len);
memcpy(newNode->filename, filename, len);
newNode->next = NULL;
newNode->wordCount = wordCount;
if ((*fNodeHead) == NULL) {
*fNodeHead = newNode;
return 0;
}
else if (strcmp((*fNodeHead)->filename, filename) > 0) {
newNode->next = *fNodeHead;
*fNodeHead = newNode;
return 0;
}
else {
fNode* current = *fNodeHead;
fNode* prev = *fNodeHead;
while (current != NULL){
if (strcmp(current->filename, filename) > 0) {
newNode->next = current;
prev->next = newNode;
return 0;
}
prev = current;
current = current->next;
}
prev->next = newNode;
}
return 0;
}
void printFileList(fNode* fNodeHead) {
fNode* ptr = fNodeHead;
while (ptr != NULL) {
printf("%s: %d\t", ptr->filename, ptr->wordCount);
ptr = ptr->next;
}
printf("\n");
}
void freeFileList(fNode* fNodeHead) {
fNode* tempNode = fNodeHead;
while (fNodeHead != NULL) {
tempNode = fNodeHead;
fNodeHead = fNodeHead->next;
free(tempNode->filename);
freeList(tempNode->head);
free(tempNode);
}
}
int fileListLength(fNode* fNodeHead) {
int len = 0;
fNode* ptr = fNodeHead;
while (ptr != NULL) {
len++;
ptr = ptr->next;
}
return len;
}