back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/server/card_queue.c
blob: 194ea0ce0a196bd469dc259d8e5091b5630af6eb (plain)
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
#include "card_queue.h"

#include <stdlib.h>

void init_queue(struct card_queue *cq)
{
    cq->first = NULL;
    cq->last = NULL;
}

void push_queue(struct card_queue *cq, const char *str)
{
    struct card_queue_item *tmp = malloc(sizeof(struct card_queue_item));
    tmp->str = str;
    tmp->next = NULL;
    if(!cq->first) {
        cq->first = tmp;
        cq->last = tmp;
    } else {
        cq->last->next = tmp;
        cq->last = tmp;
    }
}

struct card_queue_item* get_next_card_from_queue(struct card_queue *cq,
                                                 struct card_queue_item *prev)
{
    if(prev == NULL)
        return cq->first;
    else
        return prev->next;
}

int is_empty_queue(struct card_queue *cq)
{
    return !cq->first;
}

const char* pop_card_queue(struct card_queue *cq)
{
    struct card_queue_item *tmp = NULL;
    const char *card = NULL;

    tmp = cq->first;
    cq->first = cq->first->next;
    card = tmp->str;
    free(tmp);
    return card;
}

int find_out_card_quantity_in_cq(const struct card_queue *cq)
{
    int counter = 0;
    struct card_queue_item *tmp = cq->first;

    while(tmp) {
        ++counter;
        tmp = tmp->next;
    }
    return counter;
}