#include<iostream> using namespace std; class NODE { public: int data; NODE * next; }; NODE* create(); void print(NODE *); NODE* reverse(NODE *); int main() { NODE *head = create(); print(head); head = reverse(head); print(head); return 0; } NODE* create() { int data; NODE *head, *p, *q; head = new NODE(); p = head; cout<<"输入单链表数据"<<endl; cin>>data; while(data!=-1) { q = new NODE(); p->next = q; p = p->next; p->data = data; cin>>data; } p->next = NULL; return head; } void print(NODE *head) { cout<<"链表内容:"; NODE *p = head->next; while(p!=NULL) { cout<<p->data<<" "; p = p->next; } cout<<endl; } NODE* reverse(NODE *head) { NODE *p1, *p2, *p3; p1 = head; p2 = head->next; while(p2!=NULL) { p3 = p2->next; p1->data = p2->data; p2->next = p1; p1 = p2; p2 = p3; } head->next = NULL; head = p1; return head; }