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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题