/**************************************************************
	A very simple example of creating a singly linked 
	list of strings. Storing strings in the list and 
	dumping it to standard out.
								CPS125	Alex Ferworn
**************************************************************/

/*
When I run this thing I get the output

Enter a name: alex
Enter a name: fred
Enter a name: ted
Enter a name: dave
Enter a name: spare
The address of head is: 03DD71C2

Address Stores  Points at
03DD71C2        alex    03DD77E4
03DD77E4        fred    03DD7800
03DD7800        ted     03DD781C
03DD781C        dave    03DD7838
03DD7838        spare   00550000
*/

#include <stdio.h>	/* where IO prototypes live */
#include <stdlib.h>	/* where malloc() prototype lives */

/* This is the structure type that we will use to make the list */
typedef struct tagname
{
	char name[20+1];		/*able to store a string of 20 chars */
	struct tagname *next;	/*will be used to point at the next list element */
} LIST;

void main()
{
	short i; 			/* counter */
	LIST *head = NULL; 	/* will point to first thing in list */
	LIST *tail = NULL; 	/* will point to the last thing in the list */
	LIST *temp = NULL; 	/* temporary until we add element to the list */
	
	/* We will create 5 structures 1 at a time,
	link them together and put data in them */

	/* Create the first one and put it in the list*/
	temp = (LIST *) malloc(1 * sizeof(LIST));
	head = temp; /* make the head point to the first thing */
	tail = temp; /* make the tail point to the first (last) thing */
	printf("Enter a name: "); /* ask user for name */
	scanf("%s",tail->name); /* read the name */
	
	/* create, link and populate 4 more */
	for(i=0;i<4;i++)
	{
		temp = (LIST *) malloc(1 * sizeof(LIST));
		tail->next = temp; /* make the previous tail point at the new thing */
		tail = temp; /* point at the new tail */
		printf("Enter a name: "); /* ask user for name */
		scanf("%s",tail->name); /* read the name */
	}
	
	/* dump the 5 things in the list to the screen with their addressed
	and what they point at*/
	temp = head;

	printf("The address of head is: %p\n\n",head);
	printf("Address\tStores\tPoints at\n");
	for(i=0;i<5;i++)
	{
		printf("%p\t%s\t%p\n",temp,temp->name,temp->next);
		temp = temp->next;
	}
}