forked from hugotallys/Huff-Dissected
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructures.h
More file actions
325 lines (292 loc) · 6.44 KB
/
structures.h
File metadata and controls
325 lines (292 loc) · 6.44 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#define HASH_SIZE 256
typedef struct node
{
unsigned char item;
long int q;
struct node *left;
struct node *right;
struct node *next;
struct node *previous;
}node;
typedef struct byte
{
unsigned char item;
long int position;
}byte;
typedef struct pq
{
node *head;
}pq;
typedef struct queue
{
node *front;
node *rear;
}queue;
typedef struct hash_node
{
queue *fila;
struct hash_node *next;
}hn;
typedef struct hash_table
{
hn *table[HASH_SIZE];
}hash_table;
byte * create_byte(unsigned char item)
{
byte *new_byte = (byte*) malloc(sizeof(new_byte));
new_byte->item = item;
new_byte->position = 7;
return new_byte;
}
hash_table *create_hash_table()
{
int i;
hash_table *new_hash_table = (hash_table*) malloc(sizeof(hash_table));
for (i = 0; i < HASH_SIZE; ++i)
{
new_hash_table->table[i] = NULL;
}
return new_hash_table;
}
hn *create_hash_node(hn *head, queue *fila)
{
hn *new_node = (hn*) malloc(sizeof(hn));
new_node->fila = fila;
return new_node;
}
void put(hash_table *ht,unsigned char item,queue *fila)
{
long int h = (long int) item;
ht->table[h] = create_hash_node(ht->table[h],fila);
}
pq *create_pq()
{
pq *new_pq = (pq*) malloc(sizeof(pq));
new_pq->head = NULL;
return new_pq;
}
queue *create_queue()
{
queue *new_queue = (queue*) malloc(sizeof(queue));
new_queue->front = new_queue->rear = NULL;
return new_queue;
}
node *create_empty_node()
{
return NULL;
}
node *create_node(unsigned char l, long int q)
{
node *new_node = (node*) malloc(sizeof(node));
new_node->item = l;
new_node->q = q;
new_node->left = NULL;
new_node->right = NULL;
new_node->next = NULL;
return new_node;
}
node *create_tree_node(unsigned char item, long int q, node *left, node *right)
{
node *new_node = (node*) malloc(sizeof(node));
new_node->item = item;
new_node->q = q;
new_node->left = left;
new_node->right = right;
return new_node;
}
node *add(node *head,unsigned char item)
{
if(head!=NULL)
{
if (head->item == item)
{
head->q++;
return head;
}
head->next = add(head->next,item);
return head;
}
node *new_node = malloc(sizeof(node));
new_node->item = item;
new_node->q = 1;
new_node->next = NULL;
return new_node;
}
void enqueue(queue *queue,char item)
{
node *new_node = (node*) malloc(sizeof(node));
new_node->item = item;
new_node->next = NULL;
new_node->previous = queue->rear;
if (queue->front == NULL && queue->rear == NULL)
{
queue->rear = queue->front = new_node;
}
else
{
queue->rear->next = new_node;
queue->rear = new_node;
}
}
void enqueue_pq(pq *queue, node *head)
{
node *new_node = (node*) malloc(sizeof(node));
new_node->item = head->item;
new_node->q = head->q;
if (queue->head == NULL ||(head->q < queue->head->q))
{
new_node->next = queue->head;
queue->head = new_node;
}
else
{
node *temp = queue->head;
while((temp->next != NULL) && (temp->next->q < head->q))
{
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
}
}
void enqueue_tree(pq *queue, node *head,node*left,node *right)
{
node *new_node = create_tree_node(head->item,head->q,left,right);
if (queue->head == NULL || (head->q < queue->head->q))
{
new_node->next = queue->head;
queue->head = new_node;
}
else if (head->q == queue->head->q)
{
if (head->item == '*')
{
new_node->next = queue->head;
queue->head = new_node;
}
}
else
{
node *temp = queue->head;
while((temp->next != NULL) && (temp->next->q < head->q))
{
temp = temp->next;
}
new_node->next = temp->next;
temp->next = new_node;
}
}
node *dequeue_pq(pq *queue)
{
if (queue->head != NULL)
{
//printf("aqui\n");
node *temp = queue->head;
queue->head = queue->head->next;
return temp;
}
else
{
return NULL;
}
}
unsigned char dequeue(queue *queue)
{
node *temp = queue->rear;
if(queue->front == NULL)
{
return 1;
}
if (queue->front == queue->rear)
{
queue->front = queue->rear = NULL;
}
else
{
queue->rear = queue->rear->previous;
queue->rear->next = NULL;
}
return temp->item;
}
void merge(node *bt,node *left, node *right,pq *queue)
{
bt = create_node('*', left->q + right->q);
bt->left = left;
bt->right = right;
enqueue_tree(queue,bt,bt->left,bt->right);
}
void print(node *head)
{
node *temp = head;
while(temp != NULL)
{
printf("%c %ld\n",temp->item,temp->q);
temp = temp->next;
}
}
void print_queue(queue *queue)
{
node *temp = queue->front;
while(temp != NULL)
{
printf("%c",temp->item );
temp = temp->next;
}
printf("\n");
}
void print_pq(pq *queue)
{
node *temp = queue->head;
while(temp != NULL)
{
printf("%c",temp->item );
temp = temp->next;
}
printf("\n");
}
void print_tree(node *head)
{
if(head != NULL)
{
printf("%c",head->item);
print_tree(head->left);
print_tree(head->right);
}
}
void print_tree_file(node *head,FILE *new)
{
unsigned char scape = '\\';
if(head != NULL)
{
if (head->left == NULL && head->right == NULL)
{
if (head->item == '*' || head->item == '\\')
{
fputc(scape,new);
}
}
fprintf(new,"%c",head->item);
print_tree_file(head->left,new);
print_tree_file(head->right,new);
}
}
void print_hash_table(hash_table *ht)
{
int i;
for (i = 65; i < 71; ++i)
{
printf("%c ->",i);
hn *temp = ht->table[i];
while(temp != NULL)
{
node *temp2 = temp->fila->front;
while (temp2 != NULL)
{
printf("%c",temp2->item);
temp2 = temp2->next;
}
temp = temp->next;
}
printf("\n");
}
}