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

struct link{
	int data;
	struct link *next;
};

struct list{
	struct link *head;
	struct link *tail;
};

struct link * add_tail(struct list *lst, int value){
	struct link *item = (struct link *)malloc(sizeof(struct link));
	item->data = value;
	item->next = NULL;
	if(lst->head == NULL){
		lst->head = item;
		lst->tail = lst->head;
	}else{
		lst->tail->next = item;
		lst->tail = item;
	}
	return item;
}

struct link * insert(struct list *lst, int data){
	struct link *p, *t;
	if(lst->head == NULL){
		p = (struct link *)malloc(sizeof(struct link));
		p->data = data;
		p->next = NULL;
		lst->head = p;
	}else{
		p = lst->head;
		while(p = p->next)
			;
		p = (struct link *)malloc(sizeof(struct link));
		p->data = data;
		p->next = NULL;
	}
	return p;
}

void print(struct list *lst){
	struct link *p = lst->head;
	while(p){
		printf("%d\n", p->data);
		p = p->next;
	}
}

int main(){
	struct list lst;
	lst.head = lst.tail = NULL;
	int in, i;

	for(i = 1; i <= 3; i++){
		add_tail(&lst, i);
	}
	print(&lst);
	insert(&lst, 5);
	print(&lst);
}