原来错误的代码:
view plaincopy to clipboardprint?
SllNode *SllCopy( SllNode *const pTarget )    //拷贝单链表,若拷贝失败或参数为空链表,返回NULL,否则返回新链表的首指针;函数并不破坏原链表        
{        
        if( NULL == pTarget )        
                return NULL;        
    
        SllNode *pCopy = new SllNode;        
        if( NULL == pCopy )        
                return NULL;        
    
        register SllNode *pTmpTar = pTarget;        
        register SllNode *pTmpCopy = pCopy;        
                
        while( NULL != pTmpTar )        
        {        
                pTmpCopy->Data = pTmpTar->Data;        
                pTmpCopy->pNext = pTmpTar->pNext;        
    
                pTmpTar = pTmpTar->pNext;        
                pTmpCopy = pTmpCopy->pNext;        
        }        
        return pCopy;        
}    
由于才起步,经验不足,导致以上错误。
用VS2008逐条跟踪前期,并未发现此函数有错。但发现其它函数在修改SllCopy函数返回的“复制”的单链表后,原模板链表也被修改,于是想到可能是指针无意间被修改,故将参数改为const并再次调试,问题依旧。
但排除了首指针被修改问题之后,依次隔离其余各函数,发现只要调用了SllCopy函数便会出现此问题,因此将调试重点放在了此函数。
逐条跟踪局部变量,发现除模板链表首指针pTarget 和 目标链表首指针pCopy 不一样之外,其余指针都相同,便发现了问题所在。我只是简单的将原链表的各节点的pNext指针复制了一遍到pCopy所指向的目标链表,因此,函数调用后,相当于创建了一个新首指针pCopy去指向模板链表,那么通过对“复制”后的链表进行操作也就修改了原模板链表,导致后期错误的产生。
现将函数修改如下:
view plaincopy to clipboardprint?
SllNode *SllCopy( SllNode *const pTarget )    //拷贝单链表,若拷贝失败或参数为空链表,返回NULL,否则返回新链表的首指针;函数并不破坏原链表        
{        
        if( NULL == pTarget )        
                return NULL;        
    
        SllNode *pCopy = new SllNode;        
        if( NULL == pCopy )        
                return NULL;        
    
        register SllNode *pTmpTar = pTarget;        
        register SllNode *pTmpCopy = pCopy;        
                
        while( NULL != pTmpTar )        
        {        
                pTmpCopy->Data = pTmpTar->Data;        
                pTmpCopy->pNext = new SllNode;        
                if( NULL == pTmpCopy->pNext )        
                        return NULL;        
    
                pTmpCopy->pNext->pNext = NULL;        
                pTmpCopy = pTmpCopy->pNext;        
                        
                pTmpTar = pTmpTar->pNext;        
        }        
    
        //Delete the useless rear node        
        pTmpCopy = pCopy;        
        while( NULL != pTmpCopy->pNext->pNext )        
                pTmpCopy = pTmpCopy->pNext;        
                
        pTmpCopy->pNext = NULL;        
        delete pTmpCopy->pNext;        
    
        return pCopy;        
}    

 
最后删除了一个无用尾节点,但是始终觉得这几行代码应该可以不用,需要对链表复制进行怎样的改动呢?希望能得听听大家的意见。
由于才起步,代码一般很幼稚,望高手包含,更希望热心人多多指点,非常感谢。
 
【注】下附没有无用尾节点的SllCopy版本
 
SllNode *SllCopy( SllNode *const pTarget )  //拷贝单链表,若拷贝失败或参数为空链表,返回NULL,否则返回新链表的首指针;函数并不破坏原链表
{
  if( NULL == pTarget )
    return NULL;

  SllNode *pCopy = new SllNode;
  if( NULL == pCopy )
    return NULL;

  register SllNode *pTmpTar = pTarget;
  register SllNode *pTmpCopy = pCopy;
    
  while( NULL != pTmpTar )
  {
    SllNode *pTmp = new SllNode;
    if( NULL == pTmp )
      return NULL;
    
    pTmp->Data = pTmpTar->Data;
    pTmp->pNext = NULL;

    pTmpCopy->pNext = pTmp;
    pTmpCopy = pTmpCopy->pNext;

    pTmpTar = pTmpTar->pNext;
  }

  //Delete the useless rear node
  //pTmpCopy = pCopy;
  //while( NULL != pTmpCopy->pNext->pNext )
  //  pTmpCopy = pTmpCopy->pNext;
  //
  //pTmpCopy->pNext = NULL;
  //delete pTmpCopy->pNext;

  return pCopy->pNext;
}