-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
140 lines (120 loc) · 2.5 KB
/
queue.c
File metadata and controls
140 lines (120 loc) · 2.5 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* queue.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: passef <passef@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/01/19 22:11:00 by passef #+# #+# */
/* Updated: 2018/01/19 23:26:27 by passef ### ########.fr */
/* */
/* ************************************************************************** */
#include "queue.h"
int is_empty(void)
{
if (first == NULL && last == NULL)
return (1);
return (0);
}
int queue_lenght(void)
{
return nbItem;
}
t_queue *queue_first(void)
{
if (is_empty())
return (0);
return (first);
}
t_queue *queue_last(void)
{
if (is_empty())
return (0);
return (last);
}
void print_queue(void)
{
if (is_empty())
{
printf("rien a afficher\n");
return ;
}
t_queue *tmp = first;
while (tmp != NULL)
{
printf("x : %d y : %d\n", tmp->x, tmp->y);
tmp = tmp->next;
}
}
void push_queue(int x, int y)
{
t_queue *item;
item = malloc(sizeof(*item));
if (item == NULL)
{
printf("pb malloc\n");
return ;
}
item->x = x;
item->y = y;
item->next = NULL;
if (is_empty())
{
first = item;
last = item;
}
else
{
last->next = item;
last = item;
}
nbItem++;
}
void pop_queue(void)
{
if (is_empty())
{
printf("file vide, nothing to pop\n");
return ;
}
t_queue *tmp = first;
if (first == last)
{
first = NULL;
last = NULL;
}
else
first = first->next;
free(tmp);
nbItem--;
}
void clear_queue(void)
{
if (is_empty())
{
printf("Rien a nettoyer, file vide\n");
return ;
}
while (!is_empty())
pop_queue();
}
int main(void)
{
printf("taille de la file : %d\n", queue_lenght());
print_queue();
printf("cmd push\n");
push_queue(10, 20);
push_queue(35, 55);
push_queue(88, 66);
printf("Taille de la file : %d\n", queue_lenght());
print_queue();
printf("cmd clear\n");
clear_queue();
printf("Taille de la file : %d\n", queue_lenght());
print_queue();
if (is_empty())
printf("Check : file vide\n");
else
printf("Check : non vide\n");
return (0);
}