#include<stdio.h> #include<stdlib.h> #define MAXNUM 100 #define FALSE 0 #define TRUE 1 typedef int DataType; #define SPECIAL 2147483647 struct SeqList { DataType element[MAXNUM]; int n; }; typedef struct SeqList SeqList, *PSeqList;
PSeqList createNullList_seq( void ) /* 建立一个顺序表 */ { PSeqList palist; palist=(PSeqList)malloc(sizeof(struct SeqList)); if (palist!=NULL) palist ->n = 0; else printf("Out of space!!\n"); return palist; }; int insert_seq( PSeqList palist, DataType x, int p ) /* 插入一个结点 */ { int q; if ( palist->n == MAXNUM ) { printf("Overflow!\n"); return ( FALSE ); } if ( p<0 || p>palist->n ) { printf("not exist! \n"); return ( FALSE ); } for(q=palist->n - 1; q>=p; q--) palist->element[q+1] = palist->element[q]; palist->element[p] = x; palist->n = palist->n + 1; return ( TRUE ); } PSeqList delete_seq( PSeqList palist, int p ) /* 在palist所指顺序表中删除下标为p的元素 */ { int q; if ( p<0 || p>palist->n) { printf("not exist!\n "); return (FALSE); } for(q=p; q<palist->n-1; q++) palist->element[q] = palist->element[q+1]; palist->n = palist->n - 1; return ( palist ); }; DataType retrieve_seq( PSeqList palist, int p ) /* 求palist所指顺序表中第p个(即下标为p-1) 的元素的值 */ { if ( p>0 && p<=palist->n ) /* 存在下标为 p-1的元素 */ return ( palist->element[p-1] ); printf("Not exist.\n "); return SPECIAL; /* 返回一个顺序表中没有 的特殊元素值 */ }; void inputnsm(int* n,int* s,int* m) { printf("\n please input the values(<100) of n = "); scanf("%d",n); printf("\n please input the values of s = "); scanf("%d",s); printf("\n please input the values of m = "); scanf("%d",m); } void initlist(PSeqList jos_alist,int n) { int i,k; if (jos_alist==NULL) exit(1); for( i = 0; i < n; i++ ) { k=insert_seq( jos_alist, i, i+1); if (k==FALSE) exit(1); } } void josephus_seq( PSeqList palist, int s, int m) { int s1, i, w; s1 = s - 1; /* 找出列的元素 */ for(i = palist->n; i>0; i--) { s1 = ( s1 + m - 1 ) % i ; w = retrieve_seq(palist,s1); /* 求下标为s1的元素的值 */ printf("Out element %d \n",w); /* 元素出列 */ delete_seq(palist,s1); /* 删除出列的元素 */ getch(); } } main( ) {
PSeqList jos_alist; int n,s,m; inputnsm(&n,&s,&m); jos_alist=createNullList_seq( ); /* 创建空顺序表 */ initlist(jos_alist,n); josephus_seq(jos_alist,s,m); free(jos_alist);
getch(); }
这是我的程序
没有返回值a