当前位置: 代码迷 >> C语言 >> 编译出错 请高手帮帮忙
  详细解决方案

编译出错 请高手帮帮忙

热度:115   发布时间:2008-04-14 16:43:20.0
编译出错 请高手帮帮忙
//----------------------------------------------------------------------------------
//n个人围成一圈,从第一个人开始报数,凡报到3的人退出圈子,求最后留下的人是原来第几号
//----------------------------------------------------------------------------------
//编译有错误 帮忙排错

#include<stdio.h>
#include<malloc.h>
//--------------------
typedef struct linknode
{
    int data;
    struct linknode *next;
} node;
//--------------------
node *creat(int n)   //创建链表把1~n个人放进去
{
    int i;
    node *p, *s, *head;
    head = (node *)malloc(sizeof(node));
    p = head;
    for(i = 1 ; i <= n ; i++)
    {
        s = (node *)malloc(sizeof(node));
        s->data = i;
        p->next = s;
        p = s;
    }
    head = head->next;
    p->next = head;
    p = NULL;
    return head;
}
//--------------------
int suanFa()     //去掉报3的人
{
    int n;
    scanf("%d", &n);
    node *q1, *q;
    q1 = creat(n);
    q = q1;
    while(q1->next != q1)
    {
       q1 = q1->next;
       q = q1->next;
       q1->next = q1->next->next;
       q1 = q1->next;
       q = NULL;
       free(q);
    }
    return q1->data;
}
//---------------------
int main()
{
    printf("%d", suanFa());
    return 0;
}

编译有错 不知道错那里了啊 帮我看一下啊
搜索更多相关的解决方案: 编译  

----------------解决方案--------------------------------------------------------
scanf("%d", &n);
node *q1, *q;
//此两句对调位置;定义语句应先于执行语句;
----------------解决方案--------------------------------------------------------
node *creat(int n)   //创建链表把1~n个人放进去
{
    int i;
    node *p, *s, *head;
    head = (node *)malloc(sizeof(node));
    p = head;
    for(i = 1 ; i <= n ; i++)
    {
        s = (node *)malloc(sizeof(node));
        s->data = i;
        p->next = s;
        p = s;
    }
    head = head->next;//head没有释放有内存泄漏现象
    p->next = head;
    p = NULL;
    return head;
}
----------------解决方案--------------------------------------------------------
  相关解决方案