The need for a problem is to reverse the list, for example, 1->2->3->4->5 into 5->4->3->2->1, Classic can have two solutions, recursive and non-recursive, the following gives the C + + two-week implementation process.
#include <iostream>using namespace Std;const int N = 6;typedef int datatype;//define data type typedef struct NODE{// Create a linked list Nodedatatype data;struct node* next;} Linkednode,*linklist; Linklist createlist (DataType a[n])//Build table function {linkednode* listhead = new Linkednode (); Listhead->data= A[0]; listhead->next= null;for (int i = N-1;i >= 1;i-) {linkednode* p = new Linkednode ();p->data = a[i];p->next = listhead->next; Listhead->next = P;} return listhead;} void Printlist (linklist listhead)//implementation of an output table function {if (Listhead = = NULL) cout<< "This is empty list" <<endl;else{ linkednode* p = listhead;while (P! = NULL) {cout<<p->data<< "";p = P->next;} Cout<<endl;}} void Recreverselist (linkednode* pcur,linklist& listhead)//recursive implementation table inversion {if ((NULL = = pcur) | | (NULL = = Pcur->next)) {listhead = pcur;} else{linkednode* Pnext = pcur->next; Recreverselist (pnext,listhead);p next->next = pcur;pcur->next= NULL;}} void Unrecreverselist (linklist& listhead)//inverse of non-recursive implementation table {if (NULL = = Listhead) return; Linkednode *pre,*cur,*nex;pre = Listhead;cur = Pre->next;while (cur) {nex = Cur->next;cur->next = Pre;pre = Cur;cu R = NEX;} Listhead->next= NULL; Listhead = Pre;} int main () {int a[n] = {1,2,3,4,5,6}; linkednode* list = CreateList (a); Printlist (list); linkednode* ptemp = list; Recreverselist (ptemp,list); Printlist (list); Unrecreverselist (list); Printlist (list); return 0;}
Operation Result:
Implementation of C + + linking list inversion