📄 约瑟夫环问题_数据结构与算法_数据结构算法_c语言_c 语言之家.htm
字号:
width=20> </TD>
<TD background="约瑟夫环问题_数据结构与算法_数据结构算法_C语言_C 语言之家.files/banbg.gif"
height=20 width=530>当前位置:<A class=class
href="http://www.cstudyhome.com/wenzhang06/">网站首页</A>>><A
class=class
href="http://www.cstudyhome.com/wenzhang06/type.asp?typeid=11">C语言</A>>><A
class=class
href="http://www.cstudyhome.com/wenzhang06/BigClass.asp?typeid=11&BigClassid=33">数据结构算法</A>>><A
class=class
href="http://www.cstudyhome.com/wenzhang06/SmallClass.asp?typeid=11&BigClassID=33&SmallClassID=60">数据结构与算法</A></TD>
<TD background="约瑟夫环问题_数据结构与算法_数据结构算法_C语言_C 语言之家.files/banbg.gif"
height=20 width=107>双击自动滚屏</TD>
<TD background="约瑟夫环问题_数据结构与算法_数据结构算法_C语言_C 语言之家.files/banbg.gif"
width=91><INPUT name=close onclick="window.close();return false;" type=button value=关闭窗口>
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE align=center border=3 borderColor=#e2ca9f cellPadding=0 cellSpacing=0
style="BORDER-COLLAPSE: collapse" width=750>
<TBODY>
<TR><!--<td width="20%" align="middle" valign="top" background="images/002.jpg" bordercolor="#e2ca9f"> </td>
<td width="80%">-->
<TD width="100%">
<TABLE border=0 borderColor=#e2ca9f cellPadding=0 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD align=middle vAlign=top width="95%">
<TABLE border=1 borderColor=#e2ca9f cellPadding=0 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD align=middle
background="约瑟夫环问题_数据结构与算法_数据结构算法_C语言_C 语言之家.files/002.jpg"
borderColor=#e2ca9f vAlign=top width="69%">
<TABLE align=center border=0 cellPadding=0 cellSpacing=0
width="100%">
<TBODY>
<TR>
<TD height=40 width="100%"></TD></TR>
<TR>
<TD>
<FORM action=Readnews.asp?newsid=4604&id2=4604
method=post name=form1>
<CENTER><!-- <input type=submit name=aa value="点击关闭浮动图标" width=20 title="点击广告支持本站">--></CENTER></FORM></TD></TR>
<TR>
<TD align=middle bgColor=#dddddd height=20
style="FONT-SIZE: 18px" vAlign=bottom
width="85%"><STRONG><FONT color=#003399 size=4><B>约瑟夫环问题
</B></FONT></STRONG></TD><BR></TR>
<TR>
<TD align=middle width="100%"><BR></TD></TR>
<TR>
<TD align=middle style="FONT-SIZE: 9pt"
width="100%">发表日期:2004年10月26日 作者:yuyulin 已经有1332位读者读过此文</TD></TR>
<TR>
<TD align=middle width="100%"><!--下面的这一句是设置阅读文本区的宽度-->
<TABLE align=center border=0 cellPadding=0 cellSpacing=0
style="TABLE-LAYOUT: fixed" width="90%">
<TBODY>
<TR>
<TD align=middle width="100%"></TD></TR>
<TR>
<TD style="WORD-WRAP: break-word"><FONT
class=news><BR>
<P> <BR>模 块 划
分<BR>(1)带头结点的单循环链表抽象数据类型sclinlist,其中包括的基本操作函数有:初始化操作函数,插入一个结点操作函数,删除一个几结点操作函数,取一个结点数据操作函数和判表是否为非空操作函数。<BR>(2)
void sclldeleteafter(sclnode
*p),其功能是删除带头结点的单循环链表中指针p所指结点的下一个结点。<BR>(3) void
jesephring(sclnode *head,int
m),其功能是对带头结点的单循环链表head,<BR>以m为初始报数上限值实现问题要求。<BR>(4)
void
main(),主函数,其功能是给出测试数据值,建立测试数据值的带头结点单循环链表,调用jesering()函数实现问题要求。<BR>#include<stdio.h><BR>#include<stdlib.h><BR>#define
null 0<BR>//数据类型datatype定义如下:<BR>typedef
struct<BR>{<BR> int number;<BR> int
cipher;<BR> }datatype;<BR> </P>
<P>//带头结点单循环链表结点的结构体定义如下:<BR>typedef struct
node<BR>{<BR> datatype
data;<BR> struct node
*next;<BR> }sclnode;<BR> </P>
<P>//初始化<BR>void scllinitiate(sclnode *
*head)<BR>{
if((*head=(sclnode*)malloc(sizeof(sclnode)))==null)exit(1);<BR> (*head)->next=*head;<BR> }<BR> </P>
<P>//插入一个结点<BR> int scllinsert(sclnode
*head,int i,datatype x)<BR> {<BR>
sclnode *p,*q;<BR> int j;<BR>
p=head->next;j=1;<BR>
while(p!=head&&j<i-1)<BR> {
p=p->next;j++;<BR> }<BR>
if(j!=i-1&&i!=1)<BR> { printf("input
parameter error!");<BR> return 0;<BR>
}<BR>
if((q=(sclnode*)malloc(sizeof(sclnode)))==null)exit(1);<BR>
q->data=x;<BR>
q->next=p->next;<BR>
p->next=q;<BR> return 1;<BR>
}<BR> </P>
<P>//删除一个结点<BR> int sclldelete(sclnode
*head,int i,datatype *x)<BR>
{<BR> sclnode
*p,*q;<BR> int
j;<BR>
p=head;j=0;<BR>
while(p->next!=head&&j<i-1)<BR>
{ p=p->next;j++;}<BR>
if(j!=i-1)<BR> {printf("delete
parameter error!");<BR> return
0;<BR> }<BR>
q=p->next;<BR>
p->next=p->next->next;<BR>
*x=q->data;<BR>
free(q);<BR> return
1;<BR> }<BR> </P>
<P>//取一个结点数据元素值<BR> int scllget(sclnode
*head,int i,datatype *x)<BR>
{<BR> sclnode
*p;<BR> int
j;<BR>
p=head;j=0;<BR>
while(p->next!=head&&j<i)<BR>
{ p=p->next;j++;}<BR>
if(j!=i)<BR> { printf("get elem
error!");<BR> return
0;<BR> }<BR>
*x=p->data;<BR> return
1;<BR> }<BR> </P>
<P>//链表非空否<BR> int scllnotempty(sclnode
*head)<BR> {<BR>
if(head->next==head)return 0;<BR>
else return 1;<BR>
}<BR> <BR>//删除p指针所指所指结点的下一个结点<BR> void
sclldeleteafter(sclnode
*p)<BR> {<BR> sclnode
*q=p->next;<BR>
p->next=p->next->next;<BR>
free(q);<BR> }<BR> </P>
<P>//带头结点单循环链表head,初始值为m的约瑟夫环问题函数<BR> void
jesephring(sclnode *head,int
m)<BR> {<BR> sclnode
*pre,*curr;<BR> int i;<BR>
pre=head;<BR>
curr=head->next;<BR>
while(scllnotempty(head)==1)<BR>
{<BR>
for(i=1;i<m;i++)<BR> {
pre=curr;<BR>
curr=curr->next;<BR>
if(curr==head)<BR> {
pre=curr;<BR>
curr=curr->next;<BR>
}<BR>
}<BR>
printf("%d",curr->data.number);<BR>
m=curr->data.cipher;<BR>
curr=curr->next;<BR>
if(curr==head)curr=curr->next;<BR>
sclldeleteafter(pre);<BR>
}<BR> }<BR> </P>
<P> void
main(void)<BR> {<BR> datatype
test[7]={{1,3},{2,1},{3,7},{4,2},{5,4},{6,8},{7,4}};<BR>
int n=7,m=20,i;<BR> sclnode
*head;<BR>
scllinitiate(&head);<BR>
for(i=1;i<=n;i++)scllinsert(head,i,test[i-1]);<BR>
jesephring(head,m);<BR> }</P>
<P><BR> </P><BR></FONT></TD></TR></TBODY></TABLE></TD></TR>
<TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -