/* lista-1.c - Exemplo de lista encadeada

  Copyright 2009 - Francisco Jose Monaco

  This program is Free Software and can be distributed under the GNU GPL v3
  as found in www.gnu.org/licenses/gpl.txt

*/


#include <stdio.h>
#include <stdlib.h>

typedef struct no no_t; /* No da lista. */
struct no
{
  int valor;
  struct no * proximo;
};

typedef struct head head_t; /* Lista encabecada. */
struct head
{
  struct no * primeiro;
  int tamanho;

};

void mostra(head_t *h) /* Percorre a lista. */
{
  no_t *n;
 
  n = h->primeiro;
  while (n != NULL)
    {
      printf (" %d", n->valor);
      n = n->proximo;
    }
  printf ("\n");
 
}

no_t * add_no_inicio (head_t *h, int v) /* Insere um no no inicio da lista. */
{
  no_t * no;

  no = (no_t*) malloc (sizeof(no_t));

  no->valor = v;

  no->proximo = h->primeiro;
  h->primeiro = no;
 
  h->tamanho++;

  return no;
}

void del_no_inicio (head_t *h) /* Remove do inicio da lista. */
{
  no_t *no;

  no = h->primeiro;
  h->primeiro = no->proximo; 

  h->tamanho--;

  free(no);
 
}

int main(void)
{

  head_t * list_head;

  /* Criando lista (vazia). */

  list_head = (head_t*) malloc (sizeof(head_t));
 
  list_head->primeiro = NULL;
  list_head->tamanho = 0;

  /* Adicionando um no. */
 
  add_no_inicio (list_head, 30);
  add_no_inicio (list_head, 20);
  add_no_inicio (list_head, 10);

  mostra (list_head);

  del_no_inicio (list_head);

  mostra (list_head);

  return EXIT_SUCCESS;
}
Última atualização: quinta, 8 outubro 2009, 22:10