-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
80 lines (73 loc) · 1.25 KB
/
queue.c
File metadata and controls
80 lines (73 loc) · 1.25 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
void init_q(Queue** q)
{
(*q) = (Queue*)malloc(sizeof(**q));
if (*q == NULL) {
exit(1);
}
(*q)->head = (*q)->tail = NULL;
(*q)->len = 0;
}
void free_q(Queue** q)
{
node_sq* iter = (*q)->head, * aux;
while (iter) {
aux = iter->next;
free(iter);
iter = aux;
}
free(*q);
}
int add_queue(Queue* q, int node_num)
{
node_sq* new = (node_sq*)malloc(sizeof(*new));
if (new == NULL) {
return 0;
}
new->data = node_num;
new->next = NULL;
if (q->head == NULL) {
q->head = q->tail = new;
}
else {
q->tail->next = new;
q->tail = new;
}
q->len++;
return 1;
}
int pop_queue(Queue* q)
{
if (q->head == NULL) {
return -1;
}
node_sq* nd = q->head;
int aux = q->head->data;
q->head=q->head->next;
q->len--;
free(nd);
return aux;
}
void print_q(Queue* q)
{
node_sq* iter = q->head;
while(iter) {
printf("%3d ", iter->data);
iter=iter->next;
}
puts("");
}
int is_empty_q(Queue* q)
{
return q->len==0;
return q->head==NULL;
}
void free_node(node_sq* nd)
{
if(nd == NULL){
return;
}
free(nd);
}