#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct _body
{
int Number;
struct _body *pre;
struct _body *next;
};
typedef struct _body body;
void operate( void *);
void operate( void *h)
{
body *leap = (body*)h;
int i;
for(i=1;;i++)
{
if( leap==leap->next) break;
if(i%3==0)
{
(leap->pre)->next= leap->next;
(leap->next)->pre = leap->pre;
}
leap=leap->next;
}
printf("the last nubmer is:%d\n",leap->Number);
}
int main( void )
{
void *head;
body *leap;
printf("Please input the number n:");
int n;
scanf("%d", &n);
head = malloc( sizeof(body) * n );
if(head==NULL)
{
perror( "mem:");
exit(1);
}
leap=(body*)head;
int i;
for(i=1;i<=n;i++)
{
leap->Number = i;
if( i == n)
{
leap->next=(body*)head;
}else
{
leap->next = leap + 1;
}
leap = leap->next;
}
for(i=1;i<=n;i++)
{
if( i == 1)
{
leap->pre=(body*)head+n-1;//赋值成最后一个数据地址
}else
{
leap->pre = leap - 1;
}
leap = leap->pre;
}
operate(head);
free(head);
leap=head=NULL;
return 0;
}