-
Notifications
You must be signed in to change notification settings - Fork 0
/
cheatsheet.c
310 lines (232 loc) · 4.87 KB
/
cheatsheet.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
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
/* TODO: this document will contain all the useful stuff for quick solving of the
tedious and repetitive stuff */
/************** LISTS ******************/
/* a more generic list implementation */
typedef struct node {
void *value;
struct node *next;
} node;
/* just a basic int list (better than nothing) */
typedef struct node {
int value;
struct node *next;
} node;
node *list_add(node *list, int value) {
node *n;
node *idx;
n = (node *)malloc(sizeof(node));
n->value = value;
n->next = NULL;
if (list == NULL) {
return n;
}
idx = list;
while (idx->next != NULL) {
idx = idx->next;
}
idx->next = n;
return list;
}
node *list_search(node *list, int value) {
node *idx;
idx = list;
while (idx) {
if (idx->value == value)
return idx;
idx = idx->next;
}
return NULL;
}
node *list_del(node *list, node *n) {
node *idx;
if (list == n) {
node *ret = list->next;
free(list);
return ret;
}
idx = list;
while (idx->next) {
if (idx->next == n) {
idx->next = idx->next->next;
free(n);
return list;
}
idx = idx->next;
}
return list;
}
void list_print(node *list) {
node *idx;
idx = list;
printf("\n[");
while(idx) {
printf("%d ", idx->value);
idx = idx->next;
}
printf("]\n");
}
void list_fprint(node *list, FILE *f) {
node *idx;
idx = list;
fprintf(f, "\n[");
while(idx) {
fprintf(f, "(%d,%d)", idx->r->a, idx->r->b);
idx = idx->next;
}
fprintf(f, "]\n");
fflush(f);
}
/*************** HEAPS *******************/
--> TODO
/************** STRINGS ******************/
--> TODO
/*************** STACK *****************/
#define MAX_STACK 1000000
typedef struct stack {
void* v[MAX_STACK];
int top;
} stack;
int push(stack *s, void *elem)
{
if (s->top == MAX_STACK - 1) {
printf("BUFFER OVERFLOW\n");
return -1;
}
s->v[s->top++] = elem;
return 0;
}
int pop(stack *s, void **elem)
{
if (s->top == 0) {
printf("BUFFER_UNDERFLOW\n");
return -1;
}
s->top--;
*elem = s->v[s->top];
return 0;
}
int empty(stack *s)
{
return s->top == 0;
}
int count(stack *s)
{
return s->top;
}
/* a specific stack implementation (int) */
#define MAX_STACK 1000000
typedef struct stack {
int v[MAX_STACK];
int top;
} stack;
int push(stack *s, int elem)
{
if (s->top == MAX_STACK) {
printf("BUFFER OVERFLOW\n");
return -1;
}
s->v[s->top] = elem;
s->top++;
return 0;
}
int pop(stack *s, int *elem)
{
if (s->top == 0) {
printf("BUFFER_UNDERFLOW\n");
return -1;
}
s->top--;
*elem = s->v[s->top];
return 0;
}
int empty(stack *s)
{
return s->top == 0;
}
int count(stack *s)
{
return s->top;
}
/*************** NON-RECURSIVE BACKTRACK **************/
/* pseudocode (using a stack) */
boolean solve(Node n) {
put node n on the stack;
while the stack is not empty {
if the node at the top of the stack is a leaf {
if it is a goal node, return true
else pop it off the stack
}
else {
if the node at the top of the stack has untried children
push the next untried child onto the stack
else pop the node off the stack
}
return false
}
/* generic C */
/******************* RECURSIVE BACKTRACK ****************/
/* pseudocode backtrack recursive */
boolean solve(Node n) {
if n is a leaf node {
if the leaf is a goal node, return true
else return false
} else {
for each child c of n {
if solve(c) succeeds, return true
}
return false
}
}
/* generic C */
typedef struct down {
//TODO
} down;
typedef struct up {
//TODO
} up;
up *rec(down *d)
{
int i, j;
up *u = (up *)malloc(sizeof(up));
#if 0
//print intermediate solution
sleep(1);
#endif
if (/* final solution */) {
//TODO
}
for (i = 0; i < /* TODO */; i++) {
for (j = 0; j < /* TODO */; j++) {
if (/* TODO */) {
down *dn = (down *)malloc(sizeof(down));
/* TODO: prepare dn using d*/
up *un = rec(dn);
if (/* TODO: analyze un*/) {
}
}
}
}
out:
free(d);
return u;
}
/*********************************************************/
/************** SORTING ***************/
#include <stdlib.h>
int compar(const void *a, const void *b)
{
int *aa = (int *)a;
int *bb = (int *)b;
return *aa > *bb;
}
qsort(a, n, sizeof(int), compar);
/************** UTILS ***************/
#define TODO 100
size_t lnsz = 0;
char *ser[TODO];
for (i = 0; i < TODO; i++)
ser[i] = NULL;
for (j = 0; j < TODO; j++) {
getline(&ser[j], &lnsz, fin);
printf("%d:%s ", j, ser[j]);
}