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

// void * malloc(int numBytes);

struct StringListNode{
	char * payload;
	struct StringListNode * next;
};

void printStringList(struct StringListNode * head)
{
	struct StringListNode * currentNode = head;
	int i = 0;
	printf("#############################\n");
	while(currentNode != 0)
	{
		printf("Node %d has contents %s\n", i, currentNode->payload);
		currentNode = currentNode->next;
		i++;
	}
}

//assumes currentNode and newNode are valid
void addNodeAfter(struct StringListNode * currentNode, struct StringListNode * newNode)
{
	struct StringListNode * currentNodePointedTo = currentNode->next; //same as (*currentNode).next
	currentNode->next = newNode;
	newNode->next = currentNodePointedTo;
}

struct StringListNode * createNode(char * string)
{
	struct StringListNode * node = malloc(sizeof(struct StringListNode) );
	char * newString;
	//printf("stringListNode sizeof is %d\n", sizeof(struct StringListNode));
	node->next = 0;
	newString = malloc(sizeof(char) * (strlen(string) + 1));
	//printf("string sizeof is %d\n", sizeof(string));
	strcpy(newString, string);
	node->payload = newString;
	
	return node;
}

void deleteNode(struct StringListNode * node)
{
	free(node);
	free(node->payload);
}


int main()
{
	int x;
	char *string1 = "cse30";
	struct StringListNode * firstNode = createNode(string1);
	struct StringListNode * secondNode = createNode("computer org");
	
	printStringList(firstNode);
	
	addNodeAfter(firstNode, secondNode);
	printStringList(firstNode);
	deleteNode(secondNode);
	
	printStringList(firstNode);
}




























/*struct StringListNode {
	struct StringListNode * next;
	char * payload;
};


struct StringListNode * makeNode(char * string)
{
	struct StringListNode * node = malloc(sizeof(struct StringListNode));
	printf("sizeof(struct StringListNode) is %d\n", sizeof(struct StringListNode));
	node->next = 0;
	node->payload = malloc(sizeof(char)*strlen(string)+1);
	strcpy(node->payload, string);
	return node;
	
}

void printStringList(struct StringListNode * head)
{
	struct StringListNode * currentNode = head;
	int i = 0;
	printf("#############################\n");
	while(currentNode != 0)
	{
		printf("Node %d has contents %s\n", i, currentNode->payload);
		currentNode = currentNode->next;
		i++;
	}
}

void insertAfter(struct StringListNode * original, struct StringListNode * newNode)
{
	struct StringListNode * origNext = original->next;
	original->next = newNode;
	newNode->next = origNext;
	
}

void deleteNode(struct StringListNode * node)
{
	free(node);
}

void deleteNodeAfter(struct StringListNode * node)
{
	
	free(node->next);
	node->next = 0;
}

int main(int argc, int *argv[])
{
	char string1[6] = "cse30";	
	struct StringListNode * head = makeNode(string1);
	struct StringListNode * secondNode = makeNode("computer organization");
		
	printStringList(head);
	insertAfter(head, secondNode);
	printStringList(head);
	deleteNodeAfter(head);
	printStringList(head);
	
	//insertAfter(head, secondNode);
	//printStringList(head);
	
}
*/
