约瑟夫环的简单实现
时间:2010-05-30 来源:随1意2o
#include <stdio.h>
#include <malloc.h>
void deletcreat(struct Node *p);
void print(struct Node *p);
typedef struct Node
{
int x,y;
struct Node *next;
}Node;
Node *creat(void) //Á´±íµÄ´´½¨
{
int i=1,j,n,m;
Node *h=NULL,*p,*q=NULL;
printf("ÊäÈëÈËÊýn\n");
scanf("%d",&n);
for(j=1;j<=n;j++)
{
p=(Node*)malloc(sizeof(Node));
p->x=i++;
printf("ÇëÊäÈëµÚ%d¸öÈ˵ÄÃÜÂë",i-1);
scanf("%d",&m);
p->y=m;
p->next=NULL;
if(h==NULL)
h=p;
else
q->next=p;
q=p;
}
p->next=h;
return h;
free(p);
free(q);
}
int main(){
Node *a,*L,*p;
int i=1,m;
a=creat();
printf("ÇëÊäÈëµÚÒ»¸öm");
scanf("%d",&m);
for(;a->next!=a;a=a->next)
{
L=a; i++;
p=L->next;
if(i==m)
{ m=p->y; i=1;
printf("ɾ³ýµÄÊǵÚ%d¸öÈË..ËûµÄÃÜÂëÊÇ%d\n",p->x,p->y);
L->next=p->next;
free(p);
}
if(i>m||m==1)
{ for(L=a;L->next!=a;L=L->next)
;
for(p=L;p->next!=L;p=p->next)
;
printf("ɾ³ýµÄÊǵÚ%d¸öÈË..ËûµÄÃÜÂëÊÇ%d\n",L->x,L->y);
p->next=L->next;
free(L);
}
}
printf("×îºóɾ³ýµÄÊǵÚ%d¸öÈË..ËûµÄÃÜÂëÊÇ%d\n",a->x,a->y);
}
#include <malloc.h>
void deletcreat(struct Node *p);
void print(struct Node *p);
typedef struct Node
{
int x,y;
struct Node *next;
}Node;
Node *creat(void) //Á´±íµÄ´´½¨
{
int i=1,j,n,m;
Node *h=NULL,*p,*q=NULL;
printf("ÊäÈëÈËÊýn\n");
scanf("%d",&n);
for(j=1;j<=n;j++)
{
p=(Node*)malloc(sizeof(Node));
p->x=i++;
printf("ÇëÊäÈëµÚ%d¸öÈ˵ÄÃÜÂë",i-1);
scanf("%d",&m);
p->y=m;
p->next=NULL;
if(h==NULL)
h=p;
else
q->next=p;
q=p;
}
p->next=h;
return h;
free(p);
free(q);
}
int main(){
Node *a,*L,*p;
int i=1,m;
a=creat();
printf("ÇëÊäÈëµÚÒ»¸öm");
scanf("%d",&m);
for(;a->next!=a;a=a->next)
{
L=a; i++;
p=L->next;
if(i==m)
{ m=p->y; i=1;
printf("ɾ³ýµÄÊǵÚ%d¸öÈË..ËûµÄÃÜÂëÊÇ%d\n",p->x,p->y);
L->next=p->next;
free(p);
}
if(i>m||m==1)
{ for(L=a;L->next!=a;L=L->next)
;
for(p=L;p->next!=L;p=p->next)
;
printf("ɾ³ýµÄÊǵÚ%d¸öÈË..ËûµÄÃÜÂëÊÇ%d\n",L->x,L->y);
p->next=L->next;
free(L);
}
}
printf("×îºóɾ³ýµÄÊǵÚ%d¸öÈË..ËûµÄÃÜÂëÊÇ%d\n",a->x,a->y);
}
相关阅读 更多 +