,.?!!?., 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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?