想问一下我写的头插法和尾插法哪里出了错误为什么显示无法运行?还有约瑟夫的代码哪里出错了?


#include<stdio.h>
#include<string.h>
#include<cstdlib>
struct linklist{
    int a;
    struct linklist *next;
};//自定义节点;
struct linklist *create(){
    struct linklist *headnode=(struct linklist*)malloc(sizeof(struct linklist));
    headnode->next=NULL;
    headnode->a=100;
    return headnode;
}//创建表
struct linklist *print(int data,struct linklist *headnode){
    struct linklist *connect=(struct linklist*)malloc(sizeof(struct linklist));
    connect->a=data;
    connect->next=headnode;
    return connect;
}//创建节点
//头插法
void insertnodebyhead(int data,struct linklist *headnode){
    struct linklist *new=print(data,headnode);
    new->next=headnode->next;
    headnode->next=new;
}
//尾插法
void insertnodebytail(struct linklist *headnode,int data){
    struct linklist *new=print(data,headnode),*tailnode;
    tailnode=headnode;
    while(tailnode->next!=headnode){
        tailnode=tailnode->next;
    }
    tailnode->next=new;
    new->next=headnode;
}
int length(struct linklist*joe){
    int length=1;
    struct linklist *k;
    k=joe;
    while(k->next!=joe)
    {length++;
    k=k->next;
    }
    return length;
}
int main(){
    struct linklist *p,*headnode,*joe,*kill;
    int n,m,k=0,length1,i;
    printf("请输入n和m");
    scanf("%d%d",&n,&m);
    headnode=create();
    p=headnode;
    for(i=0;i<n;i++)
    {struct linklist *node;
    node=print(i+1,headnode);
    p->next=node;//链接
    p=node;
    }
    p->next=headnode->next;
    free(headnode);
    joe=p->next;
    length1=length(joe);
    while(length1!=1){
        k++;
        if(k==m-1)
        {kill=joe->next;
        joe->next=kill->next;
        free(kill);
        length1--;
        k=0;
        }
        joe=joe->next;
    }
    printf("%d",joe->a);}    
    

【相关推荐】



  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/229069
  • 你也可以参考下这篇文章:输入一个正整数,要求以相反的顺序输出该数。用递归方法实现。(输出一个数的回文数)
  • 您还可以看一下 孙玖祥老师的图解数据结构与算法课程中的 循环队列的入队出队扩容操作小节, 巩固相关知识点
  • 除此之外, 这篇博客: 浮点数的表示方法中的 浮点数为什么有精度损失? 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:

    我们再来看一下,平时经常听到的浮点数会有精度损失的情况是怎么回事?

    如果我们现在想用浮点数表示 0.2,它的结果会是多少呢?

    0.2 转换为二进制数的过程为,不断乘以 2,直到不存在小数为止,在这个计算过程中,得到的整数部分从上到下排列就是二进制的结果。

    0.2 * 2 = 0.4 -> 0
    0.4 * 2 = 0.8 -> 0
    0.8 * 2 = 1.6 -> 1
    0.6 * 2 = 1.2 -> 1
    0.2 * 2 = 0.4 -> 0(发生循环)
    ...
    

    所以 0.2(D) = 0.00110…(B)。

    因为十进制的 0.2 无法精确转换成二进制小数,而计算机在表示一个数字时,宽度是有限的,无限循环的小数存储在计算机时,只能被截断,所以就会导致小数精度发生损失的情况。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^