/*
 * This code is (basically) from "Algorithms in C, Third Edition,"
 * by Robert Sedgewick, Addison Wesley Longman, 1998.
 */

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

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

/* Prototipos das funcoes */

void imprima_lista(link p);
link reverse(link x);

/* Programa principal */

int main(int argc, char *argv[])
{
  int N=atoi(argv[1]);
  link t, seq=NULL;

  while (N-->0) {
    t=malloc(sizeof *t);
    scanf("%d", &(t->item));
    t->next=seq;
    seq=t;
  }

  imprima_lista(seq);
  seq=reverse(seq);
  imprima_lista(seq);

  return 0;
}

/* Implementacoes das funcoes */

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

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;
  }

