约瑟夫环问题 编译正确 无法执行..............
#include <stdio.h>#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OVERFLOW -2
typedef int Elemtype;
typedef int Status;
typedef struct
{
Elemtype * elem;
int length;
int listsize;
} SqList; //线性表的顺序表示
Status InitList(SqList &L)
{
//构造一个空的线性表L
//填空
L.elem=(int *) malloc (LIST_INIT_SIZE *sizeof(int));
if(!L.elem) exit(OVERFLOW);
L.length =0;
L.listsize=LIST_INIT_SIZE;
return OK;
}//InitList
int ListLength(SqList L)
{
//线性表已经存在,返回L中数据元素的个数
//填空
if(L.length ==0)
return ERROR;
else
return L.length;
}//ListLength
Status ListEmpty(SqList L)
{
//线性表已经存在,若L为空表,则返回TRUE,否则返回FALSE
//填空
if(L.length ==0)
return TRUE;
else
return FALSE;
}
Status ListInsert(SqList &L, int i, Elemtype e)
{
//线性表L已存在,1<=i<=ListLength(L)+1
//在L中第i个位置之前插入新的数据元素e,L的长度加1
//填空
int *newbase;
int *p,*q;
if(i<=1 ||i>L.length+1) return ERROR;
if(L.length>=L.listsize)
{newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!newbase) exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
}//ListInsert
Status ListDelete(SqList &L, int i, Elemtype &e)
{
//线性表L已存在且非空,1<=i<=ListLength(L)
//删除L的第i个位置的数据元素,并用e返回其值,L的长度减1
//填空
int *p,*q;
if(i<=1 ||i>L.length+1) return ERROR;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L.length;
return OK;
}//ListDelete
void main()
{
Status S;
SqList L;
int n, m;
int i;
int current;
Elemtype e;
S = InitList(L);
printf("输入n m\n");
scanf("%d %d",&n,&m);//输入个数和步长
if ((n>L.listsize) || (m<1)) //对输入值合法验证
{
printf("error");
return;
}
for (i=1; i<=n; i++) //初始化
S = ListInsert(L, ListLength(L)+1, i);
current = 1;
while (!ListEmpty(L))
{
//按照当前位置和步长计算下一次位置
current = ((current - 1) + m) % L.length;
if (current == 0) current = L.length;
//根据计算结果,current位置的元素出序列
S = ListDelete(L, current, e);
printf("%d\t", e);//输出值
}
printf("\n");
}