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

struct list {
  char *str;
  struct list *next;
} *list_p = NULL, *list_q = NULL;

struct list *list_append(struct list item)
{
  struct list *me = malloc(sizeof(struct list));

  if (!me) return NULL;
#if defined(UNSAFE)
  *me = item;
#else
  me->str = strdup(item.str);
#endif
  me->next = list_p;
  return (list_p = me);
}
struct list *list_prepend(struct list item)
{
  struct list *me, *p;

  if (!list_p) return list_append(item);
  me = malloc(sizeof(struct list));
  if (!me) return NULL;
#if defined(UNSAFE)
  *me = item;
#else
  me->str = strdup(item.str);
#endif
  me->next = NULL;
  if (!list_q) {
    for (p=list_p; p; p=p->next)
      if (!p->next)
	break;
  }
  else
    p = list_q;
  return (list_q = p->next = me);
}
void list_free(void)
{
  struct list *p, *q;

  for (p=list_p; p; p=q) {
    q = p->next;
    free(p->str);
    free(p);
  }
}
void list_print(void)
{
  struct list *p;

  p = list_p;
  while (p) {
    printf("%s", p->str);
    p = p->next;
  }
}
int main(void)
{
  char buf[1024];
  struct list item;

  while (fgets(buf, sizeof(buf), stdin)) {
    item.str = malloc(strlen(buf)+1);
    if (item.str)
      strcpy(item.str, buf);
    if (!list_append(item))
      return 1;
    if (!list_prepend(item))
      return 1;
  }
  list_print();
  list_free();
  return 0;
}
