Last active
March 23, 2021 02:47
-
-
Save lesimoes/67ab1490a2cecfd79fc419d12dadf292 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdarg.h> | |
struct No { | |
char info; | |
struct No *prox; | |
}; | |
typedef struct No Registro; | |
typedef struct { | |
int size; | |
Registro *head; | |
}Lista; | |
void criarLista (Lista *L) { | |
L = malloc(sizeof(Lista)); | |
L->size = 0; | |
L->head = NULL; | |
} | |
void inserir (Lista *L, char valor){ | |
Registro *p; | |
p = malloc(sizeof(Registro)); | |
p->info = valor; | |
p->prox = L->head; | |
L->head = p; | |
L->size ++; | |
}; | |
void imprimirLista (Lista *L) { | |
Registro *p; | |
p = malloc(sizeof(Registro)); | |
p = L->head; | |
int contador = 0; | |
while(contador < L->size) { | |
printf("%c", p->info); | |
p = p->prox; | |
contador ++; | |
} | |
} | |
Registro buscarValor (Lista *L, char valor) { | |
Registro *p; | |
p = malloc(sizeof(Registro)); | |
char buscado = '\0'; | |
for (p = L->head ; p->info != '\0' ; p = p->prox) { | |
if (p->info == valor) break; | |
} | |
return *p; | |
} | |
void remover (Lista *L, char valor) { | |
Registro buscado; | |
Registro *p; | |
p = malloc(sizeof(Registro)); | |
buscado = buscarValor(L, valor); | |
if (buscado.info != "\0") { | |
p->info = buscado.prox->info; | |
p->prox = buscado.prox->prox; | |
// REMOVE ELEMENTO HEAD | |
if (buscado.info == L->head->info) { | |
L->head = p; | |
} | |
// REMOVE ENTRE ELEMENTOS | |
if (L->head->info != p->info && p->info != '\0'){ | |
L->head->prox = p; | |
} else { | |
// REMOVER ÚLTIMO ELEMENTO | |
p = NULL; | |
} | |
L->size --; | |
} | |
} | |
void inserirElementos (Lista *L, int qtd, ...) { | |
va_list elementos; | |
va_start(elementos, qtd); | |
for (int i = 0; i < qtd ; i ++) { | |
inserir(L, va_arg(elementos, int)); | |
} | |
va_end(elementos); | |
} | |
int main(){ | |
printf("=== Início ====\n\n"); | |
Lista lista; | |
criarLista(&lista); | |
inserirElementos(&lista, 5, 'A', 'B', 'C', 'D', 'E'); | |
imprimirLista(&lista); | |
printf("\n%c Encontrado", buscarValor(&lista, 'A').info); | |
remover(&lista, 'A'); | |
printf("\n\n=== REMOCAO ===\n"); | |
imprimirLista(&lista); | |
printf("\n\n=== FIM ===\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment