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

#include 
#include 

typedef struct _Node
{
	char cItem[260];
	struct _Node *Next;
	struct _Node *Prev;
} NODE, *PNODE;

NODE *nhead = NULL;
int iSize = 0;

int j_additem(char *cItem)
{
	NODE *nNew;
	NODE *tmp;
	
	nNew = (NODE *)malloc(sizeof(NODE *));
	strcpy(nNew->cItem, cItem);
	
	if(nhead == NULL)
	{
		nNew->Next = NULL;
		nNew->Prev = NULL;
		nhead = nNew;
		iSize++;
    }
	else
	{
		tmp = nhead;
		while(tmp->Next != NULL)
		{
			tmp = tmp->Next;
		}
		nNew->Prev = tmp;
		nNew->Next = NULL;
		tmp->Next = nNew;
		iSize++;
	}

	printf("\nItem Added (%s)", cItem);

	return 0;
}

int j_printlist(int direction)
{
	NODE *tmp;

	printf("\n-------------------------------\n");
	if(direction)
	{
		tmp = nhead;
		while(tmp != NULL)
		{
			printf("\t%s\n", tmp->cItem);
			tmp = tmp->Next;
		}
	}
	else
	{
		tmp = nhead;
		while(tmp->Next != NULL)
		{
			tmp = tmp->Next;
		}
		while(tmp != NULL)
		{
			printf("\t%s\n", tmp->cItem);
			tmp = tmp->Prev;
		}
	}

	printf("\n");

	return 0;
}

int j_swap(NODE *tmp, NODE *tmp1)
{
	NODE old;

	strcpy(old.cItem, tmp->cItem);
	strcpy(tmp->cItem, tmp1->cItem);
	strcpy(tmp1->cItem, old.cItem);
	return 0;
}

int j_sort(void)
{
	int iCount = 0;
	int j = 0;
	NODE *tmp;
	NODE *temp;

	tmp = nhead;
	if(tmp == NULL)
		return 0;

	temp = nhead->Next;

	printf("starting sort\n");
	while(((iCount < iSize) && (tmp != NULL)))
	{
		for(j = iSize; ((j > 0) && (temp != NULL)); j--)
		{
			if(strcmp(tmp->cItem, temp->cItem) < 0)
				j_swap(temp, tmp);

			temp = temp->Next;
		}
		temp = nhead;
		tmp = tmp->Next;
		iCount++;
	}
	printf("End sort\n");
	return 0;
}

int main(int argc, char **argv)
{
	j_additem("DDDD");
	j_additem("FFFF");
	j_additem("EEEE");
	j_additem("CCCC");
	j_additem("AAAA");
	j_additem("BBBB");
	printf("iSize: %d\n", iSize);

	j_printlist(1);

	j_sort();

	j_printlist(1);

	return 0;
}