linkedlist.c
/*
Josh Santomieri
Santomieri Systems
http://www.santsys.com
*/


#include 
#include 

typedef struct _Node
{
	int x;
	struct _Node *next;
} NODE, *PNODE;

NODE *head;
NODE *tail;
int iSize = 0;

int j_add(int x);
int j_del(int x);
int j_print();

int j_add(int x)
{
	NODE *chk = head;
	NODE *temp;
	temp = (NODE *)malloc(sizeof(NODE *));
	temp->x = x;
	temp->next = NULL;

	if(iSize == 0)
	{
		head = temp;
		tail = temp;
	}
	else
	{
		while(chk != NULL)
		{
			if(chk->x == x)
			{
				printf("\n%d already in the list.\n", x);
				return 0;
			}
			chk = chk->next;
		}
		tail->next = temp;
		tail = temp;
	}

	iSize++;

	printf("\n%d has been added to the list.\n", x);

	return iSize;
}

int j_del(int x)
{
	NODE *temp = head, *prev = head;
	int i;

	if(iSize <= 1)
	{
		head = NULL;
		return 0;
	}

	for(i = 0;i < iSize; i++)
	{
		if(temp->x == x)
		{
			prev->next = temp->next;
			if(head == temp)
			{
				head = temp->next;
			}
			if(tail == temp)
			{
				tail = prev;
				prev->next = NULL;
			}
			free(temp);
			iSize--;
			return iSize;
		}
		else
		{
			prev = temp;
			temp = temp->next;
		}
	}

	return iSize;
}

int j_print()
{
	NODE *temp;
	temp = head;

	printf("\nItems in list:\n");

	if(temp == NULL)
	{
		printf("\tNo Items In List\n");
	}
	else
	{
		while(temp != NULL)
		{
			printf("\t%d\n", temp->x);
			temp = temp->next;
		}
	}

	return iSize;
}

int j_swap(NODE *n1, NODE *n2)
{
	NODE temp;

	temp.x = n2->x;
	n2->x = n1->x;
	n1->x = temp.x;

	return 0;
}

int j_sort()
{
	NODE *temp;
	NODE *tmp;
	int i, j;


	tmp = head;
	
	if(tmp == NULL)
	{
		printf("\nSort: No Items In list\n");
		return iSize;
	}

	temp = head->next;

	printf("\nSort Started");

	for(i = 0; ((i < iSize) && (tmp != NULL)); i++)
	{
		for(j = iSize; ((j > 0) && (temp != NULL)); j--)
		{
			if(temp->x > tmp->x)
				j_swap(tmp, temp);

			temp = temp->next;
		}
		temp = head;
		tmp = tmp->next;
	}

	printf("\nSort Complete\n");

	return 0;
}

void disp_menu()
{
	printf("0: Exit\n");
	printf("1: Add Item\n");
	printf("2: Delete Item\n");
	printf("3: Sort List\n");
	printf("4: Display List\n");
}

int main(int argc, char *argv[])
{

	int iOption;
	int iNum;

	do
	{
		iOption = 0;
		iNum = -1;
		disp_menu();
		printf("Please Select an Option: ");
		scanf("%d", &iOption);

		switch(iOption)
		{
		case 0:
			break;

		case 1:
			{
				printf("\nNumber to Add: ");
				scanf("%d", &iNum);
				j_add(iNum);
				printf("\n");
				break;
			}

		case 2:
			{
				printf("\nNumber to Delete: ");
				scanf("%d", &iNum);
				j_del(iNum);
				printf("\n");
				break;
			}

		case 3:
			{
				j_sort();
				printf("\n");
				break;
			}

		case 4:
			{
				j_print();
				printf("\n");
				break;
			}

		default:
			printf("\n%d is not a supported option!\n\n", iOption);
			break;
		}
	}while(iOption != 0);

	return 0;
}