aboutsummaryrefslogtreecommitdiff
path: root/deque.c
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2020-08-12 17:35:02 +0200
committerThomas Schmucker <ts@its1.de>2020-08-12 17:35:02 +0200
commit024cb7031b29c3d6b2f3a42ca18116195d30ea61 (patch)
tree227780dd986aa177449b144277633a20f6b3dfe2 /deque.c
parentd7011ee21a8e5a26367cb7201a0e24b1b79bcdcb (diff)
downloaddata-structures-024cb7031b29c3d6b2f3a42ca18116195d30ea61.tar.gz
data-structures-024cb7031b29c3d6b2f3a42ca18116195d30ea61.tar.bz2
data-structures-024cb7031b29c3d6b2f3a42ca18116195d30ea61.zip
Neustart der Implementierung von Deque's
Diffstat (limited to 'deque.c')
-rw-r--r--deque.c124
1 files changed, 21 insertions, 103 deletions
diff --git a/deque.c b/deque.c
index 7125764..22e2d82 100644
--- a/deque.c
+++ b/deque.c
@@ -1,125 +1,43 @@
1#include <stdbool.h>
1#include <stdio.h> 2#include <stdio.h>
2#include <stdlib.h> 3#include <stdlib.h>
3#include <stdbool.h>
4 4
5#define NELEM(x) (sizeof(x) / sizeof(x[0])) 5#define NELEM(x) (sizeof(x) / sizeof(x[0]))
6 6
7typedef int T; 7typedef int T;
8 8
9struct chunk { 9#define START_CAPACITY 8
10 int head, tail;
11 T array[8];
12};
13
14static void
15chunk_init(struct chunk *c)
16{
17 c->head = 0;
18 c->tail = 0;
19}
20
21static bool
22chunk_put(struct chunk *c, T data)
23{
24 const int next = (c->head + 1) % NELEM(c->array);
25
26 if ( next == c->tail ) /* full? */
27 return false;
28
29 c->array[c->head] = data;
30 c->head = next;
31
32 return true;
33}
34
35static bool
36chunk_get(struct chunk *c, T *data)
37{
38 if ( c->head == c->tail ) /* empty? */
39 return false;
40
41 const int next = (c->tail + 1) % NELEM(c->array);
42
43 *data = c->array[c->tail];
44 c->tail = next;
45
46 return true;
47}
48
49static int
50chunk_size(struct chunk *c)
51{
52 return (c->head + NELEM(c->array) - c->tail) % NELEM(c->array);
53}
54
55static bool
56chunk_full(struct chunk *c)
57{
58 return ((c->head + 1) % NELEM(c->array)) == c->tail;
59}
60
61static bool
62chunk_empty(struct chunk *c)
63{
64 return c->head == c->tail;
65}
66
67static T*
68chunk_at(struct chunk *c, int idx)
69{
70 if ( idx < 0 || idx >= chunk_size(c) ) /* invalid index? */
71 return NULL;
72
73 return &c->array[(c->head + idx) % NELEM(c->array)];
74}
75
76/* ================= */
77 10
78struct deque { 11struct deque {
79 int head, tail, capacity; 12 T ** chunks;
80 13 size_t head;
81 struct chunk **chunks; 14 size_t tail;
15 size_t capacity;
82}; 16};
83 17
84void 18void
85deque_init(struct deque *d) 19deque_init(struct deque *d)
86{ 20{
87 int i; 21 d->head = 0;
88 22 d->tail = 0;
89 d->head = 0; 23 d->capacity = START_CAPACITY;
90 d->tail = 0;
91 d->capacity = 1;
92 24
93 d->chunks = calloc(d->capacity, sizeof(struct chunk *)); 25 d->chunks = calloc(d->capacity, sizeof(*d->chunks));
94
95 for ( i = 0; i != d->capacity; ++i ) {
96 d->chunks[i] = malloc(sizeof(struct chunk));
97 chunk_init(d->chunks[i]);
98 }
99} 26}
100 27
101 28void
102int main(void) 29deque_free(struct deque *d)
103{ 30{
104 struct chunk c; 31 free(d->chunks);
105 int data; 32}
106
107 chunk_init(&c);
108 33
109 chunk_put(&c, '0'); 34int
110 chunk_put(&c, '0'); 35main(void)
111 chunk_get(&c, &data); 36{
112 chunk_get(&c, &data); 37 struct deque c;
113 38
114 chunk_put(&c, 'A'); 39 deque_init(&c);
115 chunk_put(&c, 'B'); 40 deque_free(&c);
116 chunk_put(&c, 'C');
117 chunk_put(&c, 'D');
118 chunk_put(&c, 'E');
119 chunk_put(&c, 'F');
120 chunk_put(&c, 'G');
121 41
122 printf("head: %d -- tail: %d -- size: %d\n", c.head, c.tail, chunk_size(&c));
123 return 0; 42 return 0;
124} 43}
125