GX_ZCww 2022-03-29 18:55 采纳率: 100%
浏览 27
已结题

合并有序数列错误输出

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

typedef struct node
{
int data;
struct node *next;
}Node;

void Creatlist(Node head, int n)
{
Node p;
p = (Node
)malloc(sizeof(Node));
p = head;
for(int i=0; i<n; i++){
p->next = (Node
)malloc(sizeof(Node));
scanf("%d", &p->next->data);
p = p->next;
}
p->next = NULL;
}

Node *hebinglist(Node *head1,Node *head2)
{
if(!head1)
return head2;
if(!head2)
return head1;
if(head1->data <= head2->data){
head1->next = hebinglist(head1->next,head2);
return head1;
}
if(head1->data > head2->data){
head2->next = hebinglist(head1,head2->next);
return head2;
}
};

void Outputlist(Node *head)
{
Node *p = head->next;
while(p){
printf("%d ", p->data);
p = p->next;
}
}

img

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-03-30 14:22
    关注

    修改如下,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    #include<string.h>
    
    typedef struct node
    {
        int data;
        struct node* next;
    }Node;
    
    void Creatlist(Node** head, int n)     //修改
    {
        Node *p;
        (*head) = (Node*)malloc(sizeof(Node));//修改
        p = (*head);                          //修改
        for (int i = 0; i < n; i++) {
            p->next = (Node*)malloc(sizeof(Node));
            scanf("%d", &p->next->data);
            p = p->next;
        }
        p->next = NULL;
    }
    
    Node* hebinglist(Node* head1, Node* head2)
    {
        if (!head1)
            return head2;
        if (!head2)
            return head1;
        if (head1->data <= head2->data) {
            head1->next = hebinglist(head1->next, head2);
            return head1;
        }
        else {   //if (head1->data > head2->data) {  //修改
            head2->next = hebinglist(head1, head2->next);
            return head2;
        }
    }                               //;
    
    void Outputlist(Node* head)
    {
        Node* p = head->next;
        while (p) {
            printf("%d ", p->data);
            p = p->next;
        }
        printf("\n");
    }
    int main()
    {
        int m, n;
        Node* h1, * h2, * h3;
        scanf("%d%d", &m, &n);
    
        Creatlist(&h1, m);
        //Outputlist(h1);
    
        Creatlist(&h2, n);
        //Outputlist(h2);
    
        h3 = (Node*)malloc(sizeof(Node));
        h3->next = hebinglist(h1->next, h2->next);
        Outputlist(h3);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 创建了问题 3月29日

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作