6.3链表和joseph(约瑟夫)问题

1.链表代码

//按升序插入
void shengxu(link*head,datatype sum){
    link*p=head;
    link*temp=(link*)malloc(sizeof(link));
    if(temp==NULL){
    printf("插入失败\n");
        return;
    }
    temp->msg.date=sum;
    while(p->next!=NULL&&p->next->msg.date<sum){
        p=p->next;
    }
    temp->next=p->next;
    p->next=temp;                                    
    head->msg.len++;

    return;
}
//链表翻转
void fanzhuan(link*head){
    link*p=head->next;
    head->next=NULL;
    while(p!=NULL){
        link*q=p->next;
        p->next=head->next;
        head->next=p;
        p=q;
    }
    return;
}

//快慢链表
datatype kuaiman(link*head){
    if(head->next==NULL||head==NULL){
        printf("链表为空");
        return (datatype)-1;
    }
    link*slow=head->next;
    link*fals=head->next;
    while(fals!=NULL&&fals->next!=NULL){
        slow=slow->next;
        fals=fals->next->next;

    }
    return slow->msg.date;
}

实现

2.joseph(约瑟夫)问题

    int n=8;
    int x=3;
    int m=4;
    link*hata=create_link();
    for(int i=1;i<=n;i++){
        weicha(hata,i);
    }
    link*p=hata;
    while(p->next!=hata){
        p=p->next;
    }
    p->next=hata->next;
    p=hata;
    hata=hata->next;
    free(p);
    
    p=hata;
    for(int i=0;i<x-1;i++){               
        p=p->next;
    }
    while(p->next!=p){
        for(int i=0;i<m-2;i++){
            p=p->next;
        }
        link*ta=p->next;
        p->next=ta->next;
        printf("%d",ta->msg.date);
        free(ta);
        p=p->next;
    }
    printf("%d\n",p->msg.date);
    free(p);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值