Computer Science 2574
Intro to Data Structures & Soft Eng
/*--------------------------------------------------------------
-Module name:		dlist.c
-Header File:    	dlist.h
-Implemenation File:   	dlist.c
-
-Purpose:
-  This module is responsible for the encapsulated list
-creation and management functions for a double linked list.
-
-The user is repsonsible for providing a datatype file called:
-  listtype.h
-in a directory where it will be found by the search path.
-the data structure will need to be in the format:
-
- struct listdata
-		{
-			define internal data items here
-		};
- struct listtitle
-		{
-			define header data items here
-		};
-
-this data file will be used by this module in its definition
-of the list.
-
-Functions:
-
-
-
-
-Created by:  Jon Ford 
-Version: 1.00
-Modified:  3/6/96
-------------------------------------------------------------*/

#include
#include

#include "dlist.h"
#include "string.h"

#define EMPTY 1
#define NOTEMPTY 0


/*-----------------------------------------------------------
-Funciton Name:		GetList
-
-Description:
-This function maintains a static header for the linked list
-and creates memory for that header.  It returns a pointer
-to that data structure when called with the CURR_LIST option.
-
-Description of Algorithm:
-
-
-Called by:	odb.c
-
-Author:      	Jon Ford  
-Revision:	3/27/96
-Version:     	1.0
--------------------------------------------------------------*/

ListMgr GetList(int listexist)
{

	static ListMgr newlist;

	if (listexist==NEW_LIST)
	{
		
		if ((newlist=(ListMgr)malloc(sizeof (struct mainstruct)))==NULL)
			return NULL;
		else
		{
			newlist->head=NULL;/*initallize the new element*/
			newlist->tail=NULL;
			return newlist;
		}
	}
	else
		return newlist;
}




/*-----------------------------------------------------------
-Funciton Name:		EmptyList
-
-Description:
-This function returns TRUE if the list is empty and FALSE if 
-the list is not empty.
-
-Description of Algorithm:
-
-Called by:	odb.c
-
-Author:      	Jon Ford  
-Revision:	3/27/96
-Version:     	1.0
--------------------------------------------------------------*/

int EmptyList(ListMgr listmgr)
{
	if (listmgr->head==NULL)
		return EMPTY;
	else
		return NOTEMPTY;
}



/*-----------------------------------------------------------
-Funciton Name:		CreateElement
-
-Description:
-This function allocates memory for and returns a pointer to
-a new list element.
-
-
-Called by:	odb.c
-
-Author:      	Jon Ford  
-Revision:	3/27/96
-Version:     	1.0
--------------------------------------------------------------*/

ElemPtr CreateElement(void)
{

	return ((ElemPtr)malloc(sizeof(struct liststruct)));

}




/*-----------------------------------------------------------
-Funciton Name:		InsertElement
-
-Description:
-This function inserts a new element at the tail of the list.
-
-
-Called by:	odb.c
-
-Author:      	Jon Ford  
-Revision:	3/27/96
-Version:     	1.0
--------------------------------------------------------------*/

void InsertElement(ListMgr listmgr, ElemPtr element)
{

	if (EmptyList(listmgr)==EMPTY)  /*first element in the list*/
	{
		listmgr->head=element;
		listmgr->tail=element;
			
	}
	else
	{
				
		element->previous=listmgr->tail;
		element->next=NULL;
		element->previous->next=element;
		listmgr->tail=element;
				
	}

}

/*-----------------------------------------------------------
-Funciton Name:		RemoveElement
-
-Description:
-This function removes an element from the list.
-
-
-
-Called by:	odb.c
-
-Parameters:	listmgr - list header data structure
-		element - pointer to the element to be removed
-
-Author:      	Jon Ford  
-Revision:	3/27/96
-Version:     	1.0
--------------------------------------------------------------*/

void RemoveElement(ListMgr listmgr, ElemPtr element)
{
   	if (EmptyList(listmgr)==1) return; /*no list, no remove*/

	if (element->previous==NULL)   /*first element*/
	{
		listmgr->head=element->next;
		listmgr->head->previous=NULL;
	}
	else
		if (element->next==NULL)     /*last element*/
		{
			listmgr->tail=element->previous;
			listmgr->tail->next=NULL;
		}
		else     /*any other element*/
		{
			element->previous->next=element->next;
			element->next->previous=element->previous;
		}

	free(element);
}




/*-----------------------------------------------------------
-Funciton Name:		SizeList
-
-Description:
-This function counts the number of items in the linked list
-and returns the number.
-
-Called by:	odb.c
-
-Parameters:	listmgr - list header data structure
-
-Author:      	Jon Ford  
-Revision:	3/27/96
-Version:     	1.0
--------------------------------------------------------------*/

int SizeList(ListMgr listmgr)
{
	ElemPtr current;
	int count=0;
	current=listmgr->head;
	while (current!=NULL)
	{
		count++;
		current=current->next;
	}
	return count;
}



