当前位置:编程学习 > C/C++ >>

链表反向,

 
template <typename T>  
class Node   
{  
 public:  
  
 T data;  
 Node* next;  
};  
template <typename T>  
class link  
{  
public:  
link()  
{  
 head=new Node<T>();  
head->data=1;  
head->next=NULL;  
  
Node<T>* pNew=NULL;  
Node<T>* p=NULL;  
p=head;  
  
while(true)  
 {  
  T i;  
  cin>>i;  
  if(i!=-1)  
  {  
      pNew=new Node<T> ();  
      pNew->data=i;  
      pNew->next=NULL;  
      p->next=pNew;  
      p=pNew;  
  }  
  else  
  {  
  break;  
  }  
}  
}  
 void reverseLink()  
 {  
  assert(head!=NULL);  
  Node<T>* previouse=NULL;  
  Node<T>* p=head;  
  Node<T>* tmp=NULL;  
  while(p!=NULL)  
  {     
      tmp=p->next;  
      p->next=previouse;  
      previouse=p;  
      p=tmp;  
  
  }  
head=previouse;  
  
}  
  
void printLink()  
{  
 Node<T>* p=head;  
  
 while(p!=NULL)  
 {  
     cout<<p->data<<" ";  
     p=p->next;  
   
 }  
}  
private:  
Node<T>* head;  
};  
链表反向:
 
 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,