-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
75 lines (50 loc) · 1.44 KB
/
queue.c
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
//
// expr_linked_list.c
//
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include "scanner/scanner.h"
#include "queue.h"
static struct QElement *QE_alloc(void);
struct QState *queue_new() {
struct QState *initial_state = (struct QState *) malloc(sizeof(struct QState));
if(initial_state == NULL)
printf("fehler new_queue...");
return initial_state;
}
void queue_enqueue(struct QState *state, void *item) {
struct QElement *it = QE_alloc();
if(state->head == NULL) {
it->item = item;
state->traversal = state->head = state->last = it;
} else {
it->item = item;
state->last->next = it;
state->last = it;
}
}
void *queue_dequeue(struct QState *state) {
struct QElement *it = state->traversal;
if(it == NULL)
return NULL;
state->traversal = state->traversal->next;
return it->item;
}
void queue_reset(struct QState *state) {
state->traversal = state->head;
}
void queue_test(struct QState *state, void (*callback)(void *it)) {
void *it;
struct QState *initial_state = &(*state);
while ((it = queue_dequeue(initial_state)) != NULL) {
callback(it);
}
}
static struct QElement *QE_alloc() {
struct QElement *elem = (struct QElement *) malloc(sizeof(struct QElement));
if(elem == NULL)
printf("kein speicher für QElem");
return elem;
}