/*-----------------------------------------------------------
-Funciton Name:		DestroyList
-
-Description:
-This function loops through the linked list and frees all of
-the memory, destroying the list.
-
-Called by:	odb.c
-
-Parameters:	listmgr - list header data structure
-
-Author:      	Jon Ford  
-Revision:	3/27/96
-Version:     	1.0
--------------------------------------------------------------*/


void DestroyList(ListMgr listmgr)
{
	ElemPtr current, previous;
	current=listmgr->head;
	while (current!=NULL)
	{
		previous=current;
		current=current->next;
		free(previous);
	}
	listmgr->head=NULL;
	listmgr->tail=NULL;
}



/*-----------------------------------------------------------
-Funciton Name:		ReturnElement
-
-Description:
-This function returns a pointer to the element at the elemnum
-position in the list from the head. (head=1, head->next=2,etc)
-
-Called by:	odb.c
-
-Parameters:	listmgr - list header data structure
-		elemnum - integer element number
-
-Author:      	Jon Ford  
-Revision:	3/27/96
-Version:     	1.0
--------------------------------------------------------------*/

ElemPtr ReturnElement(ListMgr listmgr, int elemnum)
{
	ElemPtr current;
	int count=0;
	

	if (EmptyList(listmgr)==1)  /*if list is empty we can't return elem*/
		return NULL;
	
	current=listmgr->head;

	while (current!=NULL)
	{
		if (count==elemnum) 
			return current;
		current=current->next;
		count++;
	}
	return NULL;  /*not enough elements in the list*/
}


/*-----------------------------------------------------------
-Funciton Name:		InsertTail
-
-Description:
-This function inserts the tail back into the linked list based
-on ascending alphabetical order of the last name.  We store
-the new students added in the tail with default values until
-they are editted and then odb.c calls inserttail.

-Called by:	odb.c
-
-Parameters:	listmgr - list header data structure
-
-Author:      	Jon Ford  
-Revision:	3/27/96
-Version:     	1.0
--------------------------------------------------------------*/

void InsertTail(ListMgr listmgr)
{
	ElemPtr element, current;

	element=listmgr->tail;

	element->previous->next=NULL;  /*this is the new end*/
	listmgr->tail=element->previous; /*change the tail*/
	
	/*if the element becomes the new head*/
	
	if(strcmp(element->item.lastname,listmgr->head->item.lastname)<=0)
	{
		element->next=listmgr->head;
		element->previous=NULL;
		element->next->previous=element;
		listmgr->head=element;
		return;
	}

	current = listmgr->head;

	while (current!=NULL)
	{
		if (strcmp(element->item.lastname,current->item.lastname)<=0)
		{
			element->previous=current->previous;
			current->previous->next=element;
			element->next=current;
			current->previous=element;
			return;
		}
		current=current->next;
	}

	/*if we've made it this far we're at the tail*/		
	element->next=NULL;
	element->previous=listmgr->tail;
	listmgr->tail->next=element;
	listmgr->tail=element;
	return;
}


/*-----------------------------------------------------------
-Funciton Name:		ReturnNext
-
-Description:
-This function returns a pointer to the element immediately 
-following the element passed to the function in the linked 
-list.
-
-Called by:	odb.c
-
-Parameters:	element - pointer to the current element
-
-Author:      	Jon Ford  
-Revision:	3/28/96
-Version:     	1.0
--------------------------------------------------------------*/

ElemPtr ReturnNext(ElemPtr element)
{
	if (element==NULL)
		return NULL;
	else
		return element->next;
}



/*-----------------------------------------------------------
-Funciton Name:		ReturnPrevious
-
-Description:
-This function returns a pointer to the element immediately 
-preceding the element passed to the function in the linked
-list.
-
-Called by:	odb.c
-
-Parameters:	element - pointer to the current element
-
-Author:      	Jon Ford  
-Revision:	3/28/96
-Version:     	1.0
--------------------------------------------------------------*/

ElemPtr ReturnPrevious(ElemPtr element)
{
	if(element==NULL)
		return NULL;
	else
		return element->previous;
}



/*-----------------------------------------------------------
-Funciton Name:		ReturnFirst
-
-Description:
-This function returns a pointer to the first element in the 
-list specified by listmgr passed to the function.
-
-Called by:	odb.c
-
-Parameters:	listmgr - list header data structure
-
-Author:      	Jon Ford  
-Revision:	3/28/96
-Version:     	1.0
--------------------------------------------------------------*/

ElemPtr ReturnFirst(ListMgr listmgr)
{
	if (listmgr==NULL)
		return NULL;
	else
		return listmgr->head;
}


/*-----------------------------------------------------------
-Funciton Name:		ReturnElement
-
-Description:
-This function returns a pointer to the last element in the 
-list pointed to by listmgr.
-
-Called by:	odb.c
-
-Parameters:	listmgr - list header data structure
-
-Author:      	Jon Ford  
-Revision:	3/28/96
-Version:     	1.0
--------------------------------------------------------------*/

ElemPtr ReturnLast(ListMgr listmgr)
{
	if (listmgr==NULL)
		return NULL;
	else
		return listmgr->head;
}