diff --git a/t1/libfila.c b/t1/libfila.c index 0341819ea38ca14cd3f5b7a87bf09b112fc102c0..f6a067a788c802c56824ab630df75aa40425b8e1 100644 --- a/t1/libfila.c +++ b/t1/libfila.c @@ -14,8 +14,9 @@ fila_t *cria_fila() { } fila_t *destroi_fila(fila_t *f) { - int i, e = 0, tamanho = tamanho_fila(f); - for (i = 0; i < tamanho; i++) { + int e = 0; + + while (f->ini != NULL) { retira_fila(f, &e); } diff --git a/t1/liblef.c b/t1/liblef.c index 4d53fd0b3c280f28fce47e3d6740c92fa7210e57..b1a35827f13eda0319e8b1dd3ba0fdd0ce3c4f80 100644 --- a/t1/liblef.c +++ b/t1/liblef.c @@ -1,26 +1,95 @@ #include <stdlib.h> +#include <string.h> +#include "liblef.h" lef_t *cria_lef() { - /*TODO: não implementado*/ - return NULL; + lef_t *l; + if ( !(l = malloc(sizeof(lef_t))) ) + return NULL; + + l->Primeiro = NULL; + + return l; } lef_t *destroi_lef(lef_t *l) { - /*TODO: não implementado*/ + nodo_lef_t *aux; + while (l->Primeiro != NULL) { + aux = l->Primeiro; + l->Primeiro = l->Primeiro->prox; + free(aux->evento); + free(aux); + } + + free(l); + return NULL; } -int adiciona_inicio_lef (lef_t *l, evento_t *evento) { - /*TODO: não implementado*/ - return 0; +int adiciona_inicio_lef(lef_t *l, evento_t *evento) { + nodo_lef_t *novo_nodo; + if ( !(novo_nodo = malloc(sizeof(nodo_lef_t))) ) + return 0; + + evento_t *novo_evento; + if ( !(novo_evento = malloc(sizeof(evento_t))) ) { + free(novo_nodo); + return 0; + } + + memcpy(novo_evento, evento, sizeof(evento_t)); + + novo_nodo->evento = novo_evento; + + novo_nodo->prox = l->Primeiro; + l->Primeiro = novo_nodo; + + return 1; } -int adiciona_ordem_lef (lef_t *l, evento_t *evento) { - /*TODO: não implementado*/ - return 0; +int adiciona_ordem_lef(lef_t *l, evento_t *evento) { + nodo_lef_t *novo_nodo; + if ( !(novo_nodo = malloc(sizeof(nodo_lef_t))) ) + return 0; + + evento_t *novo_evento; + if ( !(novo_evento = malloc(sizeof(evento_t))) ) { + free(novo_nodo); + return 0; + } + + memcpy(novo_evento, evento, sizeof(evento_t)); + + novo_nodo->prox = l->Primeiro; + novo_nodo->evento = novo_evento; + + if (novo_nodo->prox == NULL) { + l->Primeiro = novo_nodo; + return 1; + } + + nodo_lef_t *atual = l->Primeiro; + + while (atual->prox && atual->prox->evento->tempo < novo_nodo->evento->tempo) + atual = atual->prox; + + novo_nodo->prox = atual->prox; + atual->prox = novo_nodo; + + return 1; } -evento_t *obtem_primeiro_lef (lef_t *l) { - /*TODO: não implementado*/ - return NULL; +evento_t *obtem_primeiro_lef(lef_t *l) { + if (l->Primeiro == NULL) + return NULL; + + nodo_lef_t *aux = l->Primeiro; + + evento_t *e = l->Primeiro->evento; + + l->Primeiro = l->Primeiro->prox; + + free(aux); + + return e; } diff --git a/t1/teste b/t1/teste deleted file mode 100755 index 381c674840818c707f985538d6e15643b0e32691..0000000000000000000000000000000000000000 Binary files a/t1/teste and /dev/null differ diff --git a/t1/teste.c b/t1/teste.c deleted file mode 100644 index 764bb1605bd2b57ab5c8ff2c3dee9293a91a20d9..0000000000000000000000000000000000000000 --- a/t1/teste.c +++ /dev/null @@ -1,10 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -/* #include "libconjunto.h" */ -/* #include "libfila.h" */ -#include "liblef.h" - -int main() { - - return 0; -}