#include <iostream>using namespace std;struct Node{ int data;Node * next;};Node * Create_List(){ Node * head=new Node;//头结点 Node * rear=head;head->next=NULL;Nod 展开
#include <iostream>using namespace std;struct Node{ int data;Node * next;};Node * Create_List(){ Node * head=new Node;//头结点 Node * rear=head;head->next=NULL;Node * s;int x;cin>>x;while(x>0) { s=new Node ;s->data=x;s->next=NULL;rear->next=s;rear=s;cin>>x;} return head;}void Print_List(Node * head){ Node * p=head->next;while(p!=NULL) { cout<<p->data<<"";p=p->next;}}void Reverse_List(Node * head){ if(head->next==NULL) return ;Node * p,*q;p=head->next;//第一个元素 q=head->next;while(q->next!=NULL) q=q->next;//最后一个元素 int temp;//交换两个数用的中间变量 while(p!=q&&q->next!=p) { temp=p->data;p->data=q->data;q->data=temp;p=p->next;//指向下一个元素 Node * s=head;while(s->next!=q) s=s->next;q=s;//指向前一个元素 }} int main(int argv,char * argc[]){ Node * head;head=Create_List();cout<<"逆置前单链表元素:"<<endl;Print_List(head);Reverse_List(head);cout<<"\n逆置后单链表元素:"<<endl;Print_List(head);cout<<endl;system("pause");return 0;} //我用的是DEV C++编译器环境 收起