关于链表
#include<iostream.h>
#include<iomanip.h>
#include<stdio.h>
struct jose
{
int code;
jose* next;
};
void main()
{
int numOfBoys,interval;
cout<<"please input the number of boys,\n"
<<" interval of counting :\n";
cin>>numOfBoys>>interval;
jose* pJose=new jose[numOfBoys];
jose* pCurrent=pJose;
int itemsInLine=0;
for(int i=1;i<=numOfBoys;i++)
{
pCurrent->next=pJose+i%numOfBoys;
pCurrent->code=i;
pCurrent=pCurrent->next;
if(itemsInLine++ % 10 == 0)
cout<<endl;
cout<<setw(4)<<i;
}
itemsInLine=0;
jose* pivot;
pCurrent=&pJose[numOfBoys-1];
while(pCurrent->next!=pCurrent)
{
for(int j=0;j<interval;j++) //如果interval=3,那pCurrent只是是第几结点
{
pivot=pCurrent;
pCurrent = pivot->next;
}
if(itemsInLine++ % 10 == 0)
cout<<endl;
cout<<setw(4)<<pCurrent->code;
pivot->next=pCurrent->next;
pCurrent=pivot;
}
cout<<"\n\nthe winner is"
<<pCurrent->code<<endl;
delete[]pJose;
getchar();
}