,.?!!?., 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
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装