Created
April 6, 2016 21:59
-
-
Save werbet/fab90e49f2467040f61a4953dc7f1771 to your computer and use it in GitHub Desktop.
Lista em C
This file contains 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> | |
//#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