Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include "some_queue.h"
- queue *new_queue(void) {
- queue *q = malloc(sizeof(queue));
- if (!q)
- return NULL;
- q->elem_c = 0;
- q->curr_i = 0;
- q->q_head = NULL;
- return q;
- }
- void end_queue(queue *q) {
- if (q) {
- free(q->q_head);
- q->q_head = NULL;
- free(q);
- q = NULL;
- }
- }
- void enqueue(queue *q, int type, void *val) {
- if (!q)
- return;
- q->q_head = realloc(q->q_head, (sizeof(item_t) * (q->elem_c + 1)));
- (q->q_head + q->elem_c)->type = type;
- (q->q_head + q->elem_c)->value = val;
- ++q->elem_c;
- }
- item_t *dequeue(queue *q) {
- if (!q || q->elem_c == 0)
- return NULL;
- return ((q->q_head) + (q->curr_i++));
- }
- item_t *queue_peek(queue *q) {
- if (!q || (q->elem_c == 0))
- return NULL;
- return (q->q_head + q->curr_i);
- }
- uint32 queue_size(queue *q) {
- return (q) ? q->elem_c : (uint32)-1;
- }
- int queue_next(queue *q) {
- if (!q || ((q->elem_c > 0) && (q->curr_i == q->elem_c - 1)))
- return 0;
- ++q->curr_i;
- return 1;
- }
- int queue_back(queue *q) {
- if (!q || (q->curr_i == 0))
- return 0;
- --q->curr_i;
- return 1;
- }
- int queue_first(queue *q) {
- if (!q)
- return 0;
- q->curr_i = 0;
- return 1;
- }
- int queue_last(queue *q) {
- if (!q)
- return 0;
- q->curr_i = q->elem_c - 1;
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement