Вы находитесь на странице: 1из 5
swanora Circular queve in C | Martin Broadhurst Martin Broadhurst CIRCULAR QUEUE INC AUGUST 23, 2016 | MARTIN. circular queue, or ring buffer is an array that wraps around, so that it can be used as a queue without walking backwards in memory. This implementation reallocates the buffer when it becomes full (i.e., when the head and tail of the queue meet). The header file: wWaNVaunawne 108 1 12 B 14 15 16 17 18 19 20 21 22 23 #ifndef CIRQUE_H #define CIRQUE_H struct cirque { unsigned int head; /* First element */ unsigned int tail; /* 1 past the last element */ unsigned int is_full; void ** entries; unsigned int size; typedef struct cirque cirque; typedef void (*cirque_forfn)(void*); cirque * cirque_create(void); void cirque_delete(cirque * queue) ; unsigned int cirque_insert(cirque * queue, void * data); void * cirque_remove(cirque * queue); void *cirque_peek(const cirque * queue); unsigned int cirque_get_count(const cirque * queue); void cirque_for_each(const cirque * queue, cirque_forfn fun); endif /* CIRQUE_H */ Implementation: 1 2 3 4 5 #include #include cirque * cirque_create(void) wm martinoreadhurst comieirque-in-c hl 1s swanora Circular queve in C | Martin Broadhurst const unsigned int size = 4; cirque * queue = malloc(sizeof(cirque)); if (queue) { queue->entries = malloc(size * sizeof(void *)); if (queue->entries) { queue->size = si queue->head queue->tail queue->is full else { free(queue) ; queue = NULL; y + return queue; + void cirque_delete(cirque * queue) if (queue) { free(queue->entries) ; free(queue) ; y static void cirque_resize(cirque * queue) void **temp = malloc(queue->size * 2 * sizeof(void *)); if (temp) { unsigned int i = 0; unsigned int h = queue->head; do { temp[i] = queue->entries[h++]; if (h == queue->size) { h = 5 } ints } while (h != queue->tail); Free(queue->entries); queue->entries = temp; queue->head = 0; queue->tail = queue->size; queue->size *= 2; queue->is_full = 0; + static unsigned int cirque_is_empty(const cirque * queue) return (queue->head queue->tail) && !queue->is_full; } unsigned int cirque_insert(cirque * queue, void * data) unsigned int result; wm martinoreadhurst comieirque-in-c hl 26 swanora 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 28 89 90 91 92 93 94 95, 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 ai 112 113 114 115 116 117 118 119 Ceuta queve nC | Martin Broadhurst if (queue->is_full) { cirque_resize(queue) ; if (queue->is full) { result = + } if (Iqueue->is_full) { queue->entries[queue->tail++] = data; if (queue->tail == queue->size) { queue->tail = @; if (queue->tail == queue->head) { queue->is_full = 1; + result = 1; x return result; + void * cirque_remove(cirque * queue) void * data = NULL; if (Icirque_is_empty(queue)) { if (queue->is_full) { queue->is full } data = queue->entries[queue->head++]; if (queue->head == queue->size) { queue->head = @; } return data; + void *cirque_peek(const cirque * queue) { void *data = NULL; if (Icirque_is_empty(queue)) { data = queue->entries[queue->head]; return data; + unsigned int cirque_get_count(const cirque * queue) { unsigned int count; if (cirque_is_empty(queue)) { count = 05 } else if (queue->is_full) { count = queue->size; else if (queue->tail > queue->head) { count = queue->tail - queue->head; else { count = queue->size - queue->head; wm martinoreadhurst comieirque-in-c hl a8

Вам также может понравиться