#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
----------------解决方案--------------------------------------------------------
这是我用vc++ 写的刚刚写完 还没有优化,请大家帮帮忙怎么可以更好
我是个大菜鸟 呵呵 谢谢大家不吝赐教~~~~~
#include<stdio.h> #include<iostream.h> typedef struct poly { int data; struct poly *next; }polytype; polytype * create() { polytype *head,*p,*q; int m; int i=0; p=new polytype; if(!p) {head=NULL;return head;} cout<<"how many?\n"; cin>>m; cout<<"input the number~~~\n"; for(head=p;i<m;i++) { q=new polytype; cin>>q->data; p->next=q;p=q; } p->next=NULL; return head; } int getelem(polytype *La,int position) { polytype *p; int j=1,e; p=La->next; while(p&&j<position) {p=p->next;++j;} if(!p||j>position) {cout<<"error!!\n";return NULL;} e=p->data; return e; } polytype * list_insert(polytype *La,int i,int elem) { polytype *p,*head; p=La; head=p; int j=0; while(p&&j<i-1) {p=p->next;j++;} if(!p||j>i-1) {cout<<"error\n";return NULL;} polytype *n=new polytype; n->data=elem;n->next=p->next;p->next=n; return head; } polytype * list_delete(polytype *La,int i) { polytype *head=La; polytype *p=La; int j=0; while(p->next&&j<i-1) {p=p->next;++j;} if(!(p->next)||j>i-1) {cout<<"error\n";return NULL;} polytype *q=p->next;p->next=q->next; cout<<"the deleted data is"<<q->data<<"\n"; delete q; return head; } void print(polytype *La) { polytype *p; p=La->next; while(p) { cout<<" "<<p->data; p=p->next; } } main() { int p1,p2,p3,i; polytype *La=create(); print(La); cout<<"\nget which one?"; cin>>p1; cout<<getelem(La,p1); cout<<"insert element ? before ?'th\n"; cin>>i>>p2; polytype *Lb=list_insert(La,p2,i); print(Lb); cout<<"delete ?'th member\n"; cin>>p3; polytype *Lc=list_delete(La,p3); print(Lc); return 1; }
----------------解决方案--------------------------------------------------------