#include <iostream>using namespace STD;structnode{intData Node *next; Node (intD =int()):d ATA (d), next (NULL) {}};classclist{ Public: Clist (intA[],intN): First (NULL) {inti =0; Node *p = NULL; for(; i < n; i++) {Node *s =NewNode (A[i]);if(first = = NULL) {first = s; p = First; }Else{S->next = p->next; P->next = s; } } }voidRelea ()//recursive reverse order. {Relea (Null,first); }voidPrintf () {Node *p = first; while(P! = NULL) {cout<< P->data <<"--->"; p = p->next; }cout<< Endl; }Private:voidRelea (Node *prv,node *t)//recursion in reverse order, feel nothing so mean. {if(T->next = = NULL) {first = t;return; }Else{PRV = t; Node *p = t->next;if(PRV = = first) {prv->next = NULL; } Relea (Prv,p); P->next = PRV; } }Private: Node *first;};intMain () {intA[] = {1,2,3,4,5,6,7,8,9}; Clist List1 (A,sizeof(a)/sizeof(int)); List1. Printf ();//Normal linked list. List1.relea (); List1. Printf ();//list after reverse order. return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Algorithm problem: Recursive reverse order of linked list