For arrays (which can also be linear tables), the inverse is the end-to-end data 22 interchange, first calculating the number of interchanges:
Intermediate need a temporary variable staging data, seemingly simple, in fact a bit around, the key or array subscript starting from 0, this is very troublesome! This little problem is the most basic practice!
Full code:
#include <iostream>using namespacestd;intMain () {inta[]={1,2,3,4,5,7,8,9}; intlen=sizeof(a)/sizeof(int);/*calculating the length of an array*/ intTemp/*Temp Variable*/ for(intI=0; I<= (len-1)/2; i++)/*calculate the number of interchanges*/{Temp=A[i]; A[i]=a[len-1-I.];/*Exchange*/A[len-1-i]=temp; } for(i=0; i<len;i++)/*View Results*/cout<<a[i]<<" "; return 0;}
The second, single - linked list , this is a lot more difficult, because the list has only one head pointer, to rely on this pointer to the entire chain operation, requires a high skill! First watch the linked list:
If the inverse (reverse order), the exchange of data is not possible, only a different way of thinking, the node between the point of reversal, according to this idea, head change tail, tail change head, and the most difficult is how to change the direction of the pointer? The method of step conversion is used here, in addition to the head pointer L, also with two cursors pointer Q and r Auxiliary movement!
Code
void reverselist (List &l) /* Span style= "color: #008000;" > reverse operation */ {List q,p,r; P =L; Q =p->next; P ->next=null; /* */ while (Q!=null) /* cursor Q, r with L operation */ =q->next; Q ->next=L; L =Q; Q =R; }}
Just looking at the code may not be difficult, but this shift is ingenious, and it may take more than one walkthrough to piggyback! The approximate form is:
L,q,r three relations to see clearly, more easy to remember, l in the rear, Q Center, R always in the front, which is in the process of running the sequence of production, is just a way of memory!
Full code
//note:your Choice is C + + IDE#include <iostream>using namespaceStd;typedefstructnode{intdata; structNode *next;}*List,node;voidCreatlist (List &L) { inte; CIN>>e; if(e==0)/*input 0 o'clock end of linked list creation*/L=NULL; Else{L= (List)malloc(sizeof(Node)); L->data=e; Creatlist (L-next); }}voidTraverselist (List &l)/*Traverse, print linked list*/{ while(L) {cout<<L->data<<" "; L=l->Next; };}voidReverselist (List &l)/*Reverse Operation*/{List q,p,r; P=L; Q=p->Next; P->next=null;/*turn the head knot into a tail knot.*/ while(Q!=null)/*cursor Q, r with L operation*/{R=q->Next; Q->next=m; L=Q; Q=R; }}voidDestorylist (List &l)/*destroyed*/{ if(l) {List (l-next); Free(L); L=NULL; }}intMainvoid) {List L; Creatlist (L);/*Create*/reverselist (L);/*Reverse*/traverselist (L);/*Traverse*/destorylist (L);/*destroyed*/ return 0;}
Table inversion [arrays and linked lists]