Skip to content

Instantly share code, notes, and snippets.

@werbet
Created April 6, 2016 21:59
Show Gist options
  • Save werbet/fab90e49f2467040f61a4953dc7f1771 to your computer and use it in GitHub Desktop.
Save werbet/fab90e49f2467040f61a4953dc7f1771 to your computer and use it in GitHub Desktop.
Lista em C
#include <stdio.h>
#include <stdlib.h>
//#define NULL 0
struct no
{
int valor;
struct no* prox;
};
void inserir_final(struct no* lista, int valor)
{
if(lista->prox == NULL)
{
lista->prox = malloc(sizeof(struct no));
lista->prox->valor = valor;
lista->prox->prox = NULL;
}
else
{
struct no* aux;
aux = lista;
while(aux->prox != NULL)
{
aux = aux->prox;
}
aux->prox = malloc(sizeof(struct no));
aux->prox->valor = valor;
aux->prox->prox = NULL;
}
}
void imprimir_lista(struct no* lista)
{
struct no* aux;
aux = lista;
while(aux != NULL)
{
printf("Valor = %d\n", aux->valor);
aux = aux->prox;
}
}
void delecao(struct no* lista)
{
struct no* aux;
aux = lista;
while(aux != NULL)
{
if(aux->prox != NULL)
{
struct no* proximo;
proximo = aux->prox;
free(aux);
aux = proximo;
}
}
}
int main(int argc, char *argv[])
{
struct no* lista;
lista = malloc(sizeof(struct no));
lista->valor = -1;
lista->prox = NULL;
inserir_final(lista, 0);
inserir_final(lista, 1);
inserir_final(lista, 222);
inserir_final(lista, 223);
inserir_final(lista, 224);
inserir_final(lista, 225);
inserir_final(lista, 226);
imprimir_lista(lista);
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment