list.c


/*
** $RCSfile: list.c $
** $Revision: 1.1 $
** $Date: 1996/07/25 10:10:32 $
** $Author: doomer $
*/

#include "list.h"

static char rcsHeader[]="$Id: list.c 1.1 1996/07/25 10:10:32 doomer Exp $";

struct node *createNewNode(void *data){
	struct node *newNode;

	newNode=calloc(sizeof(struct node), 1);

	if(newNode){
		newNode->data=data;
	}

	return(newNode);
}

struct node *push(struct node **head, struct node *newNode){
	struct node *headPtr;

	headPtr = *head;

	if(headPtr){
		headPtr->prev=newNode;
	}

	newNode->next=headPtr;

	*head=newNode;

	return(newNode);
}

void *pop(struct node **head){
	return(removeFromList(head, *head));
}

void *removeFromList(struct node **head, struct node *nodeToRemove){
	struct node *prev;
	struct node *next;
	struct node *headPtr;
	void *nodeData;

	headPtr = *head;
	nodeData=nodeToRemove->data;

	prev=nodeToRemove->prev;
	next=nodeToRemove->next;

	if(prev){
		prev->next=next;
	}

	if(next){
		next->prev=prev;
	}

	/*
	** if we are removing the node that is the head of the list, which
	** could by definition be the last node in the list, adjust the
	** head pointer's prev and next pointers.
	*/

	if(nodeToRemove == headPtr){
		*head=nodeToRemove->next;
		headPtr = *head;

		if(*head){
			headPtr->prev=nodeToRemove->prev;
		}
	}

	free(nodeToRemove);

	return(nodeData);
}

/*
** This function will iterate through the list, calling the
** 'cmp' function until it hits the end of the list or until
** the 'cmp' function returns 0.
*/

struct node *iterateUntil(struct node *head, void *cmpData, compareFn cmp){
	struct node *listPtr;

	listPtr=head;

	while(listPtr){
		if(!cmp(listPtr->data, cmpData)){
			break;
		}
		
		listPtr=listPtr->next;
	}

	return(listPtr);
}

struct node *iterateOver(struct node *head, iterateFn iterate){
	struct node *listPtr;

	listPtr=head;

	while(listPtr){
		iterate(listPtr->data);
		listPtr=listPtr->next;
	}
	
	return(listPtr);
}

char *revInfo(void){
	return(rcsHeader);
}

/*
** $Log: list.c $
** Revision 1.1  1996/07/25 10:10:32  doomer
** Initial revision
** Revision 1.7  1996/01/14 12:08:12  doomer
** added implementation for revInfo() function.
** Revision 1.6  1996/01/14 11:32:54  doomer
** added RCSfile rcs header
** Revision 1.5  1996/01/13 22:19:26  doomer
** changed the function prependToList() to push().
** added function pop().
** Revision 1.4  1996/01/12 10:03:28  doomer
** changed the name of addToList() to prependToList().
** changed the iterateUntil() function to accept pointer to user-defined
** data.  did this so that the function would call the 'cmp' function with
** user-defined comparison data.  changed the iterateUntil() function
** to stop when the 'cmp' function retuns 0.  These two changes make it
** possible for a user to specify standard library functions like
** strcmp() for use as a comparison function.
** Revision 1.3  1996/01/11 15:37:22  doomer
** added implementattion for iterateOver().
** Revision 1.2  1996/01/11 13:48:40  doomer
** added inplementation for iterateUntil().
** Revision 1.1  1996/01/11 11:38:46  doomer
** Initial revision
*/