#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
  int x;
  struct node *next;
} NODE;

typedef NODE *PNODE;

int main(void)
{
  int x;
  PNODE pList = NULL, pTemp;

  printf("Enter numbers one at a time, EOF (Ctrl-D) to stop.\n");
  while(1)
  {
    if (scanf("%d", &x) == EOF)
      break;

    pTemp = (PNODE) malloc (sizeof(NODE));
    if (pTemp == NULL)
    {
      printf("Out of memory, could not store number!\n");
    }
    else
    {
      pTemp->x = x;
      pTemp->next = pList;
      pList = pTemp;
    }
  }

  printf("\nThe numbers in reverse are:\n");
  for (pTemp = pList; pTemp != NULL; pTemp = pTemp->next)
  {
    printf("%d\n", pTemp->x);
  }

  while (pList != NULL)
  {
    pTemp = pList->next;
    free(pList);
    pList = pTemp;
  }

  return 0;
}
