yandihong1 2015-01-18 02:34 采纳率: 100%
浏览 3262
已采纳

c语言循环链表,解决约瑟夫问题

c语言循环链表,一开始用头结点作为开端,创建一连串的节点,到最后一个节点的next指向head的下一个节点可以么。代码如下,虚心求教。
#include
#include
#define N 6
#define M 5
struct people
{
int num;
struct people *next;
};
struct people *head,*last,*p,*history,*now;
int i,j;
int main()
{
head=malloc(sizeof(struct people));
head->num=0;
head->next=NULL;
last=head;
history=head;

for(i=1;i<N+1;i++)
{
    p=malloc(sizeof(struct people));
    p->num=i;
    p->next=NULL;
    last->next=p;
    last=p;
}

history=head;
now=head->next;
while(now!=NULL)
{
    printf("%d",now->num);
    history=now;
    now=now->next;
}

last->next=head->next;

history=last;
now=last->next;
while(now->next!=NULL)
{
    for(j=1;j<M;j++)
    {
        history=now;
        now=now->next;
    }
    p=now->next;
    free(now);
    now=p;
    history->next=now;      
}
printf("%d",now->num);
return 0;   

}

  • 写回答

2条回答 默认 最新

查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入