All pastes #2047261 Raw Edit

Portions of Link List code

public c v1 · immutable
#2047261 ·published 2011-04-16 20:38 UTC
rendered paste body
// Link list compare functionint compareUsername(void* pUsername1, void* pUsername2){	int result = 0;	char username1[1];	char username2[1];	strncpy(username1, ((USER*)pUsername1)->username, 1);	strncpy(username2, ((USER*)pUsername2)->username, 1);		if (*username1 < *username2)		result = -1;	else if (*username1 > *username2)		result = 1;	else		result = 0;		return result;	//return 0;}/* ============================ addNode ========================================	Inserts data into list.		Pre		pList is pointer to valid list				dataInPtr is pointer to insertion data		Post	data inserted or error		Return	-1 if overflow				0 if successful				1 if dupe key  =============================================================================*/int addNode (LIST* pList, void* dataInPtr){	int found;	int success;	NODE* pPre;	NODE* pLoc;	found = _search(pList, &pPre, &pLoc, dataInPtr);	if (found)	{		// Duplicate keys not allowed		return (+1);	}	success = _insert(pList, pPre, dataInPtr);	if (!success)	{		// Overflow		return (-1);	}	return 0;} //end addNode/* ============================= _insert =======================================	Inserts data pointer into a new node.		Pre		pList pointer to a valid list				pPre pointer to a data's predecessor				dataInPtr data pointer to be inserted		Post	data has been inserted in sequence		Return	boolean, 1 is successful,				-1 if memory overflow===============================================================================*/static int _insert (LIST* pList, NODE* pPre, void* dataInPtr){	NODE* pNew;	if (!(pNew = (NODE*) malloc(sizeof(NODE))))	{		return FALSE;	}	pNew->dataPtr = dataInPtr;	pNew->link = NULL;	 if (pPre == NULL)	 {		 // Adding before first nore or to empty list.		 pNew->link = pList->head;		 pList->head = pNew;		 if (pList->count == 0)		 {			 // Adding to empty list. Set rear.			 pList->rear = pNew;		 }	 }	 else	 {		 // Adding in middle or at end		 pNew->link = pPre->link;		 pPre->link = pNew;		 // Now check for add at end of list		 if (pNew->link == NULL)		 {			 pList->rear = pNew;		 }	 }	 (pList->count)++;	 return TRUE;} //end _insert/*	================== _search =================================================	Searches list and passes back address of node 	containing target and its logical predecessor.	   Pre    pList pointer to initialized list 	          pPre  pointer variable to predecessor	          pLoc  pointer variable to receive node	          pArgu pointer to key being sought	   Post   pLoc points to first equal/greater key 	     -or- null if target > key of last node	          pPre points to largest node < key	     -or- null if target < key of first node	   Return boolean true found; false not found ===============================================================================*/int _search (LIST*  pList, NODE** pPre, NODE** pLoc, void*  pArgu){//	Macro Definition #define COMPARE \	( ((* pList->compare) (pArgu, (*pLoc)->dataPtr)) )#define COMPARE_LAST \	((* pList->compare) (pArgu, pList->rear->dataPtr))	int result;		*pPre  = NULL;	*pLoc  = pList->head;			if (pList->count == 0)	{		return FALSE;	}	// Test for argument > last node in list 	if ( COMPARE_LAST > 0) 	{		*pPre = pList->rear;		*pLoc = NULL;		return FALSE;	} // if	while ( (result = COMPARE) > 0 )	{		// Have not found search argument location 	    *pPre = *pLoc;	    *pLoc = (*pLoc)->link;	}		if (result == 0)	{		// argument found--success 		return TRUE;	}	else	{	   return FALSE;	}} // end _search