-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlef.c
146 lines (106 loc) · 2.93 KB
/
lef.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
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
141
142
143
144
145
146
#include "stdlib.h"
#include "stdio.h"
#include "lef.h"
/*Cria os eventos com os parametros informados
*Retorna um ponteiro para o evento ou NULL se falhar*/
struct evento_t *cria_evento (int tempo, int tipo, int dado1, int dado2){
struct evento_t *evento;
evento = malloc(sizeof(struct evento_t));
if (!evento)
return NULL;
evento->tempo = tempo;
evento->tipo = tipo;
evento->dado1 = dado1;
evento->dado2 = dado2;
return evento;
}
/*Destroi um evento e retorna NULL.*/
struct evento_t *destroi_evento (struct evento_t *e){
free(e);
e = NULL;
return e;
}
/*Cria Nodo Cabeça e retorna NUll se nao foi alocado */
struct lef_t *cria_lef (){
struct lef_t *lef;
lef = malloc(sizeof(struct lef_t));
if (!lef)
return NULL;
lef->primeiro = NULL;
return lef;
}
/*Desaloca toda memoria da lef e faz lef receber NULL. */
struct lef_t *destroi_lef (struct lef_t *l){
struct nodo_lef_t *nodo,*aux;
nodo = l->primeiro;
while ( nodo != NULL){
aux = nodo;
nodo = nodo->prox;
free(aux->evento);
free (aux);
}
free(l);
l = NULL;
return l;
}
/*Insere o evento apontado na LEF na ordem de tempos crescentes.
*Caso sejam iguais respeitam FIFO
*Retorna 1 em caso de sucesso ou 0 caso contrario.*/
int insere_lef (struct lef_t *l, struct evento_t *e){
struct nodo_lef_t *novo,*aux;
novo = malloc(sizeof(struct nodo_lef_t));
if (!novo)
return 0;
novo->evento = e;
aux = l->primeiro;
/*fila fazia*/
if (vazia_lef(l)){
l->primeiro = novo;
novo->prox = NULL;
return 1;
}
/*inserir o menor tempo na fila*/
if(e->tempo < aux->evento->tempo){
novo->prox = aux;
l->primeiro = novo;
return 1;
}
while (aux->prox != NULL && aux->prox->evento->tempo <= novo->evento->tempo)
aux = aux->prox;
novo->prox = aux->prox;
aux->prox = novo;
return 1;
}
/* Retira o primeiro evento da LEF.
* Retorna ponteiro para o evento ou NULL se falhar.*/
struct evento_t *retira_lef (struct lef_t *l){
struct nodo_lef_t *aux;
struct evento_t *e;
if (vazia_lef(l))
return NULL;
e = l->primeiro->evento;
aux = l->primeiro;
l->primeiro = aux->prox;
free(aux);
return e;
}
/*Retorna 1 se fila estiver vazia, caso contrario retorna 0*/
int vazia_lef (struct lef_t *l){
if (l->primeiro == NULL)
return 1;
return 0;
}
void imprime_lef (struct lef_t *l){
int evento;
struct evento_t *e;
struct nodo_lef_t *aux;
evento = 0;
aux = l->primeiro;
while (aux != NULL){
e = aux->evento;
printf("tempo %d tipo %d d1 %d d2 %d\n", e->tempo,e->tipo,e->dado1,e->dado2);
aux = aux->prox;
evento++;
}
printf("total %d eventos\n", evento);
}