Linknode * createLink (int n) {int xValue; Linknode * head, * p, * pre; cout <"enter 1st digits:"; cin> xValue; p = (Linknode *) malloc (sizeof (Linknode); // p = new Linknode; p-> data = xValue; p-> next = NULL; head = p; pre = p; for (int I = 1; I <n; I ++) {cout <"Enter the number" <I + 1 <: "; cin> xValue; p = (Linknode *) malloc (sizeof (Linknode); p-> data = xValue; if (I = N-1) {/* cout <"already forms a ring! "<Endl; */p-> next = head-> next;} else {p-> next = NULL;} pre-> next = p; pre = p ;} // printLink (head); return head ;}
Linknode * findfirst1_enode (Linknode * head) {assert (head! = NULL & head-> next! = NULL); Linknode * pStart, * pCur; pCur = head; int nCur = 0, nStart; // check each node. If it is null, it exits, not a ring. While (pCur! = NULL) {pStart = head; nStart = 0; while (pStart! = NULL) {if (pStart = pCur) {if (nStart = nCur) {break;} else // locate the entrance to the ring. {Cout <"found, <nStart + 1 <" Node "<endl; return pStart ;}} nStart ++; pStart = pStart-> next;} nCur ++; pCur = pCur-> next;} cout <"This linked list is not a ring! "<Endl; return NULL ;}
Linknode * findfirst1_enode2 (Linknode * head) {assert (head! = NULL & head-> next! = NULL); set <Linknode *> addLinkNode; Linknode * pCur = head; while (pCur! = NULL) {if (addLinkNode. find (pCur) = addLinkNode. end () {addLinkNode. insert (pCur); pCur = pCur-> next;} else {cout <"this chain table is a ring! "<Endl; return pCur ;}} cout <" the Linked List is not a ring! "<Endl; return NULL ;}
Test code
Linknode * headNode; int n; cout <"Enter the length of the created linked list" <endl; cin> n; headNode = createLink (n ); // findfirstincluenode (headNode); findfirstincluenode2 (headNode );