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

typedef struct node{
	int x;
	struct node * next;
} node;

int main(void){
	node * linkedlist = NULL;

	int n = 0;
	while (n != -1){
		printf("Enter a number, or -1 to list all the numbers and quit:");
		scanf("%d",&n);

		if (n != -1){
			//allocate memory to hold a new node
			node * newNode;
			newNode = malloc(sizeof(node));
			(*newNode).x = n;
			(*newNode).next = NULL;

			if (linkedlist == NULL){
				linkedlist = newNode;
			}
			else{
				node * placeholder;
				placeholder = linkedlist;
				while ((*placeholder).next != NULL){
					placeholder = (*placeholder).next;
				}
				(*placeholder).next = newNode;
			}
		}
	}

	printf("The linked list contains: \n");

	node * placeholder;
	placeholder = linkedlist;
	do{
		printf("%d ",(*placeholder).x);
		placeholder = (*placeholder).next;
	}
	while ((*placeholder).next != NULL);
	

	placeholder = linkedlist;
	do{
		node * temp = (*placeholder).next;
		(*placeholder).next = (*(*placeholder).next).next;
		free(temp);
	}while ((*placeholder).next != NULL);
	free(linkedlist);

	printf("\n");
	return 0;
}
