Table inversion [arrays and linked lists]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.