-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathestrutura.c
More file actions
95 lines (78 loc) · 1.78 KB
/
Copy pathestrutura.c
File metadata and controls
95 lines (78 loc) · 1.78 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
#include "estrutura.h"
#include <stdlib.h>
#include <stdio.h>
typedef struct node {
Requisicao* requisicao;
struct node* next;
} Node;
struct estrutura {
Node* front;
Node* rear;
int size;
};
Estrutura* create() {
Estrutura* e = (Estrutura*)malloc(sizeof(Estrutura));
if (e == NULL) {
fprintf(stderr, "Falha ao alocar memória em Estrutura\n");
return NULL;
}
e->front = NULL;
e->rear = NULL;
e->size = 0;
return e;
}
int inserir(Estrutura* e, Requisicao* r) {
if (e == NULL || r == NULL) {
return 0; // Failed
}
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
fprintf(stderr, "Falha ao alocar memória\n");
return 0; // Failed
}
newNode->requisicao = r;
newNode->next = NULL;
if (e->rear == NULL) {
e->front = e->rear = newNode;
} else {
e->rear->next = newNode;
e->rear = newNode;
}
e->size++;
return 1; // Success
}
Requisicao* remover(Estrutura* e) {
if (e == NULL || e->front == NULL) {
return NULL;
}
Node* temp = e->front;
Requisicao* r = temp->requisicao;
e->front = e->front->next;
if (e->front == NULL) {
e->rear = NULL;
}
free(temp);
e->size--;
return r;
}
int get_size(Estrutura* e) {
if (e == NULL) {
return -1; // Error
}
return e->size;
}
void destroy_queue(Estrutura* e) {
if (e == NULL) {
return;
}
while (e->front != NULL) {
Node* temp = e->front;
e->front = e->front->next;
// Libera a memória da requisição
if (temp->requisicao != NULL) {
libera(temp->requisicao);
}
free(temp);
}
free(e);
}