#include <iostream>
using namespace std;
typedef struct tagNode
{
tagNode *pNext;
int data;
}Node;
//创建有n个结点的循环链表
Node *CreateLoopList(int n)
{
Node *head = NULL;
Node *tail = NULL;
Node *temp = NULL;
//创建头结点
head = tail = new Node;
head->pNext = NULL;
head->data = 0;
for (int i = 1; i < n; ++i)
{
temp = new Node;
temp->pNext = NULL;
temp->data = i;
tail->pNext = temp;
tail = temp;
}
tail->pNext = head;
return head;
}
//返回最后一个元素(从当前位置开始数,第num个出列,依次类推)
int LastNumber(Node *head, int num)
{
Node *pPre = head;
Node *pCur = head;
int n = 1;
while (pCur->pNext != pCur)
{
while (n < num)
{
pPre = pCur;
pCur = pCur->pNext;
++n;
}
n = 1;
pPre->pNext = pCur->pNext;
delete pCur;
pCur = pPre->pNext;
}
return pCur->data;
}
int main()
{
Node *nd = new Node;
nd = CreateLoopList(3);
int n = LastNumber(nd, 3);
cout<<n<<endl;
return 1;
}