Diagram operation for single-linked list inversion

Source: Internet
Author: User

There is a linear table $ (a_1,a_2, \cdots, A_n) $, which takes the lead node of the single-linked list $l$ storage, and designs an algorithm to reverse it in place. The so-called "in-place" refers to the auxiliary space of $o (1) $.

Answer: Scan the original single linked list with $p$ pointer, first $l$ the $next$ domain of the head node to $null$ and become an empty list, then, the $*p$ node is inserted into the $l$ by the head interpolation method. The algorithm is as follows:

1 voidReverse (linklist *&L) {2Linklist *p=l->next,*Q;3l->next=NULL;4      while(p!=NULL)5     {6Q=p->Next;7p->next=l->next;//Insert the *p node in front of the new linked list8l->next=p;9p=Q;Ten     } One  A}

The diagram of the above algorithm is as follows:

The complete code is as follows:

//LinkBase.h#include <stdio.h>#include<malloc.h>typedefintElemtype;typedefstructlnode{elemtype data; structLnode *Next;} linklist;voidInitlist (linklist *&L) {L= (linklist *)malloc(sizeof(linklist)); L->next=NULL;}voidDestroylist (linklist *&L) {linklist*p=l, *q=p->Next;  while(q!=NULL) {         Free(P); P=Q; Q=p->Next; }     Free(P); }intListlength (linklist *m) {linklist*p=m; intI=0;  while(p->next!=NULL) {i++; P=p->Next; }    returni;}voidDisplist (linklist *L) {linklist*p=l->Next;  while(p!=NULL) {printf ("%d",p->data); P=p->Next; } printf ("\ n");}intGetelem (linklist *l,intI,elemtype &e) {    intj=0; Linklist*p=m;  while(J<i && p!=NULL) {J++; P=p->Next; }    if(p==NULL) {        return 0; }    Else{e=p->data; return 1; }}intLocateelem (linklist *L,elemtype e) {linklist*p=l->Next; intn=1;  while(P!=null && p->data!=e) {p=p->Next; N++; }    if(p==NULL)return 0; Else        returnN;}intListinsert (linklist *&l,intI,elemtype e) {    intj=0; Linklist*p=l,*s;  while(j<i-1&& p!=NULL) {J++; P=p->Next; }    if(p==NULL)return 0; Else{s= (linklist *)malloc(sizeof(linklist)); S->data=e; S->next=p->Next; P->next=s; return 1; }}intListdelete (linklist *&l,intI,elemtype &e) {    intj=0; Linklist*p=l,*Q;  while(j<i-1&& p!=NULL) {J++; P=p->Next; }    if(p==NULL)return 0; Else{Q=p->Next; if(Q==null)return 0; E=q->data; P->next=q->Next;  Free(q); return 1; }}//main function. cpp#include"LinkBase.h"#include<stdio.h>voidReverse (linklist *&m) {linklist*p=l->next,*Q; L->next=NULL;  while(p!=NULL) {Q=p->Next; P->next=l->next;//Insert the *p node in front of the new linked listl->next=Q; P=Q; }}voidMain () {linklist*ls = (linklist *)malloc(sizeof(linklist)); Ls->next=NULL; intI=0;  for(i=1;i<Ten; i++) {Listinsert (ls,i,i*i-1); } printf ("original linear table: \ n");    Displist (Ls); //Listinsert (ls,3,15);Reverse (Ls); printf ("linear table after reversal: \ n");    Displist (Ls);  Free(Ls);}

The effect is as follows:

Diagram operation for single-linked list inversion

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.