上篇说了我心中的链表,它很是简单,就是一个简简单单的数据的链接,链表就是数据单元类型的指针。而教科书上的链表就相对复杂了些。如它会为链表单独定义一种类型。这点在开始学习链表的时候总是弄糊涂。下面就看看教科书上是如何定义链表类型的。例子还是上篇的电话簿。
第一步:定义结点类型。
typedef struct LinkMan
{
   char name[PHB_NAME_LEN];
   char num[PHB_NUM_LEN];
   struct LinkMan *next;
}LinkMan,*pLinkMan;
第二步:定义链表类型:
typedef struct PhoneBook
{
  pLinkMan first;             //头指针
  pLinkMan last;             //尾指针
   int len;                         //链表长度
}PhoneBook,*pPhoneBook;
有的没有尾指针,只有头指针。定义如下:
typedef struct PhoneBook
{
  pLinkMan first;             //头指针
   int len;                         //链表长度
}PhoneBook,*pPhoneBook;
OK ,到目前为止,一个链表类型基本就定义完成了,剩下的就是使用了。虽然这种定义开起来容易使初学者糊涂,但它还是蛮好用的。下面就简单看下它的使用:
PhoneBook phonebook;

/************************************************************************/
/* Function:    InitPhoneBook                                                                                         */
/* Description:    Init a phonebook                                                                             */
/* Parameters:                                                                                                                    */
/* phb: phonebook                                                                                                             */
/* Value: void                                                                                                                    */
/* Author: Hoiman                                                                                                             */
/* Date: 2010/11/05                                                                                                         */
/************************************************************************/
void InitPhoneBook(pPhoneBook phb)
{
  phb->first = NULL;
  phb->last = NULL;
  phb->len = 0;
}

/************************************************************************/
/* 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
  {
     if (1 == index)
    {
       if (pTemp != NULL)
      {
        man->next = pTemp;
        phb->first = man;
      }
       else
      {
        phb->first = man;
        phb->last = man;
        man->next = NULL;
      }
    }
     else
    {
       for (i = 2; 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;
  pLinkMan pPrior = pTemp;
   for(i = 0; i < phb->len; ++i)
  {
     if (0 == strcmp(pTemp->name,name))
    {
       if (0 == i)
      {
        phb->first = (phb->first)->next;
         //栈上的空间不能用free进行释放
       //  free(pTemp);
          phb->len--;
         return;
      }
       else
      {
        pPrior->next = pTemp->next;
         if (pTemp == phb->last)
        {
          phb->last = pPrior;
        }
         //栈上的空间不能用free进行释放
         //    free(pTemp);
           phb->len--;
         return;
      }
    }
     else
    {
      pPrior = pTemp;
      pTemp = pTemp->next;
    }
  }
  printf( "Can't find the linkman in phonebook!\n");
}