链表反转怎么也写不对。。。好烦躁啊啊啊啊,花了好多时间了,感觉要崩溃了
#include<iostream> using namespace std; typedef struct LNode *List; struct LNode { int data; List next; }; List reverse(List &L) { List p, r, s; p = L->next; L->next = NULL; r = p->next; p->next = NULL; while (r) { s = r; r->next = p; p = r; r = s->next; } L->next = p; return L; } void InitList(List &L) { L = new LNode; L->next = NULL; } void CreatList(List &L, int n) { List r = L; for (int i = 0; i < n; i++) { List q = new LNode; cin >> q->data; q->next = NULL; r->next = q; r = q; } } void Print(List &L, int n) { List p = L->next; for (int i = 0; i < n; i++) { cout << p->data; p = p->next; } } int main() { List L; InitList(L); CreatList(L, 5); Print(L, 5); List p = reverse(L); Print(p, 5); return 0; }