Skip to content

Instantly share code, notes, and snippets.

@philorocha
Last active June 15, 2018 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philorocha/183e68f277512759eaf18be41576c711 to your computer and use it in GitHub Desktop.
Save philorocha/183e68f277512759eaf18be41576c711 to your computer and use it in GitHub Desktop.
/*Implementação de lista dinâmica*/
#include <stdio.h>
#include <stdlib.h>
typedef struct No{
int valor;
struct No* proximo;
} No;
No* adicionar(No* no, int valor) {
if (no == NULL) {
No* novo = (No*)malloc(sizeof(No));
novo->valor = valor;
novo->proximo = NULL;
return novo;
} else {
no->proximo = adicionar(no->proximo, valor);
return no;
}
}
No* remover(No* no, int valor) {
if (no == NULL) {
return NULL;
} else {
if (no->valor == valor) {
No* temp = no;
free(no);
return temp->proximo;
} else {
no->proximo = remover(no->proximo, valor);
return no;
}
}
}
void imprimir(No* lista) {
while(lista != NULL) {
printf("%d\n", lista->valor);
lista = lista->proximo;
}
}
int main(void) {
No* lista = NULL;
lista = adicionar(lista, 10);
adicionar(lista, 20);
adicionar(lista, 30);
adicionar(lista, 40);
adicionar(lista, 50);
adicionar(lista, 60);
adicionar(lista, 70);
adicionar(lista, 80);
adicionar(lista, 90);
adicionar(lista, 100);
lista = remover(lista, 40);
adicionar(lista, 200);
imprimir(lista);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment