-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathexample.c
129 lines (106 loc) · 2.44 KB
/
example.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
#include <stdio.h>
#include <stdlib.h>
#include <queue.h>
typedef struct {
char *test;
} test;
DEFINE_Q_GET(queue_get_test, test)
DEFINE_Q_DESTROY(queue_destroy_complete_test, test)
void free_test(test *t) {
if(t->test != NULL)
free(t->test);
free(t);
}
int cmp_int_ptr(int *a, int *b) {
if(*a < *b)
return -1;
else if(*a > *b)
return 1;
else
return 0;
}
void unsorted_mode() {
queue_t *q = queue_create();
char *t1 = (char *)malloc(1);
char *t2 = (char *)malloc(2);
char *t3 = (char *)malloc(4);
char *t4 = (char *)malloc(8);
test *s1 = (test *)malloc(sizeof(test));
s1->test = t1;
test *s2 = (test *)malloc(sizeof(test));
s2->test = t2;
test *s3 = (test *)malloc(sizeof(test));
s3->test = t3;
test *s4 = (test *)malloc(sizeof(test));
s4->test = t4;
queue_put(q, s1);
queue_put(q, s2);
queue_put(q, s3);
queue_put(q, s4);
test *t;
queue_get_test(q, &t);
free_test(t);
queue_get_test(q, &t);
free_test(t);
queue_destroy_complete_test(q, free_test);
}
void sorted_mode() {
queue_t *q = queue_create_sorted(1, (int (*)(void *, void *))cmp_int_ptr);
int *t1 = (int *)malloc(sizeof(int));
int *t2 = (int *)malloc(sizeof(int));
int *t3 = (int *)malloc(sizeof(int));
int *t4 = (int *)malloc(sizeof(int));
*t1 = 10;
*t2 = 12;
*t3 = 1;
*t4 = 1;
queue_put(q, t1);
queue_put(q, t2);
queue_put(q, t3);
queue_put(q, t4);
int *t;
queue_get(q, (void **)&t);
printf("first int %i\n", *t);
free(t);
queue_get(q, (void **)&t);
printf("second int %i\n", *t);
free(t);
queue_get(q, (void **)&t);
printf("third int %i\n", *t);
free(t);
queue_get(q, (void **)&t);
printf("fourth int %i\n", *t);
free(t);
queue_destroy_complete(q, NULL);
}
void sorted2_mode() {
queue_t *q = queue_create_limited_sorted(3, 1, (int (*)(void *, void *))cmp_int_ptr);
int t1 = 1;
queue_put(q, &t1);
int t2 = 15;
queue_put(q, &t2);
int t3 = 3;
queue_put(q, &t3);
int t4 = 27;
queue_put(q, &t4);
int t5 = 9;
queue_put(q, &t5);
int *i;
queue_get(q, (void **)&i);
printf("first element was %d\n", *i);
queue_get(q, (void **)&i);
printf("second element was %d\n", *i);
queue_get(q, (void **)&i);
printf("third element was %d\n", *i);
queue_get(q, (void **)&i);
printf("fourth element was %p\n", i);
queue_get(q, (void **)&i);
printf("fifth element was %p\n", i);
queue_destroy_complete(q, NULL);
}
int main(int argc, char *argv[]) {
unsorted_mode();
sorted_mode();
sorted2_mode();
return 0;
}