Advertisement
CarlosWGama

Aula 01 - Lista

Sep 14th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct no {
  5.     int valor;
  6.     struct no *proximo;
  7. };
  8.  
  9. struct lista {
  10.     struct no *inicio;
  11.     struct no *fim;
  12. };
  13.  
  14.  
  15. void inserirValor(struct lista* lista, int valor) {
  16.  
  17.     struct no *novoNo = malloc(sizeof(struct no));
  18.     novoNo->valor = valor;
  19.     novoNo->proximo = NULL;
  20.  
  21.     if (lista->inicio == NULL) {
  22.         lista->inicio = novoNo;
  23.         lista->fim = novoNo;
  24.     } else {
  25.         novoNo->proximo = lista->inicio;
  26.         lista->inicio = novoNo;
  27.     }
  28. }
  29.  
  30. void inserirNoFinal(struct lista *lista, int valor) {
  31.  
  32.     struct no* novoNo = malloc(sizeof(struct no));
  33.     novoNo->valor = valor;
  34.     novoNo->proximo = NULL;
  35.  
  36.     if (lista->inicio == NULL) {
  37.         lista->inicio = novoNo;
  38.         lista->fim = novoNo;
  39.     } else {
  40.         lista->fim->proximo = novoNo;
  41.         lista->fim = novoNo;
  42.     }
  43. }
  44.  
  45. int main()
  46. {
  47.     struct lista lista;
  48.     lista.inicio = NULL;
  49.     lista.fim = NULL;
  50.  
  51.     inserirValor(&lista, 5);
  52.     inserirValor(&lista, 10);
  53.     inserirNoFinal(&lista, 20);
  54.     printf("Inicio %d\n",lista.inicio->valor);
  55.     printf("Fim %d",lista.fim->valor);
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement