- #include<iostream>
- using namespace std;
- struct person
- {
- int num;
- struct person *next;
- };
- int n=0;//n表示节点数。
- struct person *creat()//创建链表函数creat,返回头指针。//
- {
- person *p1,*p2,*head;
- int num1,num2=1;
- head=NULL;
- cout<<"please input the num of people m:(m>1) :";
- cin>>num1;
- p1=new(person);
- p1->num=num2;
- p2=p1;
- while(num1--)
- {
- n++;
- if(n==1)//如果是第一个节点。
- {head=p1;}
- else//假如不是第一个节点。
- {p2->next=p1;}
- p2=p1;
- p1=new(person);
- p1->num=++num2;
- }
- delete(p1);
- p2->next=head;
- return(head);
- }
- void main()
- {
- struct person *head;
- head=creat();
- int k,b,i;
- cout<<"输入开始报名人的编号k和报数值b:"<<endl;//报名人的姓名是用数字来代替的。
- cin>>k>>b;
- int b1=b-1;
- struct person *p,*q;
- p=head;
- while(p->num!=k)//找编号为k的节点。
- {p=p->next;}//循环结束后指针p指向k。
- if(1==b)
- { while(n--)
- {
- cout<<p->num<<"\\t";
- p=p->next;
- }
- }
- else
- {
- for(i=1;i<b1;i++)
- p=p->next;
- q=p->next;
- cout<<q->num<<"\\t";//第一次是从k开始报数。
- p->next=q->next;//删除报b的节点。
- while(--n)
- {
- p=p->next;
- for(i=1;i<b1;i++)
- {p=p->next;}//指针p指向从k开始报b的前一个节点。
- q=p->next;//q为报b的节点。
- cout<<q->num<<"\\t";
- p->next=q->next;//删除报b的节点。
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2608201410324.html
来源: http://www.codesnippet.cn/detail/2608201410324.html