该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
}
/*
Compare two PhoneRecord structures
Returns -1 if the name for the first is < name for the second
Returns 0 if the name for the first is equal to the name for the second
Returns +1 if the name for the first is > name for the second
*/
int compare_records(struct PhoneRecord *pFirst, struct PhoneRecord *pSecond)
{
return compare_names(pFirst->name, pSecond->name);
}
/* Compare two names
Returns -1 if the first is < the second
Returns 0 if the first is equal to tthe second
Returns +1 if the first is > the second
The comparison is by second name. If second names are equal,
first names are compared.
*/
int compare_names(struct Name first, struct Name second)
{
int result = 0;
result = strcmp(first.secondname,second.secondname);
return (result != 0 ? result : strcmp(first.firstname,second.firstname));
}
/* Insert a node into the list */
void insert_node(struct Node *pNode)
{
struct Node *pCurrent = NULL;
struct Node *pPrevious = NULL;
/* Check for empty list */
if(!pStart)
{
pStart = pNode; /* Store address of the node as the start node */
return;
}
/* Find position to insert the new node */
pCurrent = pStart;
while(pCurrent)
{
if(compare_records(pNode->pRecord, pCurrent->pRecord) <= 0)
{ /* New node goes before current list node */
pNode->pNext = pCurrent; /* Set new node next pointer to current */
if(!pPrevious) /* If pCurrent is the first node */
{
pNode->pNext = pStart; /* New node next pointer points to current */
pStart = pNode; /* New node is the first node */
}
else
{ /* Otherwise... */