用C++模板技术写的单链表

来源:互联网 发布:网络销售有没有三包 编辑:程序博客网 时间:2024/06/06 00:21
第一段应用C++类模板技术写的东西,并且用在现在的系统中,效果还不错,感觉代码还不够精简。

  1. //单链表模板类
  2. using namespace std;
  3. template <class ElemType>
  4. class CSingleLink{
  5. private:
  6.  ElemType* phead;                //头指针
  7.  ElemType* ptail;                //尾指针
  8.  ElemType* pcurr;                //当前指针
  9.  int NodesNum;                   //节点数量
  10.  int CurrPos;                    //当前指针的序号(第一个节点的序号为1)
  11. protected:
  12. public:
  13.  CSingleLink();
  14.  ~CSingleLink();
  15.  int GetNNum();                   //返回节点数量
  16.  int GetIndex();                  //返回当前指针的序号
  17.  ElemType* First();
  18.  ElemType* Next();                //返回当前节点的下一个节点的指针
  19.  ElemType* Prev();                //返回当前节点的前一个节点的指针
  20.  ElemType* MoveTo(int index);     //返回序号为index的节点指针(index'svalue from 1)
  21.  ElemType* Insert(int pos,ElemType* aNode);//向第pos个节点插入节点aNode(pos >=                            1 )
  22.  ElemType* Append(ElemType* aNode);        //向单链表末尾追加一个节点aNode
  23.  ElemType* Delete(int pos);                //删除第pos个节点
  24.  int       Delete(ElemType* p);            //删除某个节点
  25.  void Clear();                             //清除链表
  26. };
  27. template <class ElemType>
  28. CSingleLink<ElemType>::CSingleLink()
  29. {
  30.   phead = NULL;
  31.   ptail = NULL;
  32.   pcurr = NULL;
  33.   NodesNum = 0;
  34.   CurrPos = 0;
  35. }
  36. template <class ElemType>
  37. CSingleLink<ElemType>::~CSingleLink()
  38. {
  39.  Clear();
  40. }
  41. template <class ElemType>
  42. int CSingleLink<ElemType>::GetNNum(){
  43.    return (NodesNum);
  44. }
  45. template <class ElemType>
  46. int CSingleLink<ElemType>::GetIndex(){
  47.    return(CurrPos);
  48. }
  49. template <class ElemType>
  50. ElemType* CSingleLink<ElemType>::First(){
  51.    if(NodesNum > 0){
  52.        pcurr = phead;
  53.        CurrPos = 1;
  54.        return (pcurr);
  55.    }
  56.    else return NULL;
  57. }
  58. template <class ElemType>
  59. ElemType* CSingleLink<ElemType>::Next(){
  60.  if(NodesNum > 0){
  61.  pcurr = pcurr->pnext;
  62.                if(pcurr != NULL)
  63.      CurrPos++;
  64.  return(pcurr);
  65.  }
  66.  else return NULL;
  67. }
  68. template <class ElemType>
  69. ElemType* CSingleLink<ElemType>::Prev(){
  70.    if(NodesNum > 1){                   //NodesNum = 1只有头结点
  71.          ElemType* pN = phead;
  72.          while(pN->next != pcurr)
  73.              pN = pN->next;
  74.          pcurr = pN;
  75.          CurrPos--;
  76.          return(pcurr);
  77.  }
  78.  else return NULL;
  79. }
  80. template <class ElemType>
  81. ElemType* CSingleLink<ElemType>::MoveTo(int index){
  82.  if(NodesNum > 0 && index >0 && index <= NodesNum){
  83.  pcurr = phead;
  84.  CurrPos = 1;
  85.  for(int i = 1;i < index; i++)
  86.   this->Next();
  87.        return(pcurr);
  88.  }
  89.  else return NULL;
  90. }
  91. template <class ElemType>
  92. ElemType* CSingleLink<ElemType>::Insert(int pos,ElemType* aNode){
  93.  if(pos < 1 || pos  > NodesNum || NodesNum == 0) return NULL;
  94.  if(NodesNum == 0){
  95.  phead = aNode;
  96.  ptail = aNode;
  97.  aNode->next = NULL;
  98.  CurrPos = 1;
  99.  NodesNum = 1;
  100.  }
  101.  else if(pos == 1){
  102.        aNode->next = phead;
  103.        phead = aNode;
  104.  CurrPos = 1;
  105.  NodesNum++;
  106.  }
  107.    else if(this->MoveTo(pos - 1) != NULL){
  108.  aNode->next = pcurr->next;
  109.  pcurr->next = aNode;
  110.  pcurr = aNode;
  111.  CurrPos = pos;
  112.  NodesNum++;
  113.  }
  114.  pcurr = aNode;
  115.  return(pcurr);
  116. }
  117. template <class ElemType>
  118. ElemType* CSingleLink<ElemType>::Append(ElemType* aNode){
  119.  if(NodesNum == 0){
  120.  phead = aNode;
  121.  ptail = aNode;
  122.  }
  123.  else {//if(this->MoveTo(NodesNum) != NULL){
  124.                ptail->pnext = aNode;
  125.                ptail = aNode;
  126.  }
  127.        NodesNum++;
  128.        aNode->pnext = NULL;
  129.  pcurr = aNode;
  130.  CurrPos = NodesNum;
  131.  return (pcurr);
  132. }
  133. template <class ElemType>
  134. ElemType* CSingleLink<ElemType>::Delete(int pos){
  135.  if(pos < 1 || pos  > NodesNum || NodesNum == 0) return NULL;
  136.  if(pos == 1){
  137.  this->MoveTo(1);
  138.  phead = phead->next;
  139.  }else if(this->MoveTo(pos - 1) != NULL){
  140.     pcurr->next = pcurr->next->next;
  141.  }
  142.  NodesNum--;
  143.    CurrPos = pos - 1;
  144.  return(pcurr);
  145. }
  146. template <class ElemType>
  147. int CSingleLink<ElemType>::Delete(ElemType* p){
  148.    if(NodesNum > 0){
  149.        int r = 1;
  150.        pcurr = this->First();
  151.        while(pcurr != NULL)
  152.        {
  153.           if(pcurr != p)
  154.           {
  155.             pcurr = this->Next();
  156.             r++;
  157.           }
  158.           else break;
  159.        }
  160.        if(pcurr != NULL)
  161.        {
  162.            delete pcurr;
  163.            NodesNum--;
  164.            CurrPos = r - 1;
  165.            return r;
  166.        }else return NULL;
  167.    }
  168.    else return NULL;
  169. }
  170. template <class ElemType>
  171. void CSingleLink<ElemType>::Clear(){
  172.    if(NodesNum > 0)
  173.    {
  174.      ElemType *aNode;
  175.      for(int i = 0; i< NodesNum;i++){
  176.              aNode = pcurr;
  177.              delete aNode;
  178.              if(pcurr != NULL)
  179.              pcurr = pcurr->pnext;
  180.      }
  181.    }
  182.   phead = NULL;
  183.   ptail = NULL;
  184.   pcurr = NULL;
  185.   NodesNum = 0;
  186.   CurrPos = 0;
  187. }

原创粉丝点击