qq_34378013 2016-03-23 10:23 采纳率: 14.3%
浏览 1397
已采纳

新手关于链表问题求助

#include<stdio.h>
#include<stdlib.h>


typedef struct node
{
    int num;
    struct node *next;
}LN;


LN *Creat();
LN Divide(LN *h);
void Printf(LN *s);


void main()
{
    LN *h;
    h=Creat();
    Divide(h);
    printf("The even term is:  ");
    Printf(h);
    printf("The odd term is:  ");
    Printf(q);
}


LN *Creat()
{
    LN *p;
    LN *b;
    LN *h;
    int a;
    h=(LN *)malloc(sizeof(LN));
    h->next=NULL;
    b=h;
    printf("Input the sum of the numbers:  ");
    scanf("%d",&a);
    for(int i=0;i<a;i++)
    {
        printf("Input the %d number: ",i+1);
        p=(LN *)malloc(sizeof(LN));
        scanf("%d",&p->num);
        b->next=p;
        b=p;
    }
    p->next=NULL;
    system("CLS");
    return h;
}


LN Divide(LN *h)
{
    LN *q;
    LN *b;
    q=(LN *)malloc(sizeof(LN));
    b=q;
    while(h!=NULL&&h->next!=NULL)
    {
        b=h->next;
        h->next=b->next;
        h=h->next;
    }
    return *q;
}

void Printf(LN *s)
{
    LN *p;
    p=s->next;
    while(p)
    {
        printf("%d  ",p->num);
        p=p->next;
    }
}

**在Divide这个函数里面我有两个链表,如代码所示,我已经返回头指针q,可是为什么程序还是运行不了?简而言之就是我的q怎么传出来,我明明已经传出来了,在主函数里面又要由什么接应他。
**

  • 写回答

3条回答

  • 小灸舞 2016-03-23 12:40
    关注

    Divide函数应该返回一个指针,然后在main函数里接收该指针地址,而且Divide函数里本身也有点问题,我已经改了,楼主可以看一下

    #include<stdio.h>
    #include<stdlib.h>
    
    
    typedef struct node
    {
        int num;
        struct node *next;
    }LN;
    
    
    LN *Creat();
    LN *Divide(LN *h);
    void Printf(LN *s);
    
    
    void main()
    {
        LN *h;
        h=Creat();
        LN *q = Divide(h);
        printf("The even term is:  ");
        Printf(h);
        printf("The odd term is:  ");
        Printf(q);
    }
    
    
    LN *Creat()
    {
        LN *p;
        LN *b;
        LN *h;
        int a;
        h=(LN *)malloc(sizeof(LN));
        h->next=NULL;
        b=h;
        printf("Input the sum of the numbers:  ");
        scanf("%d",&a);
        for(int i=0;i<a;i++)
        {
            printf("Input the %d number: ",i+1);
            p=(LN *)malloc(sizeof(LN));
            scanf("%d",&p->num);
            b->next=p;
            b=p;
        }
        p->next=NULL;
        system("CLS");
        return h;
    }
    
    
    LN *Divide(LN *h)
    {
        LN *q;
        LN *b;
        q=(LN *)malloc(sizeof(LN));
    
    
        b=q;
        while(h!=NULL&&h->next!=NULL)
        {
            b->next=h->next;
            b=b->next;
            h->next=b->next;
            h=h->next;
    
        }
        return q;
    }
    
    void Printf(LN *s)
    {
        LN *p;
        p=s->next;
        while(p)
        {
            printf("%d  ",p->num);
            p=p->next;
        }
    }
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料