/************************************************************************/
/* Function: InitPhoneBook */
/* Description: Init a phonebook */
/* Parameters: */
/* phb: phonebook */
/* Value: void */
/* Author: Hoiman */
/* Date: 2010/11/05 */
/************************************************************************/
void InitPhoneBook(pPhoneBook phb)
{
pLinkMan man = (pLinkMan)malloc(sizeof(LinkMan));
phb->first = man;
phb->last = man;
phb->len = 0;
man->next = NULL;
}
/************************************************************************/
/* Function: InsertLinkMan */
/* Description: Insert a linkman to a phonebook */
/* Parameters: */
/* phb: phonebook , index: the insert position man: new linkman */
/* Value: void */
/* Author: Hoiman */
/* Date: 2010/11/05 */
/************************************************************************/
void InsertLinkMan(pPhoneBook phb, int index, pLinkMan man)
{
int i = 0;
pLinkMan pTemp = phb->first;
if (index <= 0)
{
printf("The index must be bigger than zero.\n");
return;
}
else if (index > phb->len + 1)
{
printf("The index is bigger than the length of phonebook.\n");
return;
}
else
{
for (i = 1; i < index; ++i)
{
pTemp = pTemp->next;
}
man->next = pTemp->next;
pTemp->next = man;
phb->len++;
}
}
/************************************************************************/
/* Function: DeleteLinkManByName */
/* Description: Delete a linkman from a phonebook by name */
/* Parameters: */
/* phb: phonebook , name: the name of linkman will be deleted */
/* Value: void */
/* Author: Hoiman */
/* Date: 2010/11/05 */
/************************************************************************/
void DeleteLinkManByName(pPhoneBook phb, char *name)
{
int i = 0;
pLinkMan pTemp = (phb->first)->next;
pLinkMan pPrior = phb->first;
for(i = 0; i < phb->len; ++i)
{
if (0 == strcmp(pTemp->name,name))
{
pPrior->next = pTemp->next;
if (pTemp == phb->last)
{
phb->last = pPrior;
}
phb->len--;
//栈上的空间不能用free进行释放
// free(pTemp);
return;
}
else
{
pPrior = pTemp;
pTemp = pTemp->next;
}
}
printf("Can't find the linkman in phonebook!\n");
}