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