| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1151 人关注过本帖
标题:链表问题 想不通 请大神指教 万分感谢
只看楼主 加入收藏
n172474108
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2016-8-28
结帖率:0
收藏
 问题点数:0 回复次数:4 
链表问题 想不通 请大神指教 万分感谢


设有a,b两个链表,每个链表中的节点包括学号·成绩。要求将两个链表合并为一个链表,按学号升序排列
struct student *insert(struct student *ah,struct student *bh)  
{struct student  *pa1 , *pa2,*pb1,*pb2;  
 pa2=pa1=ah;  
 pb2=pb1=bh;  
 do   
 {while((pb1->num>pa1->num)&&(pa1->next!=NULL))  
{pa2=pa1;  
pa1=pa1->next;  
}  
if(pb->num<=pa1->num)&nbsp;&nbsp;
{if(ah=pa1)&nbsp;&nbsp;
&nbsp;ah=pb1;&nbsp;&nbsp;
&nbsp;else&nbsp;pa2->next=pb1;&nbsp;&nbsp;
&nbsp;pb1=pb1->next;&nbsp;&nbsp;
&nbsp;pb2->next=pa1;&nbsp;&nbsp;
pa2=pb2;&nbsp;&nbsp;
pb2=pb1;&nbsp;&nbsp;
}&nbsp;&nbsp;
}&nbsp;&nbsp;

谁给我讲讲思路 万分感谢
搜索更多相关主题的帖子: insert 
2017-01-03 16:58
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
先问一句,你是用什么编译器的~~~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-01-03 17:15
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
C语言没有&nbsp这样东东的,这个……我在HTML上见过~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-01-03 17:16
n172474108
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2016-8-28
收藏
得分:0 
struct student *insert(struct student *ah,struct student *bh)
{struct student *pa1,*pa2,*pb1,*pb2;
pa2=pa1=ah;
pb2=pb1=bh;
do
{while((pb1->num>pa1->num)&&(pa1->next!=NULL))
{pa2=pa1;
pa1=pa1->next;
}
if(pb1->num<=pa1->num)
{if(ah==pa1)
ah=pb1;
else pa2->next=pb1;
pb1=pb1->next;
pb2->next=pa1;
pa2=pb2;
pb2=pb1;
}
}
while((pa1->next!=NULL&&pb1!=NULL));
if(pb1!=NULL)
pa1->next=pb1;
return(ah);
}       不好意思  弄错了  是这个
2017-01-04 14:46
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10541
专家分:42927
注 册:2014-5-20
收藏
得分:0 

提供的代码不完整,只好写个示例参考:

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

struct student
{
    int num;
    int score;
    struct student *next;
};

struct student *insert_sort(struct student *h, struct student *h2)
{
    struct student *p, *q, *s;
    p = h;
    q = NULL;
    s = (struct student *)malloc(sizeof(struct student));
    s->num = h2->num;
    s->score = h2->score;
    s->next = NULL;
    while (p)
    {
        if (h2->num <= p->num)
        {
            s->next = p;
            break;
        }
        q = p;
        p = p->next;
    }
    if (q)
        q->next = s;
    else
        h = s;
    return h;
}

void _list(struct student *h)
{
    struct student *p=h;
    while (p)
    {
        printf("%d\n", p->num);
        printf("%d\n\n", p->score);
        p = p->next;
    }
}

void _free(struct student *h)
{
    struct student *p=h, *q;
    while (p)
    {
        q = p->next;
        free(p);
        p = q;
    }
}

void _test()
{
    struct student ah[3]={{311,90,&ah[1]},{111,99,&ah[2]},{212,95,NULL}};
    struct student bh[3]={{321,80,&bh[1]},{121,89,&bh[2]},{222,85,NULL}};
    struct student *h, *p;
    p = ah;
    h = insert_sort(NULL, p);
    for (p=p->next; p!=NULL; p=p->next)
        h = insert_sort(h, p);
    for (p=bh; p!=NULL; p=p->next)
        h = insert_sort(h, p);
    _list(h);
    _free(h);
}

main()
{
    _test();
}

2017-01-04 16:34
快速回复:链表问题 想不通 请大神指教 万分感谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015254 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved