-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveToFront.c
More file actions
42 lines (36 loc) · 789 Bytes
/
MoveToFront.c
File metadata and controls
42 lines (36 loc) · 789 Bytes
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
#include <stdio.h>
#include <string.h>
#include "DoublyLinkedList.h"
list init_list(void) {
list l = {NULL};
for (int ch = 0; ch < 256; ++ch) {
insert_end(&l, ch);
}
return l;
}
void encode(void) {
int index, ch;
list l = init_list();
while ((ch = getchar()) != EOF) {
index = find_index(&l, ch);
delete(&l, ch);
insert_start(&l, ch);
putchar(index);
}
}
void decode(void) {
int index, ch;
list l = init_list();
while ((index = getchar()) != EOF) {
ch = delete_index(&l, index);
insert_start(&l, ch);
putchar(ch);
}
}
int main(int argc, char *argv[]) {
if (!strcmp(argv[1], "-"))
encode();
else if (!strcmp(argv[1], "+"))
decode();
return 0;
}