2010年9月6日星期一

在链表中添加节点

/*****************************************************************************
    56   *
    57   * 功能:在链表的末尾添加节点
    58   * 传入参数:链表的首节点和需要添加的节点
    59   *
    60   * **************************************************************************/
    61  void addNode(NODE* pHead, NODE* node)
    62  {
    63      if (NULL==pHead) {      /*如果首节点为空,则初始化有问题,输出错误信息,并退出*/
    64          DEBUG("in addNode, pHead is NULL\n");
    65          exit(-1);
    66      }
    67
    68      if(pHead->next!=NULL)    /*首节点不为空,则遍历链表,直到链表末尾*/
    69      {
    70          pHead=pHead->next;
    71      }
    72      pHead->next=(NODE*)malloc(sizeof(NODE));    /*在链表末尾,创建内存空间*/
    73      if (NULL==pHead->next) {        /*判断是否为野指针*/
    74          DEBUG("in addNode, malloc error!\n");
    75          exit(-1);
    76      }
    77
    78      pHead=pHead->next;  /*在链表的末尾添加节点*/
    79      pHead->x=node->x;
    80      pHead->next=NULL;   /*链表的末尾节点指针指向NULL*/
    81
    82      DEBUG("addNode success!\n");
    83  }

没有评论:

发表评论