,.?!!?., 2017-12-24 18:37 采纳率: 0%
浏览 1257

大佬们 帮帮我吧 困扰几天的一个小问题 万分感激

两个 链表的合并 怎么运行就是不对 图片说明![图片说明](https://img-ask.csdn.net/upload/201712/25/1514140727_96676.jpg)图片说明#include
#include
#include
struct jg
{
int data;
struct jg *next;

};
void attach(int a, struct jg** nrear)
{
struct jg *p;

p = (struct jg *)malloc(sizeof(struct jg));
p->data = a;
p->next = NULL;
(*nrear)->next = p;
*nrear = p;

}
struct jg* read()
{
struct jg* p;
struct jg* rear;
struct jg* t;
p=(struct jg )malloc(sizeof(struct jg));
int a, m;
scanf("%d", &a);
p->next=NULL;
rear=p;
while (a--)
{
scanf("%d", &m);
attach(m,&rear);
}
t = p;
p = p->next;
free(t);
return p;
}
struct jg
add( struct jg* p1,struct jg* p2)
{
struct jg *p, *rear, *t;
p = (struct jg *)malloc(sizeof(struct jg));
rear = p;
p->next = NULL;

while(p1&&p2)
{
    if (p1->data > p2->data)
    {
        attach(p2->data, &rear);
        p2 = p2->next;

    }
    else if (p1->data = p2->data)
    {
        attach(p1->data, &rear);

        p1 = p1->next;
        p2=p2->next;

    }
    else
    {
        attach(p1->data, &rear);
        p1 = p1->next;
    }

}

    while (p1)
    {
        attach(p1->data, &rear);
        p1 = p1->next;
    }
    while(p2)
    {
        attach(p2->data, &rear);
        p2 = p2->next;
    }

t = p;
p = p->next;
free(t);
return p;

}
void print( struct jg *p1)
{
int a = 0;
while(p1)
{
if (a ==0)
{
a = 1;
}

else
{
printf(" ");
}
printf("%d",p1->data);
p1= p1->next;
}
}

int main()
{
struct jg *p1, *p2,*p3;
p1 = read();
p2 = read();
p3 = add(p1, p2);
print(p3);
system("pause");
return 0;

}

![![![图片说明](https://img-ask.csdn.net/upload/201712/25/1514140655_34377.jpg)图片说明](https://img-ask.csdn.net/upload/201712/25/1514140647_156200.jpg)图片说明](https://img-ask.csdn.net/upload/201712/25/1514140639_525873.jpg)图片说明

不知道为啥 程序运行怎么就不对呢 两个链表合并

图片说明
按照大家说的改完之后成这样了图片说明

  • 写回答

5条回答 默认 最新

  • 我要出家当道士 运维领域新星创作者 2017-12-25 00:38
    关注

    链接两个链表,把前者链尾指向后者的头部,得到了新的链表再排序

    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同