-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fila.c
executable file
·54 lines (46 loc) · 1.1 KB
/
Fila.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
#include <stdio.h>
#include <stdlib.h>
#include "Fila.h"
//Queue* first;
//Queue* last;
/*int main(void) {
printf("1\n");
Queue *q = init_queue();
printf("2\n");
enqueue(q, "a");
printf("3\n");
printf("dequeue: %s\n", dequeue(q));
printf("4\n");
return 0;
}*/
Queue *init_queue(void) {
Queue *nova = (Queue *) malloc(sizeof(Queue));
Queue_elmt *elmt = (Queue_elmt*)malloc(sizeof(Queue_elmt));
elmt->value = NULL;
elmt->next = NULL;
nova->first = nova->last = elmt;
return nova;
}
Queue_elmt* newQueue_elmt(void) {
Queue_elmt* elmt = (Queue_elmt*)malloc(sizeof(Queue_elmt));
elmt->value = NULL;
elmt->next = NULL;
return elmt;
}
void enqueue(Queue *q, char* value){
q->last->value = value;
q->last->next = newQueue_elmt();
q->last = q->last->next;
}
char *dequeue(Queue *q) {
if (is_queue_empty(q))
return NULL;
char* value = q->first->value;
Queue_elmt* next = q->first->next;
free(q->first);
q->first = q->first->next;
return value;
}
int is_queue_empty(Queue *q) {
return q->first->value == NULL;
}