单链表,写程序总被打断,NND,最近烦死了

it2022-05-08  7

代码   1  #include  < stdio.h >   2  #include  < iostream >   3  using   namespace  std;   4    5  template  < typename T >   6  class  SLink;   7    8  template  < typename T >   9  class  SLinkNode  10  {  11  public :  12      SLinkNode(T data);  13      SLinkNode();  14       ~ SLinkNode();  15      SLinkNode < T >   * next;  16      T get_data(){ return  m_data;};  17      friend  class  SLink < T > ;  18  protected :  19  private :  20      T m_data;  21  };  22   23  template < class  T >  24  SLinkNode < T > ::SLinkNode(T data)  25  {  26      m_data  =  data;  27      next  =  NULL;  28  }  29   30  template < class  T >  31  SLinkNode < T > ::SLinkNode()  32  {  33      m_data  =   0 ;  34      next  =  NULL;  35  }  36   37  template < class  T >  38  SLinkNode < T > :: ~ SLinkNode()  39  {  40      cout  <<   " deconstruction \n " ;  41  }  42   43   44  /*  ================================================ */  45  template < class  T >  46  class  SLink  47  {  48  public :  49      SLink();  50       ~ SLink();  51       int  insert_node(SLinkNode < T >   * _node);  52       int  delete_node();  53       int  get_length();  54       int  sort_link();  55       int  invert_link();  56       void  SLinkPrint();  57      SLinkNode < T >   * head;  58      SLinkNode < T >   * curr_ptr;  59       // SLinkNode<T> *next;  60  protected :  61  private :  62  };  63   64  template < typename T >  65  SLink < T > ::SLink()  66  {  67      head  =  NULL;  68      curr_ptr  =  NULL;  69  }  70   71  template  < typename T >  72  SLink < T > :: ~ SLink()  73  {  74      curr_ptr  =  head;  75       int  cnt  =   0 ;  76       while (NULL  !=  curr_ptr)  77      {  78          cout  <<   " delete  "   <<  cnt  <<   " \n " ;  79          delete_node();  80          cnt ++ ;  81      }  82  }  83   84  /*  85      插入实际上是在末尾处插入  86  */  87  template  < typename T >  88  int  SLink < T > ::insert_node(SLinkNode < T >   * _node)  89  {  90       if  (NULL  ==  _node)  91      {  92           return   0 ;  93      }  94        95       if  (NULL  ==  head)  96      {  97          head  =  _node;  98          curr_ptr  =  head;  99           return   1 ; 100      } 101  102       if  (NULL  ==  curr_ptr -> next) 103      { 104          curr_ptr -> next  =  _node; 105      } 106       else 107      { 108          SLinkNode < T >   * temp_next  =  curr_ptr -> next; 109          curr_ptr -> next  =  _node; 110          _node -> next  =  temp_next; 111      } 112       return   1 ; 113  } 114  115  116  /* 117      单链表的数据中删除时必须是删除head处的节点 118  */ 119  template  < typename T > 120  int  SLink < T > ::delete_node() 121  { 122       if  (NULL  ==  head) 123      { 124           return   1 ; 125      } 126       else   if  (curr_ptr  ==  head ) 127      { 128          SLinkNode < T >   * temp  =  head; 129          curr_ptr  =  head -> next; 130          head  =  curr_ptr; 131          free(temp); 132          temp  =  NULL; 133      } 134       else 135      { 136  //          SLinkNode<T> *temp_node = curr_ptr; 137  //          curr_ptr = curr_ptr->next; 138  //          free(temp_node); 139  //          temp_node = NULL; 140      } 141       return   1 ; 142       143  } 144  145  template  < typename T > 146  int  SLink < T > ::get_length() 147  { 148       int  len  =   0 ; 149      SLinkNode < T >   * ptr  =  head; 150       while (NULL  !=  ptr) 151      { 152          len ++ ; 153          ptr  =  ptr -> next; 154      } 155       return  len; 156  } 157  158  template  < typename T > 159  int  SLink < T > ::sort_link() 160  { 161       //  insert sort 162       int  len  =  get_length(); 163      SLinkNode < T >   * node_i; 164      SLinkNode < T >   * node_j; 165      T temp; 166       for  (node_i  =  head; NULL  !=  node_i -> next; node_i  =  node_i -> next) 167      { 168           for  (node_j  =  node_i -> next; NULL  !=  node_j; node_j  =  node_j -> next) 169          { 170               if  (node_i -> m_data  >  node_j -> m_data) 171              { 172                  temp  =  node_i -> m_data; 173                  node_i -> m_data  =  node_j -> m_data; 174                  node_j -> m_data  =  temp; 175              } 176          } 177      } 178       return   1 ; 179  } 180  181  template  < typename T > 182  int  SLink < T > ::invert_link() 183  { 184       int  len  =  get_length(); 185       int  half_len  =  len  >>   1 ; 186       int  cnt  =  len  -   1 ; 187       int  i  =   0 , j  =   0 ; 188      T temp; 189      SLinkNode < T >   * ptr; 190      SLinkNode < T >   * according_ptr; 191       for  (ptr  =  head; i  <  half_len; ptr  =  ptr -> next)  //  do half_len times 192      { 193           //  for every time, here 194          according_ptr  =  ptr; 195          j  =   0 ; 196           while (j  <  cnt) 197          { 198              according_ptr  =  according_ptr -> next; 199              j ++ ; 200          } 201          temp  =  according_ptr -> m_data; 202          according_ptr -> m_data  =  ptr -> m_data; 203          ptr -> m_data  =  temp; 204          i ++ ; 205          cnt  -=   2 ; 206      } 207       return   1 ; 208  } 209  210  template  < typename T > 211  void  SLink < T > ::SLinkPrint() 212  { 213      SLinkNode < T >   * ptr  =  head; 214       while (NULL  !=  ptr) 215      { 216          cout  <<  ptr -> get_data()  <<  endl; 217          ptr  =  ptr -> next; 218      } 219  } 220  221  void  main() 222  { 223      SLink < double >  SL; 224       double  data; 225       for  ( int  i  =   0 ; i  <   10 ; i ++ ) 226      { 227          data  =  rand()  %   1000 ; 228          SLinkNode < double >   * node  =   new  SLinkNode < double > (data); 229          SL.insert_node(node); 230      } 231      cout  <<   " original data:\nLength of the link:  "   <<  SL.get_length()  <<   " \n " ; 232      SL.SLinkPrint(); 233      SL.sort_link(); 234      cout  <<   " after sorted:\n " ; 235      SL.SLinkPrint(); 236      cout  <<   " after invert:\n " ; 237      SL.invert_link(); 238      SL.SLinkPrint(); 239  }

 

转载于:https://www.cnblogs.com/luweiseu/archive/2010/03/19/1689768.html

相关资源:日本NND网页模板(2)

最新回复(0)