Review the C-linked list operation (bidirectional loop linked list, find loop node)

Source: Internet
Author: User

Two-way circular linked list and one-way circular linked list find the loop node the idea is the same. Fast and Slow pointer lookup method. Theory can be referenced

C linked list of fast and slow pointer lookup loop node

typedefstructstudent_double{Charname[Ten]; intPoint ; structStudent_double *Prestu; structStudent_double *Nextstu;} studentdouble; Studentdouble*createdoublecirclelink_table () {inti =0; Studentdouble*head =NULL; Head= (studentdouble *) malloc (sizeof(studentdouble)); Head->name[0]=' /'; Head->point =0; Head->nextstu =NULL; Head->prestu =NULL; //Loop NodeStudentdouble *cirlestu =NULL; Studentdouble*temp =NULL; Studentdouble*currentnode =Head;  while(i<=9) {Temp= (studentdouble *) malloc (sizeof(studentdouble)); strncpy (Temp->name,"Node",sizeof(temp->name)); Temp->point =i; Temp->nextstu =NULL; Temp->prestu =CurrentNode; CurrentNode->nextstu =temp; CurrentNode=temp; if(i==3) {Cirlestu=CurrentNode; } I++; }    //finally merge loop nodescurrentnode->nextstu=Cirlestu; returnhead;}//known loop node condition query loop linked list, verify availabilityvoidSelectdoublelinktable (studentdouble *student) {studentdouble*next = student->Nextstu; inti =0; Studentdouble*circlestu =NULL;  while(next) {if(Circlestu!=null&&next->point = = circlestu->Point ) {printf ("loop node%d, end loop \ n",next->Point );  Break; }        if(i==3) {Circlestu=Next; } printf ("index%d; studentname is%s; Point is%d\n",i,next->name,next->Point ); I++; Next= next->Nextstu; }}//Unknown condition query loop nodeStudentdouble * selectcirclenodeindoublelinktable (studentdouble *head) {    //Fast and slow pointer queryStudentdouble *fast =Head; Studentdouble*slow =Head;  while(FAST) {fast= fast->nextstu->Nextstu; Slow= slow->Nextstu; if(Fast==null) {//not a circular link list launch             Break; }        if(Fast==slow) {//Fast and slow hands meet             Break; }    }    if(Fast = =NULL) {printf ("The linked list is not a circular link list \ n"); returnNULL; }        //Find loop NodesFast =Head;  while(fast!=slow) {Fast=fast->Nextstu; Slow=slow->Nextstu; } printf ("===== Find the cyclic link list loop node as%d\n",fast->Point ); returnFast;}intMainvoid){    Charsf[ the]; //create a two-way circular linked listStudentdouble *head =NULL; printf ("Creating a bidirectional circular link list y| n\n"); scanf ("%s", SF); if(STRCMP (SF,"Y")==0) {Head=createdoublecirclelink_table (); } printf ("known situation query loop linked list y| N. \ n"); scanf ("%s", SF); if(STRCMP (SF,"Y")==0) {selectdoublelinktable (head); } printf ("Unknown condition query loop linked list y| N. \ n"); scanf ("%s", SF); if(STRCMP (SF,"Y")==0) {selectcirclenodeindoublelinktable (head); }    return 0;}

Review the C-linked list operation (bidirectional loop linked list, find loop node)

Related Article

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.