/* prog3.10_c.c */
#include <stdio.h>
#include <stdlib.h>

typedef struct node* link;
struct node { int item; link next; };

link reverse(link x)
  { link t, y = x, r = NULL;
    while (y != NULL)
      { t = y->next; y->next = r; r = y; y = t; }    
    return r;
  }

void print_list(link t)
{
  for ( ; t; t = t->next) 
    printf("%d\n", t->item);
}

int main(int argc, char *argv[])
{
  int i, N = atoi(argv[1]);
  link t = malloc(sizeof *t), x = t;
  t->item = rand(); 
  for (i = 2; i <= N; i++) {
    x = (x->next = malloc(sizeof *x));
    x->item = rand(); 
  }
  x->next = NULL;
  printf("Lista original:\n");
  print_list(t);
  t = reverse(t);
  printf("\nLista reversa:\n");
  print_list(t);
  return 0;
}
