#include "face.h"

Index *newIndex(i)
int i;
{
	Index *ind;
	
	ind = Malloc(Index,1);
	ind->i = i;
	ind->next = NOINDEX;
	
	return(ind);
}

Index *addToFrontIndex(ind,i)
Index *ind;
int i;
{
	Index *newI;
	
	newI = Malloc(Index,1);
	newI->i = i;
	newI->next = ind;
	
	return(newI);
}

Index *addToBackIndex(ind,i)
Index *ind;
int i;
{
	Index *newI;
	
	newI = newIndex(i);
	ind->next = newI;
	
	return(newI);
}

Index *nextIndex(ind)
Index *ind;
{
	if(ind == NOINDEX) return(NOINDEX);
	else return(ind->next);
}

void deleteIndex(ptind,i)
IndexPtr *ptind;
{
	Index *m,*n;
	
	m = n = *ptind;
	
	if(m == NOINDEX) return;
	n = NEXT(m);
	
	if(INDEX(m) == i){
		*ptind = n;
		FREE(m);
		return;
	}
	
	while(n != NOINDEX){
		if(INDEX(n) == i){
			NEXT(m) = NEXT(n);
			FREE(n);
			return;
		}
		else{
			m = n;
			n = NEXT(n);
		}
	}
}

void traverseIndex(index,f)
Index *index;
void (*f)();
{
	Index *ind;
	
	ind = index;
	while(ind != NOINDEX){
		(*f)(INDEX(ind));
		ind = NEXT(ind);
	}
}

BOOLEAN memberOfIndex(index,item)
Index *index;
int item;
{
	Index *ind;
	
	ind = index;
	while(ind != NOINDEX){
		if(INDEX(ind) == item) return(TRUE);
		ind = NEXT(ind);
	}
	return(FALSE);
}

void printList(index)
Index *index;
{
	Index *ind;
	
	ind = index;
	printf("[ ");
	while(ind != NOINDEX){
		printf("%d ",INDEX(ind));
		ind = NEXT(ind);
	}
	printf("]");
}

void appendToIndex(index,item,last)
Index **index;
int item;
Index **last;
{
	
	if(*index == NOINDEX){
		*index = newIndex(item);
		*last = *index;
	}
	else *last = addToBackIndex(*last,item);
}
		
Index *copyIndex(index)
Index *index;
{
	Index *ind,*new,*last;
	
	ind = index;
	new = last = NOINDEX;
	while(ind != NOINDEX){
		appendToIndex(&new,INDEX(ind),&last);
		ind = NEXT(ind);
	}
	return(new);
}
