Give a list, first determine whether the linked list with a ring, if the band ring, to find the entrance of the ring.
Determine if you have a ring: use a fast or slow pointer. Fast pointer every two steps, slow hands step, if the two at a point in the phase
The chain strap ring.
The implementation code for the function is given below:
typedef struct LINKNODE{DATATYPE data;struct Linknode *next;} linknode,*plinknode;typedef struct Linklist{linknode *phead;} Linklist,*plinklist;plinknode iscircle (plinklist plist) {assert (plist); if (null = = Plist->phead) {printf ("the list is empty \ n") ; return NULL;} Plinknode fast = Plist->phead;plinknode slow = plist->phead;while (Fast && fast->next) {fast = Fast->n Ext->next;slow = slow->next;if (fast = = slow) return fast;} return NULL;}
If
If the chain strap rings, look at the following diagram:
Code:
Plinknode Firstcrossnode (plinklist plist) {assert (plist), if (NULL = = Plist->phead) {printf ("list is empty \ n"); return NULL;} Plinknode ret = iscircle (plist), if (ret = = NULL) {printf ("list without ring \ n"); return NULL;} Plinknode fast = Plist->phead;plinknode slow = ret;while (fast) {fast = Fast->next;slow = slow->next;if (Fast = = s) Low) return fast;}}
Determine if a linked list has a ring, and the entry of the ring