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

#define BUFFER_LEN	20
typedef char t_name[BUFFER_LEN];

typedef struct {
	t_name	name;
	int	num;
} t_course;

typedef struct node {
	t_course	 course;
	struct node	*next;
} t_node;

void store_list(t_node **);
void display_list(const t_node *);
void free_list(t_node *);

int
main()
{
	t_node *list = NULL;	/* Pointer to the front of the list */

	store_list(&list);
	display_list(list);
	free_list(list);
	return 0;
}

void store_list(t_node **list)
{
	t_course course[] = {
		{ "COMP3710",	25 },
		{ "COMP3724",	20 },
		{ "COMP4718",	15 }
	};
	int	i   = 0;

	/* Fill up the list */
	for (i = 0; i < sizeof(course)/sizeof(course[0]); ++i) {
		t_node *cur = (t_node *) malloc(sizeof(t_node));

		strcpy(cur->course.name, course[i].name);
		cur->course.num = course[i].num;

		cur->next = *list;	/* Insert node at front of list */
		*list = cur;		/* Update the front of the list */
	}
}

/* Display the list */
void display_list(const t_node *list)
{
	for (; list != NULL; list = list->next)
		printf("%s: %d\n", list->course.name, list->course.num);
}

/* Free up the list */
void free_list(t_node *list)
{
	while (list) {
		t_node	*del = list;
		list = list->next;
		free(del);
	}

}
