#include <iostream>
#define Reverse2
using namespace std;
struct linka{
int data; linka* next; };
void reverse(linka* &head) { if(head == NULL) return; linka* pPre = NULL; linka* pCur = head; while(pCur != NULL) { linka* pNext = pCur->next; pCur->next = pPre; pPre = pCur; pCur = pNext; } head = pPre; }
linka* reverse2(linka* cur, linka*& head) { if(cur == NULL || cur->next == NULL) { head = cur; return cur; } else
{ linka* temp = reverse2(cur->next, head); temp->next = cur; return cur; } }
int main() { linka arrayTest[10]; for(int i = 0; i < 10; i++) { arrayTest[i].data = i; arrayTest[i].next = arrayTest+ i +1; cout<< arrayTest[i].data<<endl; } arrayTest[9].next = NULL; linka* phead = arrayTest;
#ifdef Reverse2
linka* cur = reverse2(phead, phead); cur->next = NULL; #else reverse(phead);
#endif while(phead->next != NULL) { cout<< phead->data<<endl; phead = phead->next; } cout<<phead->data<<endl; }
相关文章: