From 1f4b43a61eabfb939ba3994d9efb2debaa6fdc8c Mon Sep 17 00:00:00 2001 From: LucasSNeves <37223189+LucasSNeves@users.noreply.github.com> Date: Wed, 23 Oct 2019 16:28:15 -0300 Subject: [PATCH 1/3] main function in list structure --- lists.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 lists.c diff --git a/lists.c b/lists.c new file mode 100644 index 0000000..03825bf --- /dev/null +++ b/lists.c @@ -0,0 +1,83 @@ +#include +#include + +//This prog teachs how to use structs of list in c + +//Main struct of all structs + +typedef struct no +{ + struct no * next; + int n; +}NODE; + +NODE * cria_no(int n); +void print_no(NODE * l); + + +//list progs +NODE * insert_lista (NODE * l,int n); +NODE * delete_lista(NODE * l,int n); + +int main (){ + int n,i; + + NODE * lista = NULL; + + while(scanf("%d",&n)!=EOF){ + lista = insert_lista (lista,n); + getchar(); + } + + printf("Numeros inseridos\n"); + print_no(lista); + + + while(scanf("%d",&n)!=EOF){ + lista = delete_lista (lista,n); + print_no(lista); + } + return 0; +} + + +NODE * cria_no(int n){ + NODE * new = malloc(sizeof(NODE)); + if (new!= NULL){ + new->next = NULL; + new->n = n; + return new; + } + exit(1); +} + +void print_no(NODE * l){ + if (l !=NULL){ + printf("%d ",l->n ); + print_no(l->next); + }else { + printf("\n"); + } +} + +///***LISTA***** + +NODE * insert_lista (NODE * l,int n){ + if (l == NULL) return cria_no(n); + else{ + l->next = insert_lista(l->next,n); + } + return l; +} + +NODE * delete_lista(NODE * l,int n){ + if (l == NULL) return NULL; + else if (l->n == n){ + NODE * tmp = l; + l = l->next; + free(tmp); + }else{ + l->next = delete_lista(l->next,n); + } + return l; +} From c3c61c4d749a34696625c2fe61606dbe2f8a38fa Mon Sep 17 00:00:00 2001 From: LucasSNeves <37223189+LucasSNeves@users.noreply.github.com> Date: Wed, 23 Oct 2019 16:30:08 -0300 Subject: [PATCH 2/3] main functions in a stack data struct --- stacks.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 stacks.c diff --git a/stacks.c b/stacks.c new file mode 100644 index 0000000..a2b263e --- /dev/null +++ b/stacks.c @@ -0,0 +1,80 @@ +#include +#include + +//This prog teachs how to use structs of Stacks in c + +//Main struct of all structs + +typedef struct no +{ + struct no * next; + int n; +}NODE; + +//auxiliar functions +NODE * cria_no(int n); +void print_no(NODE * l); + +//stacks progs +NODE * insert_pilha (NODE * p,int n); +NODE * delete_pilha (NODE * p); + +int main (){ + int n,i; + + NODE * pilha = NULL; + + while(scanf("%d",&n)!=EOF){ + pilha = insert_lista (pilha,n); + getchar(); + } + + printf("Numeros inseridos\n"); + print_no(pilha); + + + while(scanf("%d",&n)!=EOF){ + lista = delete_pilha (lista,n); + print_no(pilha); + } + return 0; +} + + +NODE * cria_no(int n){ + NODE * new = malloc(sizeof(NODE)); + if (new!= NULL){ + new->next = NULL; + new->n = n; + return new; + } + exit(1); +} + +void print_no(NODE * l){ + if (l !=NULL){ + printf("%d ",l->n ); + print_no(l->next); + }else { + printf("\n"); + } +} + + +///***PILHA***** + +NODE * insert_pilha (NODE * p,int n){ + if (p == NULL) return cria_no(n); + else{ + NODE * aux = cria_no(n); + aux->next = p; + return aux; + } +} +NODE * delete_pilha (NODE * p){ + if (p!=NULL){ + NODE * aux = p; + p = p->next; + free(aux); + } +} From e02cd73b37f4958c2979c9e08a8567a5367379e1 Mon Sep 17 00:00:00 2001 From: LucasSNeves <37223189+LucasSNeves@users.noreply.github.com> Date: Wed, 23 Oct 2019 16:34:17 -0300 Subject: [PATCH 3/3] main functions in a Queue data struct --- queue.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 queue.c diff --git a/queue.c b/queue.c new file mode 100644 index 0000000..1c490a3 --- /dev/null +++ b/queue.c @@ -0,0 +1,107 @@ +#include +#include + +//This prog teachs how to use structs of Queue in c + +//Main struct of all structs + +typedef struct no +{ + struct no * next; + int n; +}NODE; + + +//expecific struct used in queue programs +typedef struct fila +{ + NODE * begin,*final; +}FILA; + +//auxiliar functions +NODE * cria_no(int n); +void print_no(NODE * l); + +//Queue progs +FILA * cria_fila(void); +FILA * insert_fila(FILA * f,int n); +FILA * delete_fila (FILA * f); + + +int main (){ + int n,i; + FILA * fila = cria_fila(); + while(scanf("%d",&n)!=EOF){ + fila = insert_fila(fila,n); + } + + printf("OK\n"); + while(scanf("%d",&n)!=EOF){ + delete_fila(fila); + print_no(fila->begin); + } + + return 0; +} + + +NODE * cria_no(int n){ + NODE * new = malloc(sizeof(NODE)); + if (new!= NULL){ + new->next = NULL; + new->n = n; + return new; + } + exit(1); +} + +void print_no(NODE * l){ + if (l !=NULL){ + printf("%d ",l->n ); + print_no(l->next); + }else { + printf("\n"); + } +} +///***FILAS***** + +FILA * cria_fila(void){ + FILA * f = malloc(sizeof(FILA)); + if (f!=NULL){ + f->begin = f->final = NULL; + return f; + } + exit(1); +} + +FILA * insert_fila(FILA * f,int n){ + if (f->begin == NULL){ + f->begin = f->final = cria_no(n); + }else{ + NODE * aux = cria_no(n); + aux->next = f->begin; + f->begin = aux; + } + return f; +} + +FILA * delete_fila (FILA * f) { + if (f->begin == NULL) return f; + else{ + if (f->begin == f->final){ + NODE * aux = f->begin; + f->begin = f->final = NULL; + free(aux); + }else{ + NODE * aux = f->begin; + while(aux->next!=f->final){ + aux = aux->next; + } + f->final = aux; + aux = aux->next; + free(aux); + f->final->next = NULL; + } + } + return f; +}