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

#define scr_put(line, colm, c)		/* position cursor and write */ \
	printf("\033[%d;%dH%c", line, colm, c)
#define scr_clr() printf("\033[2J")	/* clear screen */

void wait(unsigned int seconds);

typedef struct node {
	struct node *pfwd;	/* ptr to next node in list */
	unsigned char line;	/* cursor position line */
	unsigned char colm;	/* cursor position column */
	unsigned char ch;	/* char to be displayed */
} Node;

main()
{
	Node *proot_node = NULL;/* start of data list */
	Node *pnode;		/* Node just allocated */
	Node *pprev_node;	/* Node previously allocated */
	unsigned int line, colm;
	unsigned char ch;

/* get input data pairs until EOF */

	while (1) {
/*		printf("Enter line, colm, char: "); */
		if (scanf("%u %u %c", &line, &colm, &ch) == EOF)
			break;

		pnode = malloc(sizeof(Node));
		if (pnode == NULL) {
			fprintf(stderr, "Can't malloc Node\n");
			exit(EXIT_FAILURE);
		}

		/* update root on first node only */

		if (proot_node == NULL)
			proot_node = pnode;
		else
			pprev_node->pfwd = pnode;

		pnode->line = line;
		pnode->colm = colm;
		pnode->ch = ch;
		pnode->pfwd = proot_node;
		pprev_node = pnode;
	}


/* cycle indefinitely through circular list, processing data */

	if (proot_node == NULL) {
		fprintf(stderr, "List is empty\n");
		exit(EXIT_FAILURE);
	}

	pnode = proot_node;
	while (1) {
		if (pnode == proot_node) {
			wait(5);
			scr_clr();
		}
		scr_put(pnode->line, pnode->colm, pnode->ch);
		pnode = pnode->pfwd;
	}

	return(EXIT_SUCCESS);
}

