// exp.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<iostream> #include<windows.h> using namespace std; struct Node { int data; struct Node* next; }; Node* createList() { Node *head = NULL,*q = NULL; Node *p = new Node; p->next = NULL; head = p; char c; cout<<"请输入链表元素并以#号结束:"; while(1) { cin>>c; if(c == '#') { break; } else { q = new Node; q->data = c - '0'; q->next = NULL; p->next = q; p = q; } } head = head->next; return head; } void print(Node *head) { Node *p = head; if(head == NULL) { cout<<"链表没有元素!"; } else { cout<<"链表元素为:"; while(p != NULL) { cout<<p->data<<" "; p = p->next; } } cout<<endl; } Node* reverseList(Node* head) { if(head == NULL||head->next==NULL) { return NULL; } Node* p = head; Node* q = head->next; Node* s = NULL; while(q) { s = q->next; q->next = p; p = q; q = s; if(s == NULL) { head->next = NULL; head = p; } } return head; } int _tmain(int argc, _TCHAR* argv[]) { Node *s = createList(); print(s); Node *sr = reverseList(s); cout<<"反序后"; print(sr); system("pause"); return 0; }