浙大版《C语言程序设计(第3版)》题目集 - 习题11-7 奇数值结点链表(20 分)

版权声明:本文为博主原创文章,转载前动动小手点个【赞】吧~ https://blog.csdn.net/Dream_Weave/article/details/85533413

题目链接:点击打开链接

题目大意:略。

解题思路:题目说删除了,其实可以转化为再搞一个获取奇数时的做法来做偶数的情况,最后把地址赋值给L,这样思路就简单许多~。还有这里带两个星号的L,其实多了一个星号是因为传参时,传进去的是指针变量的地址(此地址非内容)

AC 代码

struct ListNode *readlist()
{
    struct ListNode *h, *p, *pre;
    int da, fst=1;
    while(~scanf("%d", &da) && da!=-1)
    {
        p=(struct ListNode*)malloc(sizeof(struct ListNode));
        p->data=da;
        if(!fst) pre->next=p, pre->next->next=NULL, pre=pre->next;
        if(fst) pre=h=p, fst=0;
    }

    return h;
}

struct ListNode *getodd( struct ListNode **L )
{
    struct ListNode *p, *pre1, *pre2, *h1=NULL, *h2=NULL;
    int f1=1, f2=1, da;
    while(*L)
    {
        da=(*L)->data;
        if(da%2)
        {
            p=(struct ListNode*)malloc(sizeof(struct ListNode));
            p->data=da;
            if(!f1) pre1->next=p, pre1->next->next=NULL, pre1=pre1->next;
            if(f1) pre1=h1=p, f1=0;
        }
        else
        {
            p=(struct ListNode*)malloc(sizeof(struct ListNode));
            p->data=da;
            if(!f2) pre2->next=p, pre2->next->next=NULL, pre2=pre2->next;
            if(f2) pre2=h2=p, f2=0;
        }
        *L=(*L)->next;
    }
    *L=h2;
    return h1;
}

猜你喜欢

转载自blog.csdn.net/Dream_Weave/article/details/85533413