#include
#include
#include
#define MAX 13 //圆桌围的最大人数
typedef struct Node{//定义节点
int inum;
struct Node *next;
}Node;
int _tmain(int argc, _TCHAR* argv[])
{
Node *pHead = (Node *)malloc(sizeof(Node));//申请头节点,这个节点是多余的,可以不用头节点
if(pHead == NULL)
{
printf("pHead Malloc Error!!");
exit(-1);
}
pHead->inum = MAX;
pHead->next = NULL;
for(int i = MAX; i > 0; i--)
{//为每个人申请一个节点,并围成一个圈
Node *pNode = (Node *)malloc(sizeof(Node));
if(pNode == NULL)
{
printf("pNode[%d] Malloc Error!!",i);
exit(-1);
}
pNode->next = pHead->next;
pHead->next = pNode;
pNode->inum = i;
}
Node *pNode;
for(pNode = pHead; pNode->next; pNode = pNode->next); //找最后一个节点
pNode->next = pHead;//把最后一个节点指向头节点
int icount = MAX;
pNode = pHead->next;//pNode为删除后的第一个节点,最开始指向第一个节点
Node *pPre = pHead;//pPre指向pNode的前一个节点
while(icount > 2)//直到找到剩两个节点时截止
{
for(int inum = 0; inum < 2; inum++)
{ //找到下一个要删除的节点
pPre = pNode;
pNode = pNode->next;
if(pNode == pHead)
{//如果是头指针,则跳过
pPre = pNode;
pNode = pNode->next;
}
}
pPre->next = pNode->next;
free(pNode);
pNode = pPre->next;
if(pNode == pHead)
{//如果是头指针,则跳过
pPre = pNode;
pNode = pNode->next;
}
icount--;
}
printf("%d,%d\n",pHead->next->inum,pHead->next->next->inum);
free(pHead);
_getch();
return 0;
}