-
Notifications
You must be signed in to change notification settings - Fork 1
/
ByteQueue.c
166 lines (133 loc) · 3.84 KB
/
ByteQueue.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
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
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "ByteQueue.h"
ByteQueue Byte_Queue();
ByteQueue_node* malloc_ByteQueue_node(uint8_t);
void ByteQueue_append_left(ByteQueue*, uint8_t);
void ByteQueue_append_right(ByteQueue*, uint8_t);
uint8_t ByteQueue_pop_left(ByteQueue*);
uint8_t ByteQueue_pop_right(ByteQueue*);
uint8_t ByteQueue_peek_left(ByteQueue*);
uint8_t ByteQueue_peek_right(ByteQueue*);
void ByteQueue_free(ByteQueue*);
void ByteQueue_print(ByteQueue*);
void ByteQueue_print_hex(ByteQueue*);
ByteQueue Byte_Queue() {
ByteQueue queue;
queue.right = NULL;
queue.left = NULL;
queue.size = 0;
queue.append_left = ByteQueue_append_left;
queue.append_right = ByteQueue_append_right;
queue.pop_left = ByteQueue_pop_left;
queue.pop_right = ByteQueue_pop_right;
queue.peek_left = ByteQueue_peek_left;
queue.peek_right = ByteQueue_peek_right;
queue.free = ByteQueue_free;
queue.print = ByteQueue_print;
queue.print_hex = ByteQueue_print_hex;
return queue;
}
ByteQueue_node* malloc_ByteQueue_node(uint8_t value) {
ByteQueue_node* node = (ByteQueue_node*)malloc(sizeof(ByteQueue_node));
node -> value = value;
node -> left = NULL;
node -> right = NULL;
return node;
}
void ByteQueue_append_right(ByteQueue* queue, uint8_t value) {
ByteQueue_node* node = malloc_ByteQueue_node(value);
if (queue -> size == 0) {
queue -> right = node;
queue -> left = node;
} else {
node -> left = queue -> right;
queue -> right -> right = node;
queue -> right = node;
}
queue -> size += 1;
}
void ByteQueue_append_left(ByteQueue* queue, uint8_t value) {
ByteQueue_node* node = malloc_ByteQueue_node(value);
if (queue -> size == 0) {
queue -> right = node;
queue -> left = node;
} else {
node -> right = queue -> left;
queue -> left -> left = node;
queue -> left = node;
}
queue -> size += 1;
}
uint8_t ByteQueue_pop_right(ByteQueue* queue) {
if (queue -> size <= 0) {
return 0;
}
ByteQueue_node* del = queue -> right;
uint8_t value = queue -> right -> value;
if (queue -> size == 1) {
queue -> right = NULL;
queue -> left = NULL;
} else {
queue -> right = queue -> right -> left;
queue -> right -> right = NULL;
}
free(del);
queue -> size -= 1;
return value;
}
uint8_t ByteQueue_pop_left(ByteQueue* queue) {
if (queue -> size <= 0) {
return 0;
}
ByteQueue_node* del = queue -> left;
uint8_t value = queue -> left -> value;
if (queue -> size == 1) {
queue -> right = NULL;
queue -> left = NULL;
} else {
queue -> left = queue -> left -> right;
queue -> left -> left = NULL;
}
free(del);
queue -> size -= 1;
return value;
}
uint8_t ByteQueue_peek_right(ByteQueue* queue) {
if (queue -> size <= 0) {
return 0;
}
return queue -> right -> value;
}
uint8_t ByteQueue_peek_left(ByteQueue* queue) {
if (queue -> size <= 0) {
return 0;
}
return queue -> left -> value;
}
void ByteQueue_free(ByteQueue* queue) {
while (queue -> left != NULL) {
ByteQueue_pop_right(queue);
}
}
void ByteQueue_print(ByteQueue* queue) {
uint64_t size = queue -> size;
ByteQueue_node* node_ptr = queue -> left;
printf("left <=> ");
for (uint64_t i = 0; i < size; i++) {
printf("%02x <=> ", node_ptr -> value);
node_ptr = node_ptr -> right;
}
printf("right\n");
}
void ByteQueue_print_hex(ByteQueue* queue) {
uint64_t size = queue -> size;
ByteQueue_node* node_ptr = queue -> left;
printf("0x");
for (uint64_t i = 0; i < size; i++) {
printf("%02x", node_ptr -> value);
node_ptr = node_ptr -> right;
}
printf("\n");
}