back to scratko.xyz
aboutsummaryrefslogtreecommitdiff
path: root/queue.c
blob: dd3dc1b3353d7d5abd3364d5b4c42738ccbfa83f (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
62
63
64
65
66
67
68
69
70
#include "queue.h"
#include <stdlib.h>

void queue_init(struct queue *q)
{
    q->first = NULL;
    q->last = NULL;
}

void queue_push(struct queue *q, const struct coordinates *data)
{
    struct item *new_element = malloc(sizeof(struct item));
    new_element->point = *data;
    new_element->next = NULL;
    if(!q->first)
        q->first = q->last = new_element;
    else {
        q->last->next = new_element;
        q->last = q->last->next;
    }
}

int empty(const struct queue *q)
{
    return q->first ? 0 : 1;
}

struct coordinates queue_front(const struct queue *q)
{
    return q->first->point;
}

void pop(struct queue *q)
{
    struct item *tmp = q->first;
    q->first = q->first->next;
    free(tmp);
}

int equal_points(struct coordinates first_point,
                        struct coordinates second_point)
{
    return first_point.x == second_point.x && first_point.y == second_point.y;
}

int is_consist_point(const struct queue *q, struct coordinates target_point)
{
    enum { false, true };
    struct item *tmp_item = q->first;
    struct coordinates tmp_point;
    while(tmp_item != NULL) {
        tmp_point = tmp_item->point;
        if(equal_points(tmp_point, target_point))
            return true;
        tmp_item = tmp_item->next;
    }
    return false;
}

void queue_clear(struct queue *q)
{
    struct item *tmp;
    while(q->first != NULL) {
       tmp = q->first;
       q->first = q->first->next;
       free(tmp);
    }
    q->first = NULL;
    q->last = NULL;
}