-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.h
77 lines (61 loc) · 1.24 KB
/
queue.h
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
71
72
73
74
75
76
77
#include <stdio.h>
typedef struct node {
char l;
struct node* next;
struct node* left;
struct node* right;
} node;
void print_node(node* head) {
printf("%c: ", head->l);
if (head->left != NULL)
printf("L-> %c ", head->left->l);
else
printf("L-> NULL ");
if (head->right != NULL)
printf("R-> %c ", head->right->l);
else
printf("R-> NULL ");
if (head->next != NULL)
printf("N-> %c\n", head->next->l);
else
printf("N-> NULL\n");
}
typedef struct queue {
node* head;
node* tail;
} queue;
void enqueue(queue* q, node* new_node)
{
if (new_node == NULL)
return;
if (q->head == NULL)
q->head = new_node;
else
q->tail->next = new_node;
q->tail = new_node;
}
node* dequeue(queue* q)
{
node* result = q->head;
// No elements
if (result == NULL)
return NULL;
// One element
if (result == q->tail || q->tail == NULL)
q->head = NULL;
// Two elements
if (result == q->tail)
q->tail = NULL;
// Normal case
q->head = result->next;
result->next = NULL;
return result;
}
void print_connections(node* parent)
{
print_node(parent);
if (parent->right != NULL)
print_connections(parent->right);
if (parent->left != NULL)
print_connections(parent->left);
}