back to scratko.xyz
summaryrefslogtreecommitdiff
path: root/queue.c
blob: eed3cf8be82d97d49e3492c3f995637633ce5e81 (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
#include "queue.h"
#include <stdlib.h>

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

void queue_push(struct queue *q, char *word)
{
    struct word_item *tmp = malloc(sizeof(struct word_item));
    tmp->word = word;
    tmp->next = NULL;
    if(!q->first) {
        q->first = tmp;
        q->last = q->first;
    } else {
        q->last->next = tmp;
        q->last = q->last->next;
    }
}

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

#if 0
void queue_processing(const struct queue *q, void (*callback)(char*))
{
    struct word_item *tmp;
    tmp = q->first;
    while(tmp) {
        callback(tmp->word);
        tmp = tmp->next;
    }
}
#endif

int queue_get_word_count(const struct queue *q)
{
    struct word_item *tmp;
    int counter;
    for(counter = 0, tmp = q->first; tmp; tmp = tmp->next, ++counter)
    {}
    return counter;
}

void queue_copy_words_to_args(const struct queue *q, char **cmdline)
{
    struct word_item *tmp;
    int mas_idx;
    for(tmp = q->first, mas_idx = 0; tmp; tmp = tmp->next, ++mas_idx)
        cmdline[mas_idx] = tmp->word;
    cmdline[mas_idx] = NULL;
}