Problems with the C-language circular list

Source: Internet
Author: User

Today, the teacher said a question, the main idea is that there are a group of children 10 people, but the teacher only one apple, can only give a child, so the teacher decided to let the children make a circle, from the first children start, every other children have no chance to get apples, the last one can get, Now the teacher wants to know who the lucky man will be.

Then the teacher asked for the problem with a circular list to solve, oneself also incidentally review, directly on the code

structdata{intnum; structData *next;};intMain () {structData *p,*head,*pnew; intn=2; P=head= (structdata*) malloc (sizeof(structdata));//Create a head pointer that is stored as the first datap->num=1;  while(n<=Ten)//Loop to create the next linked list{pnew=(structData *) malloc (sizeof(structdata)); Pnew->num=N; P->next=pnew; P=pnew; N++; } P->next=head;//let the last tail pointer point to the head pointer to form a circular linked listP=head;//P point to the head pointer, ready to traverse the linked list     while(1) {p->next=p->next->next;//according to the conditions, one out of two people had no chance to get candyp=p->next;//to the next person's next man.            if(p->next==p)//Loop list, when there is only one node left, it will point to itself                 Break; } printf ("%d\n", P->num);//output The number of this node and see which one is lucky .}

Look directly at the code, come on! No matter what others think, as long as insist is the best self.

Problems with the C-language circular list

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.