-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhctn_queue.c
118 lines (96 loc) · 3.34 KB
/
hctn_queue.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
108
109
110
111
112
113
114
115
116
117
118
#include "hctn_queue.h"
HCTNQueue* createHCTNQueue() {
HCTNQueue* pNewHCTNQueue = (HCTNQueue*)calloc(1, sizeof(HCTNQueue));
if (pNewHCTNQueue == NULL) {
fprintf(stderr, "ERROR: Failed to Create Huffman Coding Tree Node Queue!\nCAUSE: Memory Allocation for HCTN Queue Failed!\n");
return NULL;
}
pNewHCTNQueue->pFront = NULL;
pNewHCTNQueue->pBack = NULL;
return pNewHCTNQueue;
}
bool isEmptyHCTNQueue(HCTNQueue* pHCTNQueue) {
if (pHCTNQueue == NULL) {
fprintf(stderr, "ERROR: Failed to Check if Huffman Coding Tree Node Queue is Empty!\nCAUSE: Pointer to HCTN Queue is NULL!\n");
return NULL;
}
return pHCTNQueue->pFront == NULL;
}
void enqueueIntoHTCNQueue(HCTNQueue* pHCTNQueue, HCTNode* pHCTNode)
{
if (pHCTNQueue == NULL)
{
fprintf(stderr, "ERROR: Failed to Enqueue into Huffman Coding Tree Node Queue!\nCAUSE: Pointer to HCTN Queue is NULL!\n");
return;
}
if (pHCTNode == NULL)
{
fprintf(stderr, "ERROR: Failed to Enqueue into Huffman Coding Tree Node Queue!\nCAUSE: Pointer to HCT Node is NULL!\n");
return;
}
if (isEmptyHCTNQueue(pHCTNQueue) == true)
{
pHCTNQueue->pFront = pHCTNode;
pHCTNQueue->pBack = pHCTNode;
}
else if (isEmptyHCTNQueue(pHCTNQueue) == false)
{
pHCTNQueue->pBack->pNextNode = pHCTNode;
pHCTNQueue->pBack = pHCTNode;
}
}
HCTNode* dequeueFromHTCNQueue(HCTNQueue* pHCTNQueue)
{
if (pHCTNQueue == NULL)
{
fprintf(stderr, "ERROR: Failed to Dequeue from Huffman Coding Tree Node Queue!\nCAUSE: Pointer to HCTN Queue is NULL!\n");
return NULL;
}
if (isEmptyHCTNQueue(pHCTNQueue) == true)
{
fprintf(stderr, "ERROR: Failed to Dequeue from Huffman Coding Tree Node Queue!\nCAUSE: HCTN Queue is Empty!\n");
return NULL;
}
HCTNode* pHCTNode = pHCTNQueue->pFront;
pHCTNQueue->pFront = pHCTNQueue->pFront->pNextNode;
pHCTNode->pNextNode = NULL;
if (isEmptyHCTNQueue(pHCTNQueue) == true)
{
pHCTNQueue->pBack = NULL;
}
return pHCTNode;
}
void fillUpHCTNQueue(HCTNQueue* pHCTNQueue, CharInfoNode* pCharInfoNodeDictionary)
{
if (pHCTNQueue == NULL)
{
fprintf(stderr, "ERROR: Failed to Fill Up Huffman Coding Tree Node Queue!\nCAUSE: Pointer to HCTN Queue is NULL!\n");
return;
}
if (pCharInfoNodeDictionary == NULL)
{
fprintf(stderr, "ERROR: Failed to Fill Up Huffman Coding Tree Node Queue!\nCAUSE: Pointer to Character Frequencies Array is NULL!\n");
return;
}
for (int decimalCharIndex = 0; decimalCharIndex < MAX_CHARACTERS; decimalCharIndex++) {
if (pCharInfoNodeDictionary[decimalCharIndex].frequency > 0)
{
enqueueIntoHTCNQueue(pHCTNQueue,createHCTNode(pCharInfoNodeDictionary[decimalCharIndex].character, pCharInfoNodeDictionary[decimalCharIndex].frequency));
}
}
}
void destroyHCTNQueue(HCTNQueue* pHCTNQueue)
{
if (pHCTNQueue == NULL)
{
fprintf(stderr, "ERROR: Failed to Destroy Huffman Coding Tree Node Queue!\nCAUSE: Pointer to HCTN Queue is NULL!\n");
return;
}
while (pHCTNQueue->pFront != NULL)
{
HCTNode* pNext = pHCTNQueue->pFront->pNextNode;
destroyHCTNode(pHCTNQueue->pFront);
pHCTNQueue->pFront = pNext;
}
free(pHCTNQueue);
}