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)