diff --git a/U5 - Estruturas de dados lineares e flexíveis/c/celula.h b/U5 - Estruturas de dados lineares e flexíveis/c/celula.h new file mode 100644 index 0000000..13e326d --- /dev/null +++ b/U5 - Estruturas de dados lineares e flexíveis/c/celula.h @@ -0,0 +1,32 @@ + #ifndef CELULA_H + #define CELULA_H + //============================================================================= +#include +#include + //============================================================================= +typedef struct Celula{ + int elemento; + struct Celula *prox; +}Celula; +//============================================================================= +Celula* new_celula(int elemento){ + Celula *temp = (Celula*)malloc(sizeof(Celula)); + temp->elemento = elemento; + temp->prox = NULL; + return temp; +} + //============================================================================= + typedef struct CelulaDupla{ + int elemento; + struct CelulaDupla *prox, *ant; +}CelulaDupla; +//============================================================================= +CelulaDupla* new_celula_dupla(int elemento){ + CelulaDupla *temp = (CelulaDupla*)malloc(sizeof(CelulaDupla)); + temp->elemento = elemento; + temp->ant = NULL; + temp->prox = NULL; + return temp; +} + //============================================================================= +#endif \ No newline at end of file diff --git a/U5 - Estruturas de dados lineares e flexíveis/c/fila.h b/U5 - Estruturas de dados lineares e flexíveis/c/fila.h new file mode 100644 index 0000000..0a287c7 --- /dev/null +++ b/U5 - Estruturas de dados lineares e flexíveis/c/fila.h @@ -0,0 +1,62 @@ +#ifndef FILA_H +#define FILA_H +//============================================================================= +#include +#include +#include +#include +#include "celula.h" +//============================================================================= +typedef struct Fila{ + struct Celula *primeiro, *ultimo; + int size; +} Fila; +//============================================================================= +Fila new_fila(){ + Fila temp; + temp.primeiro = temp.ultimo = new_celula(-1); + temp.size = 0; + return temp; +} +//============================================================================= +int size_fila(Fila *f){ + return f->size; +} +//============================================================================= +void enqueue_fila(Fila *f, int elemento){ + f->ultimo->prox = new_celula(elemento); + f->ultimo = f->ultimo->prox; + f->size++; +} +//============================================================================= +int dequeue_fila(Fila *f){ + + if (f->primeiro == f->ultimo){ + printf("\nA fila esta vazia!\n"); + return INT_MIN; + } + + Celula *temp = f->primeiro; + f->primeiro = f->primeiro->prox; + f->size--; + free(temp); + return f->primeiro->elemento; +} +//============================================================================= +void print_fila(Fila *f){ + Celula *i; + printf("["); + for (i = f->primeiro->prox; i != NULL; i = i->prox) + { + printf("%d, ", i->elemento); + } + printf("] \n"); +} +//============================================================================= +void delete_fila(Fila *f){ + while(f->size > 0) + dequeue_fila(f); + free(f->primeiro); +} +//============================================================================= +#endif \ No newline at end of file diff --git a/U5 - Estruturas de dados lineares e flexíveis/c/fila_teste.c b/U5 - Estruturas de dados lineares e flexíveis/c/fila_teste.c new file mode 100644 index 0000000..1eb1d51 --- /dev/null +++ b/U5 - Estruturas de dados lineares e flexíveis/c/fila_teste.c @@ -0,0 +1,27 @@ +#include "fila.h" +//============================================================================= +int main() { + + Fila f; + int i, x1, x2, x3; + printf("==== FILA FLEXIVEL ====\n"); + + f = new_fila(); + + for(i=0; i < 10; i++) + enqueue_fila(&f, i); + + printf("Apos inserrir os dados: \n"); + print_fila(&f); + + x1 = dequeue_fila(&f); + x2 = dequeue_fila(&f); + x3 = dequeue_fila(&f); + + printf("Apos as remocoes (%d, %d, %d) \n", x1, x2, x3); + print_fila(&f); + + delete_fila(&f); + + return 0; +} \ No newline at end of file diff --git a/U5 - Estruturas de dados lineares e flexíveis/c/lista.h b/U5 - Estruturas de dados lineares e flexíveis/c/lista.h new file mode 100644 index 0000000..26ea2cb --- /dev/null +++ b/U5 - Estruturas de dados lineares e flexíveis/c/lista.h @@ -0,0 +1,123 @@ +#ifndef LISTA_H +#define LISTA_H +//============================================================================= +#include +#include +#include +#include +#include "celula.h" +//============================================================================= +typedef struct Lista{ + struct Celula *primeiro, *ultimo; + int size; +} Lista; +//============================================================================= +Lista new_lista(){ + Lista temp; + temp.primeiro = temp.ultimo = new_celula(-1); + temp.size = 0; + return temp; +} +//============================================================================= +int size_lista(Lista *l){ + return l->size; +} +//============================================================================= +void insert_begin(Lista *l, int elemento){ + + Celula *temp = new_celula(-1); + temp->prox = l->primeiro; + + l->primeiro->elemento = elemento; + l->primeiro = temp; + l->size++; +} +//============================================================================= +void insert_end(Lista *l, int elemento){ + l->ultimo->prox = new_celula(elemento); + l->ultimo = l->ultimo->prox; + l->size++; +} +//============================================================================= +void insert_at(Lista *l, int elemento, int pos){ + + if(pos < 0 || pos > l->size) + printf("Erro ao tentar inserir na posicao (%d/ tamanho = %d) invalida!", pos, l->size); + else if (pos == 0) + insert_begin(l, elemento); + else if (pos == l->size) + insert_end(l, elemento); + else{ + + Celula *ant = l->primeiro; + for(int i=0; iprox; + + Celula *temp = new_celula(elemento); + temp->prox = ant->prox; + ant->prox = temp; + l->size++; + } +} +//============================================================================= +int remove_at(Lista *l, int pos){ + + if(l->primeiro == l->ultimo){ + printf("\nA lista esta vazia!\n"); + return INT_MIN; + }else if(pos < 0 || pos > l->size-1) + printf("Erro ao tentar remover item da posicao (%d/ tamanho = %d) invalida!", pos, l->size); + else{ + + Celula *ant = l->primeiro; + for(int i=0; iprox; + + Celula *temp = ant->prox; + int elemento = temp->elemento; + + ant->prox = temp->prox; + free(temp); + + if(pos == l->size-1) + l->ultimo = ant; + + l->size--; + + return elemento; + } +} +//============================================================================= +int remove_begin(Lista *l){ + return remove_at(l, 0); +} +//============================================================================= +int remove_end(Lista *l){ + return remove_at(l, l->size-1); +} +//============================================================================= +bool pesquisar_lista(Lista *l, int elemento){ + Celula *i; + for (i = l->primeiro->prox; i != NULL; i = i->prox) + if(i->elemento == elemento) + return true; + return false; +} +//============================================================================= +void print_lista(Lista *l){ + Celula *i; + printf("["); + for (i = l->primeiro->prox; i != NULL; i = i->prox) + { + printf("%d, ", i->elemento); + } + printf("] \n"); +} +//============================================================================= +void delete_lista(Lista *l){ + while(l->size > 0) + remove_at(l,0); + free(l->primeiro); +} +//============================================================================= +#endif \ No newline at end of file diff --git a/U5 - Estruturas de dados lineares e flexíveis/c/lista_dupla.h b/U5 - Estruturas de dados lineares e flexíveis/c/lista_dupla.h new file mode 100644 index 0000000..31340ed --- /dev/null +++ b/U5 - Estruturas de dados lineares e flexíveis/c/lista_dupla.h @@ -0,0 +1,152 @@ +#ifndef LISTADUPLA_H +#define LISTADUPLA_H +//============================================================================= +#include +#include +#include +#include +#include "celula.h" +//============================================================================= +typedef struct ListaDupla{ + struct CelulaDupla *primeiro, *ultimo; + int size; +} ListaDupla; +//============================================================================= +ListaDupla new_lista_dupla(){ + ListaDupla temp; + temp.primeiro = temp.ultimo = new_celula_dupla(-1); + temp.size = 0; + return temp; +} +//============================================================================= +int size_lista_dupla(ListaDupla *l){ + return l->size; +} +//============================================================================= +void insert_begin_dupla(ListaDupla *l, int elemento){ + + CelulaDupla *temp = new_celula_dupla(-1); + temp->prox = l->primeiro; + + l->primeiro->elemento = elemento; + l->primeiro->ant = temp; + l->primeiro = temp; + l->size++; +} +//============================================================================= +void insert_end_dupla(ListaDupla *l, int elemento){ + l->ultimo->prox = new_celula_dupla(elemento); + l->ultimo->prox->ant = l->ultimo; + l->ultimo = l->ultimo->prox; + l->size++; +} +//============================================================================= +void insert_at_dupla(ListaDupla *l, int elemento, int pos){ + + if(pos < 0 || pos > l->size) + printf("Erro ao tentar inserir na posicao (%d/ tamanho = %d) invalida!", pos, l->size); + else if (pos == 0) + insert_begin_dupla(l, elemento); + else if (pos == l->size) + insert_end_dupla(l, elemento); + else{ + + CelulaDupla *ant = l->primeiro; + for(int i=0; iprox; + + CelulaDupla *temp = new_celula_dupla(elemento); + temp->prox = ant->prox; + temp->prox->ant = temp; + temp->ant = ant; + ant->prox = temp; + l->size++; + } +} +//============================================================================= +int remove_end_dupla(ListaDupla *l){ + + if(l->primeiro == l->ultimo){ + printf("\nA lista esta vazia!\n"); + return INT_MIN; + } + + CelulaDupla *temp = l->ultimo; + int elemento = temp->elemento; + + l->ultimo = l->ultimo->ant; + l->ultimo->prox = NULL; + l->size--; + + free(temp); + + return elemento; +} +//============================================================================= +int remove_at_dupla(ListaDupla *l, int pos){ + + if(l->primeiro == l->ultimo){ + printf("\nA lista esta vazia!\n"); + return INT_MIN; + }else if(pos < 0 || pos > l->size-1) + printf("Erro ao tentar remover item da posicao (%d/ tamanho = %d) invalida!", pos, l->size); + else if(pos == l->size-1) + remove_end_dupla(l); + else{ + + CelulaDupla *ant = l->primeiro; + for(int i=0; iprox; + + CelulaDupla *temp = ant->prox; + int elemento = temp->elemento; + + temp->prox->ant = ant; + ant->prox = temp->prox; + free(temp); + + l->size--; + + return elemento; + } +} +//============================================================================= +int remove_begin_dupla(ListaDupla *l){ + return remove_at_dupla(l, 0); +} +//============================================================================= +bool pesquisar_lista_dupla(ListaDupla *l, int elemento){ + CelulaDupla *i; + for (i = l->primeiro->prox; i != NULL; i = i->prox) + if(i->elemento == elemento) + return true; + return false; +} +//============================================================================= +void print_lista_dupla(ListaDupla *l){ + CelulaDupla *i; + printf("["); + for (i = l->primeiro->prox; i != NULL; i = i->prox) + { + printf("%d, ", i->elemento); + } + printf("] \n"); +} +//============================================================================= +void print_lista_dupla_inverso(ListaDupla *l){ + CelulaDupla *i; + printf("["); + for (i = l->ultimo; i != l->primeiro; i = i->ant) + { + printf("%d, ", i->elemento); + } + printf("] \n"); +} +//============================================================================= +void delete_lista_dupla(ListaDupla *l){ + while(l->size > 0) + remove_at_dupla(l,0); + free(l->primeiro); +} +//============================================================================= +#endif \ No newline at end of file diff --git a/U5 - Estruturas de dados lineares e flexíveis/c/lista_dupla_teste.c b/U5 - Estruturas de dados lineares e flexíveis/c/lista_dupla_teste.c new file mode 100644 index 0000000..2e01e51 --- /dev/null +++ b/U5 - Estruturas de dados lineares e flexíveis/c/lista_dupla_teste.c @@ -0,0 +1,51 @@ +#include "lista_dupla.h" +//============================================================================= +int main() { + + ListaDupla l; + int i, x1, x2, x3; + printf("==== LISTA FLEXIVEL ====\n"); + + l = new_lista_dupla(); + + for(i=0; i < 5; i++) + insert_end_dupla(&l, i); + + printf("Apos inserir os dados: \n"); + print_lista_dupla(&l); + print_lista_dupla_inverso(&l); + + printf("Apos inserir no inicio: \n"); + insert_begin_dupla(&l, i++); + print_lista_dupla(&l); + print_lista_dupla_inverso(&l); + + printf("Apos inserir no final: \n"); + insert_end_dupla(&l, i++); + print_lista_dupla(&l); + print_lista_dupla_inverso(&l); + + printf("Apos inserir na posicao 4: \n"); + insert_at_dupla(&l, i++, 4); + print_lista_dupla(&l); + print_lista_dupla_inverso(&l); + + printf("Apos remover no inicio: \n"); + x1 = remove_begin_dupla(&l); + print_lista_dupla(&l); + print_lista_dupla_inverso(&l); + + printf("Apos remover no final: \n"); + x1 = remove_end_dupla(&l); + print_lista_dupla(&l); + print_lista_dupla_inverso(&l); + + printf("Apos remover na posicao 2: \n"); + x1 = remove_at_dupla(&l, 2); + print_lista_dupla(&l); + print_lista_dupla_inverso(&l); + + delete_lista_dupla(&l); + + return 0; +} \ No newline at end of file diff --git a/U5 - Estruturas de dados lineares e flexíveis/c/lista_teste.c b/U5 - Estruturas de dados lineares e flexíveis/c/lista_teste.c new file mode 100644 index 0000000..01b92fe --- /dev/null +++ b/U5 - Estruturas de dados lineares e flexíveis/c/lista_teste.c @@ -0,0 +1,44 @@ +#include "lista.h" +//============================================================================= +int main() { + + Lista l; + int i, x1, x2, x3; + printf("==== LISTA FLEXIVEL ====\n"); + + l = new_lista(); + + for(i=0; i < 5; i++) + insert_end(&l, i); + + printf("Apos inserir os dados: \n"); + print_lista(&l); + + printf("Apos inserir no inicio: \n"); + insert_begin(&l, i++); + print_lista(&l); + + printf("Apos inserir no final: \n"); + insert_end(&l, i++); + print_lista(&l); + + printf("Apos inserir na posicao 4: \n"); + insert_at(&l, i++, 4); + print_lista(&l); + + printf("Apos remover no inicio: \n"); + x1 = remove_begin(&l); + print_lista(&l); + + printf("Apos remover no final: \n"); + x1 = remove_end(&l); + print_lista(&l); + + printf("Apos remover na posicao 2: \n"); + x1 = remove_at(&l, 2); + print_lista(&l); + + delete_lista(&l); + + return 0; +} \ No newline at end of file diff --git a/U5 - Estruturas de dados lineares e flexíveis/c/pilha.h b/U5 - Estruturas de dados lineares e flexíveis/c/pilha.h new file mode 100644 index 0000000..03cc4b9 --- /dev/null +++ b/U5 - Estruturas de dados lineares e flexíveis/c/pilha.h @@ -0,0 +1,61 @@ +#ifndef PILHA_H +#define PILHA_H +//============================================================================= +#include +#include +#include +#include +#include "celula.h" +//============================================================================= +typedef struct Pilha{ + struct Celula *topo; + int size; +} Pilha; +//============================================================================= +Pilha new_pilha(){ + Pilha temp; + temp.topo = NULL; + temp.size = 0; + return temp; +} +//============================================================================= +int size_pilha(Pilha *p){ + return p->size; +} +//============================================================================= +void push_pilha(Pilha *p, int elemento){ + Celula *temp = new_celula(elemento); + temp->prox = p->topo; + p->topo = temp; + p->size++; +} +//============================================================================= +int pop_pilha(Pilha *p){ + if (p->topo == NULL){ + printf("\nA pilha esta vazia!\n"); + return INT_MIN; + } + int elemento = p->topo->elemento; + Celula *temp = p->topo; + p->topo = p->topo->prox; + p->size--; + free(temp); + return elemento; +} +//============================================================================= +void print_pilha(Pilha *p){ + Celula *i; + printf("["); + for (i = p->topo; i != NULL; i = i->prox) + { + printf("%d, ", i->elemento); + } + printf("] \n"); +} +//============================================================================= +void delete_pilha(Pilha *p){ + while(p->size > 0) + pop_pilha(p); +} +//============================================================================= +#endif \ No newline at end of file diff --git a/U5 - Estruturas de dados lineares e flexíveis/c/pilha_teste.c b/U5 - Estruturas de dados lineares e flexíveis/c/pilha_teste.c new file mode 100644 index 0000000..5dc85a0 --- /dev/null +++ b/U5 - Estruturas de dados lineares e flexíveis/c/pilha_teste.c @@ -0,0 +1,27 @@ +#include "pilha.h" +//============================================================================= +int main() { + + Pilha p; + int i, x1, x2, x3; + printf("==== PILHA FLEXIVEL ====\n"); + + p = new_pilha(); + + for(i=0; i < 10; i++) + push_pilha(&p, i); + + printf("Apos inserrir os dados: \n"); + print_pilha(&p); + + x1 = pop_pilha(&p); + x2 = pop_pilha(&p); + x3 = pop_pilha(&p); + + printf("Apos as remocoes (%d, %d, %d) \n", x1, x2, x3); + print_pilha(&p); + + delete_pilha(&p); + + return 0; +} \ No newline at end of file