Determine whether a single-chain table is a ring and find the combination of the ring

Source: Internet
Author: User

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 );


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.