diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-08-12 17:35:02 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-08-12 17:35:02 +0200 |
| commit | 024cb7031b29c3d6b2f3a42ca18116195d30ea61 (patch) | |
| tree | 227780dd986aa177449b144277633a20f6b3dfe2 /deque.c | |
| parent | d7011ee21a8e5a26367cb7201a0e24b1b79bcdcb (diff) | |
| download | data-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.c | 124 |
1 files changed, 21 insertions, 103 deletions
| @@ -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 | ||
| 7 | typedef int T; | 7 | typedef int T; |
| 8 | 8 | ||
| 9 | struct chunk { | 9 | #define START_CAPACITY 8 |
| 10 | int head, tail; | ||
| 11 | T array[8]; | ||
| 12 | }; | ||
| 13 | |||
| 14 | static void | ||
| 15 | chunk_init(struct chunk *c) | ||
| 16 | { | ||
| 17 | c->head = 0; | ||
| 18 | c->tail = 0; | ||
| 19 | } | ||
| 20 | |||
| 21 | static bool | ||
| 22 | chunk_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 | |||
| 35 | static bool | ||
| 36 | chunk_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 | |||
| 49 | static int | ||
| 50 | chunk_size(struct chunk *c) | ||
| 51 | { | ||
| 52 | return (c->head + NELEM(c->array) - c->tail) % NELEM(c->array); | ||
| 53 | } | ||
| 54 | |||
| 55 | static bool | ||
| 56 | chunk_full(struct chunk *c) | ||
| 57 | { | ||
| 58 | return ((c->head + 1) % NELEM(c->array)) == c->tail; | ||
| 59 | } | ||
| 60 | |||
| 61 | static bool | ||
| 62 | chunk_empty(struct chunk *c) | ||
| 63 | { | ||
| 64 | return c->head == c->tail; | ||
| 65 | } | ||
| 66 | |||
| 67 | static T* | ||
| 68 | chunk_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 | ||
| 78 | struct deque { | 11 | struct 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 | ||
| 84 | void | 18 | void |
| 85 | deque_init(struct deque *d) | 19 | deque_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 | 28 | void | |
| 102 | int main(void) | 29 | deque_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'); | 34 | int |
| 110 | chunk_put(&c, '0'); | 35 | main(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 | |||
