DataTypeelement[MAXNUM];/*存放线性表中的元素*/
};
typedefstructSeqListSeqList,*PSeqList;
/*创建新的顺序表*/
PSeqListcreateNullList_seq(void);
/*判断顺序表是否为空*/
intisNullList_seq(PSeqListpalist);
/*在palist所指顺序表中下标为p的元素之前插入元素x*/
intinsert_seq(PSeqListpalist,intp,DataTypex);
/*在palist所指顺序表中删除下标为p的元素*/
intdelete_seq(PSeqListpalist,intp);
/*求x在palist所指顺序表中的下标*/
intlocate_seq(PSeqListpalist,DataTypex);
/*求palist所指顺序表中下标为p的元素值*/
DataTyperetrieve_seq(PSeqListpalist,intp);
/*线性表的顺序表示:
函数实现*/
#include
#include
#include"slist.h"
PSeqListcreateNullList_seq(void){
PSeqListpalist=(PSeqList)malloc(sizeof(structSeqList));
if(palist!
=NULL)
palist->n=0;/*空表长度为0*/
else
printf("Outofspace!
\n");/*存储分配失败*/
returnpalist;
}
/*在palist所指顺序表中下标为p的元素之前插入元素x*/
intinsert_seq(PSeqListpalist,intp,DataTypex){
intq;
if(palist->n==MAXNUM){/*溢出*/
printf("Seq-listoverflow!
\n");
returnFALSE;
}
if(p<0||p>palist->n){/*不存在下标为p的元素*/
printf("Indexofseq-listisoutofrange!
\n");
returnFALSE;
}
for(q=palist->n-1;q>=p;q--)/*插入位置及之后的元素均后移一个位置*/
palist->element[q+1]=palist->element[q];
palist->element[p]=x;/*插入元素x*/
palist->n++;/*元素个数加1*/
returnTRUE;
}
/*在palist所指顺序表中删除下标为p的元素*/
intdelete_seq(PSeqListpalist,intp){
intq;
if(p<0||p>palist->n-1){/*不存在下标为p的元素*/
printf("Indexofseq-listisoutofrange!
\n");
returnFALSE;
}
for(q=p;qn-1;q++)/*被删除元素之后的元素均前移一个位置*/
palist->element[q]=palist->element[q+1];
palist->n--;/*元素个数减1*/
returnTRUE;
}
/*求x在palist所指顺序表中的下标*/
intlocate_seq(PSeqListpalist,DataTypex){
intq;
for(q=0;qn;q++)
if(palist->element[q]==x)returnq;
return-1;
}
/*求palist所指顺序表中下标为p的元素值*/
DataTyperetrieve_seq(PSeqListpalist,intp){
if(p>=0&&pn)/*存在下标为p的元素*/
returnpalist->element[p];
printf("Indexofseq-listisoutofrange.\n");
returnSPECIAL;/*返回一个顺序表中没有的特殊值*/
}
intisNullList_seq(PSeqListpalist){
returnpalist->n==0;
}
/*线性表的单链表表示:
类型和界面函数定义*/
/*定义链接表元素类型。
应根据需要定义*/
typedefintDataType;
structNode;/*单链表结点类型*/
typedefstructNode*PNode;/*结点指针类型*/
typedefstructNode*LinkList;/*单链表类型*/
structNode{/*单链表结点结构*/
DataTypeinfo;
PNodelink;
};
/*创建一个带头结点的空链表*/
LinkListcreateNullList_link(void);
/*在llist带头结点的单链表中下标为i的(第i+1个)结点前插入元素x*/
intinsert_link(LinkListllist,inti,DataTypex);
/*在llist带有头结点的单链表中删除第一个值为x的结点*/
intdelete_link(LinkListllist,DataTypex);
/*在llist带有头结点的单链表中找第一个值为x的结点存储位置*/
PNodelocate_link(LinkListllist,DataTypex);
/*在带有头结点的单链表llist中求下标为i的(第i+1个)结点的存储位置*/
/*当表中无下标为i的(第i+1个)元素时,返回值为NULL*/
PNodefind_link(LinkListllist,inti);
/*判断llist带有头结点的单链表是否是空链表*/
intisNullList_link(LinkListllist)
/*线性表的单链表表示:
函数实现*/
#include
#include
#include"llist.h"
/*创建一个带头结点的空链表*/
LinkListcreateNullList_link(void){
LinkListllist;
llist=(LinkList)malloc(sizeof(structNode));/*申请表头结点空间*/
if(llist!
=NULL)llist->link=NULL;
returnllist;
}
/*在llist带头结点的单链表中下标为i的(第i+1个)结点前插入元素x*/
intinsert_link(LinkListllist,inti,DataTypex){
PNodep=llist,q;
intj;
for(j=0;p!
=NULL&&j
p=p->link;
if(j!
=i){/*i<1或者大于表长*/
printf("Indexoflink-listisoutofrange.\n",i);
return0;
}
q=(PNode)malloc(sizeof(structNode));/*申请新结点*/
if(q==NULL){
printf("Outofspace!
\n");
return0;
}
/*插入链表中*/
q->info=x;
q->link=p->link;
p->link=q;/*注意该句必须在上句后执行*/
return1;
}
/*在llist带有头结点的单链表中删除第一个值为x的结点*/
/*这时要求DataType可以用!
=比较*/
intdelete_link(LinkListllist,DataTypex){
PNodep=llist,q;
/*找值为x的结点的前驱结点的存储位置*/
while(p->link!
=NULL&&p->link->info!
=x)
p=p->link;
if(p->link==NULL){/*没找到值为x的结点*/
printf("Datumdoesnotexist!
\n");
return0;
}
q=p->link;/*找到值为x的结点*/
p->link=p->link->link;/*删除该结点*/
free(q);
return1;
}
/*在llist带有头结点的单链表中找第一个值为x的结点存储位置*/
/*找不到时返回空指针*/
PNodelocate_link(LinkListllist,DataTypex){
PNodep;
if(llist==NULL)returnNULL;
for(p=llist->link;p!
=NULL&&p->info!
=x;)
p=p->link;
returnp;
}
/*在带有头结点的单链表llist中下标为i的(第i+1个)结点的存储位置*/
/*当表中无下标为i的(第i+1个)元素时,返回值为NULL*/
PNodefind_link(LinkListllist,inti){
PNodep;
intj;
if(i<0){/*检查i的值*/
printf("Indexoflink-listisoutofrange.\n",i);
returnNULL;
}
for(p=llist->link,j=0;p!
=NULL&&j
p=p->link;
if(p==NULL)
printf("Indexoflink-listisoutofrange.\n",i);
returnp;
}
/*判断llist带有头结点的单链表是否是空链表*/
intisNullList_link(LinkListllist){
returnllist==NULL||llist->link==NULL;
}
/*线性表的顺序表示:
类型和界面定义*/
#defineTRUE1
#defineFALSE0
#defineSPECIAL-1
/*定义顺序表的大小。
应根据需要修改*/
enum{NBASE=20};
/*定义顺序表的元素类型。
应根据需要修改*/
typedefintDataType;
structSeqList{
intn,nmax;/*当前元素个数n,存储区大小nmax*/
DataType*elems;/*存放线性表中的元素*/
};
typedefstructSeqList*PSeqList;
/*创建新的顺序表*/
PSeqListcreateNullList_seq(void);
/*判断顺序表是否为空*/
intisNullList_seq(PSeqListpalist);
/*在palist所指顺序表中下标为p的元素之前插入元素x*/
intinsert_seq(PSeqListpalist,intp,DataTypex);
/*在palist所指顺序表中删除下标为p的元素*/
intdelete_seq(PSeqListpalist,intp);
/*求x在palist所指顺序表中的下标*/
intlocate_seq(PSeqListpalist,DataTypex);
/*求palist所指顺序表中下标为p的元素值*/
DataTyperetrieve_seq(PSeqListpalist,intp);
/*线性表的顺序表示:
函数实现*/
#include
#include
#include"dslist.h"
PSeqListcreateNullList_seq(void){
PSeqListlp=(PSeqList)malloc(sizeof(structSeqList));
if(lp!
=NULL){
lp->elems=(DataType*)malloc(sizeof(DataType)*NBASE);
if(lp->elems){
lp->n=0;/*空表长度为0*/
lp->nmax=NBASE;
returnlp;
}
elsefree(lp);
}
printf("Outofspace!
\n");/*存储分配失败*/
returnNULL;
}
/*在palist所指顺序表中下标为p的元素之前插入元素x*/
intinsert_seq(PSeqListlp,intp,DataTypex){
intq;
if(p<0||p>lp->n){/*不存在下标为p的元素*/
printf("Indexofseq-listisoutofrange!
\n");
returnFALSE;
}
if(lp->n==lp->nmax){/*存储区满*/
DataType*dp=/*存储区扩大一倍*/
(DataType*)realloc(lp->elems,lp->nmax*2*sizeof(DataType));
if(dp==NULL){/*空间耗尽,原来元素还在原处*/
printf("Seq-listoverflow!
\n");
returnFALSE;
}
lp->elems=dp;
lp->nmax*=2;
}
for(q=lp->n-1;q>=p;q--)/*插入位置及之后的元素均后移一个位置*/
lp->elems[q+1]=lp->elems[q];
lp->elems[p]=x;/*插入元素x*/
lp->n++;/*元素个数加1*/
returnTRUE;
}
/*在lp所指顺序表中删除下标为p的元素*/
intdelete_seq(PSeqListlp,intp){
intq;
if(p<0||p>lp->n-1){/*不存在下标为p的元素*/
printf("Indexofseq-listisoutofrange!
\n");
returnFALSE;
}
for(q=p;qn-1;q++)/*被删除元素之后的元素均前移一个位置*/
lp->elems[q]=lp->elems[q+1];
lp->n--;/*元素个数减1*/
returnTRUE;
}
/*求x在lp所指顺序表中的下标*/
intlocate_seq(PSeqListlp,DataTypex){
intq;
for(q=0;qn;q++)
if(lp->elems[q]==x)returnq;
return-1;
}
/*求lp所指顺序表中下标为p的元素值*/
DataTyperetrieve_seq(PSeqListlp,intp){
if(p>=0&&pn)/*存在下标为p的元素*/
returnlp->elems[p];
printf("Indexofseq-listisoutofrange.\n");
returnSPECIAL;/*返回一个顺序表中没有的特殊值*/
}
intisNullList_seq(PSeqListlp){
returnlp->n==0;
}
/*用顺序表解决josephus问题的算法*/
#include
#include
#defineMAXNUM100
#defineFALSE0
#defineTRUE1
typedefintDataType;
structSeqList{
intn;/*存放线性表中元素的个数nDataTypeelement[MAXNUM];/*存放线性表中的元素*/
};
typedefstructSeqList*PSeqList;
PSeqListcreateNullList_seq(void){
PSeqListpalist=(PSeqList)malloc(sizeof(structSeqList));
if(palist!
=NULL)
palist->n=0;/*空表长度为0*/
else
printf("Outofspace!
!
\n");/*存储分配失败*/
returnpalist;
}
/*在palist所指顺序表中下标为p的元素之前插入元素x*/
intinsert_seq(PSeqListpalist,intp,DataTypex){
intq;
if(palist->n==MAXNUM){/*溢出*/
printf("Seq-listoverflow!
\n");
returnFALSE;
}
if(p<0||p>palist->n){/*不存在下标p*/
printf("Indexoutofrange!
\n");
returnFALSE;
}
for(q=palist->n-1;q>=p;q--)/*插入位置及之后的元素均后移一个位置*/
palist->element[q+1]=palist->element[q];
palist->element[p]=x;/*插入元素x*/
palist->n++;/*元素个数加1*/
returnTRUE;
}
/*在palist所指顺序表中删除下标为p的元素*/
intdelete_seq(PSeqListpalist,intp){
intq;
if(p<0||p>palist->n){/*不存在下标p*/
printf("Indexoutofrange!
\n");
returnFALSE;
}
for(q=p;qn-1;q++)/*被删除元素之后的元素均前移一个位置*/
palist->element[q]=palist->element[q+1];
palist->n--;/*元素个数减1*/
returnTRUE;
}
/*求palist所指顺序表中下标为p的元素值*/
DataTyperetrieve_seq(PSeqListpalist,intp){
if(p>=0&&pn)/*存在下标为p的元素*/
returnpalist->element[p];
printf("Indexoutofrange!
\n");
return-1;/*返回一个顺序表中没有的特殊值*/
}
intinit_jlist(PSeqListslist,intn){
inti,k;
if(slist==NULL)returnFALSE;
if(n<1||n>MAXNUM){
printf("Numberofelementsisoutofrange!
\n");
returnFALSE;
}
for(i=0;ik=insert_seq(slist,i,i+1);
}
voidjosephus_seq(intn,ints,intm){
ints1,i,w;
PSeqListjlist;
s1=s-1;
jlist=createNullList_seq();/*创建空顺序表*/
if(jlist==NULL)return;
if(init_jlist(jlist,n)==FALSE)return;
/*找出列的元素*/
for(i=jlist->n;i>0;i--){
s1=(s1+m-1)%i;
w=retriev