Skip to content

Instantly share code, notes, and snippets.

@joffilyfe
Created June 8, 2015 14:55
Show Gist options
  • Save joffilyfe/3bbb5f121076c432558d to your computer and use it in GitHub Desktop.
Save joffilyfe/3bbb5f121076c432558d to your computer and use it in GitHub Desktop.
Lista encadeada, exemplo.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} Node;
void initialize(Node **list);
void viewList(Node *list);
int listEmpty(Node *list);
int addNode(Node **list, int value);
int main(void) {
Node *InicioDaLista;
initialize(&InicioDaLista);
addNode(&InicioDaLista, 10);
addNode(&InicioDaLista, 11);
addNode(&InicioDaLista, 12);
viewList(InicioDaLista);
printf("Lista vazia = %d\n", listEmpty(InicioDaLista));
return 0;
}
void initialize(Node **list) {
*list = NULL;
}
int addNode(Node **list, int value) {
Node *new = (Node*)malloc(sizeof(Node));
if (new == NULL) return 0;
new->data = value;
new->next = *list;
*list = new;
return 1;
}
int listEmpty(Node *list) {
return (list == NULL);
}
void viewList(Node *list) {
Node *Actual;
for (Actual = list; Actual != NULL; Actual = Actual->next) {
// printf("> Address: %d - Next: %d - valor:%d\n", Actual, Actual->next, Actual->data);
printf("> valor: %d\n", Actual->data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment