-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsimple_queue_example.c
More file actions
61 lines (53 loc) · 1.33 KB
/
simple_queue_example.c
File metadata and controls
61 lines (53 loc) · 1.33 KB
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
// gcc simple_queue_example.c -o simple_queue_example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "queue.h" // queue taken from FreeBSD 10
// The data type for the node
struct node
{
char c;
// This macro does the magic to point to other nodes
TAILQ_ENTRY(node) nodes;
};
int main (int arc, char * argv [])
{
// This macro creates the data type for the head of the queue
// for nodes of type 'struct node'
TAILQ_HEAD(head_s, node) head;
// Initialize the head before use
TAILQ_INIT(&head);
char string[] = "Hello World\n";
//
// put some nodes in the queue
//
struct node * e = NULL;
int c = 0;
for (c = 0; c < strlen(string); ++c)
{
e = malloc(sizeof(struct node));
if (e == NULL)
{
fprintf(stderr, "malloc failed");
exit(EXIT_FAILURE);
}
e->c = string[c];
// Actually insert the node e into the queue at the end
TAILQ_INSERT_TAIL(&head, e, nodes);
e = NULL;
}
// print the queue
TAILQ_FOREACH(e, &head, nodes)
{
printf("%c", e->c);
}
// free the elements from the queue
while (!TAILQ_EMPTY(&head))
{
e = TAILQ_FIRST(&head);
TAILQ_REMOVE(&head, e, nodes);
free(e);
e = NULL;
}
return EXIT_SUCCESS;
}