#include <stdio.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
typedef int Datatype;
struct Node;
typedef struct Node*PNode;
struct Node{
Datatype info;
PNode link;
};
typedef struct Node*LinkList;
typedef LinkList*PLinkList;
typedef int DataType;
typedef struct seq
{
int Maxnum;
int n;
int *element;
}*PSeqList;
PSeqList palist;
/*创建空表*/
create_seq(int m)
{
palist=(PSeqList)malloc(sizeof(struct seq));
if(palist!=NULL)
{
palist->element=(int *)malloc(sizeof(int)*m);
if(palist->element!=NULL)
{
palist->Maxnum=m;
palist->n=0;
}
else free(palist);
}
else
printf("Out of space!!!\n");
}
/*插入函数*/
insert_seq(int p,int x)
{
int q;
if(palist->n>=palist->Maxnum)
{ printf("Overflow!!\n");
exit(0);
}
if(p<0||p>palist->n)
{ printf("The location is not exist,illegal!\n");
exit(0);
}
for(q=palist->n-1;q>=p;q--)
{ palist->element[q+1]=palist->element[q];
} //ºóÒÆ
palist->element[p]=x;
palist->n=palist->n+1;
}
/*删除函数*/
int deleteP_seq(int p)
{
int q;
int x;
if(p<0||p>palist->n-1)
printf("NOT exist!\n");
else
{
x=palist->element[p];
for(q=p;q<palist->n-1;q++)
palist->element[q]=palist->element[q+1]; //Ç°ÒÆ
palist->n=palist->n-1;
}
return x;
}
int locate_seq(int x)
{
int q;
for(q = 0;q < palist ->n;q++)
{
if(palist ->element[q] == x)
{
return q;
}
}
return -1;
}
void josephus_seq(int s,int m)
{
int s1,i,w;
s1 = s - 1;
for(i = palist->n;i>0;i--)
{
s1 = (s1 + m - 1) % i;
w = palist ->element[s1];
printf("Out element %d \n",w);
deleteP_seq(s1);
}
}
int jos_Link()
{
PSeqList jos_alist;
int i,k;
int n,s,m;
printf("please input the values(<100) of n= \n");
scanf("%d",&n);
printf("please input the values of s = \n");
scanf("%d",&s);
printf("please input the values of m = \n");
scanf("%d",&m);
jos_alist = create_seq(n);
if(jos_alist!=NULL)
{
for(i = 0;i < n;i++)
insert_seq(i,i+1);
josephus_seq(s,m);
free(jos_alist->element);
free(jos_alist);
}
}
int init_clist(PLinkList pclist,int n)
/* 用1,2...,n为*pclist所示的循环表初始化*/
{
PNode p,q;
int i;
q = (PNode)malloc(sizeof(struct Node));
if(q== NULL)return FALSE;
*pclist = q;
q->info=1;
q->link=q;
if(n==1)return TRUE;
for(i = 2;i<n+1;i++)
{
p = (PNode)malloc(sizeof(struct Node));
if(p==NULL)return FALSE;
p->info = i;
p->link= q->link;
q->link=p;
q=p;
}
return TRUE;
}
void josephus_clist(PLinkList pclist,int s,int m)
{
PNode p,pre;
int i;
p = *pclist;
/*找第s个元素*/
if(s==1)
{
pre = p;
p = p->link;
while(p!= *pclist)
{
pre = p;
p = p->link;
}
}
else for(i=1;i<s;i++)
{
pre = p;
p = p->link;
}
while(p!=p->link)
{
for(i=1;i<m;i++)
{
pre = p;
p = p->link;
}
printf("out element:%d\n",p->info);
if(*pclist == p)
*pclist = p->link;
pre ->link = p->link;
free(p);
p=pre->link;
}
printf("out element:%d\n",p->info);
*pclist = NULL;
free(p);
}
int jos_Seq()
{
LinkList jos_clist;
int n,s,m;
do{
printf("\n please input the values of n=");
scanf("%d",&n);
}while(n<1);
do{
printf("\n please input the values of s=");
scanf("%d",&s);
}while(s<1);
do{
printf("\n please input the values of m=");
scanf("%d",&m);
}while(m<1);
if(init_clist(&jos_clist,n))
josephus_clist(&jos_clist,s,m);
else
printf("Out of space! \n");
}
int main()
{
int c,m,n,i,x,p;
int xx;
while(1)
{
printf("\n---------------");
printf("\n1------jos_Link");
printf("\n2------jos_Seq");
printf("\n0------Exit");
printf("\n---------------");
printf("\nPlease choose menu(0-2):");
scanf("%d",&c);
switch(c)
{
case 1: {
jos_Link();
break;
}
case 2: { jos_Seq();
break;
}
case 0:return 0;
defalult: printf("\nYour choice number is error!!!");
}
}
}
还有相同密码和不同密码的两种约瑟夫环,欢迎练习
qq:1351006594