头插法,没有输出,跪求大佬解答!!
  • 板块学术版
  • 楼主tndbsfk
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/3/9 20:47
  • 上次更新2023/10/23 22:05:09
查看原帖
头插法,没有输出,跪求大佬解答!!
955141
tndbsfk楼主2023/3/9 20:47
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node
{
    long int number;
    struct Node *next;
};
struct Node *create(int a)   //链表创建函数
{
    int n;
    struct Node *p1, *p2, *head;
    head = NULL;
    n = 0;
    p2 = p1 = (struct Node *) malloc(sizeof(struct Node));   //分配内存
    scanf("%ld", &p1->number);

    while (a)  //录入链表信息
    {
        n = n + 1;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = (struct Node *) malloc(sizeof(struct Node));
        if (a != 1)  //分配内存
            scanf("%ld", &p1->number);
        a--;     //控制输入的个数
    }
    p2->next = NULL;
    return (head);
}  //链表创建函数结束

void print(struct Node *head)    //输出函数
{
    struct Node *p;
    p = head;
    printf("数字:\n");
    if (head != NULL)
        do
        {
            printf("%ld", p->number);
            printf(" ");
            p = p->next;
        }
        while (p != NULL);
    printf("\n");
}
struct Node *hb(struct Node *p1,struct Node *p2)
{
    struct Node *p3,*p4,*l;
    l->next=NULL;
    while((p1=!NULL)&&(p2!=NULL))
    {
        if(p1->number<p2->number)
        {
           p3=p1->next;
           p1->next=l->next;
           l->next=p1;
           p1=p3;
        }
        else
        {
            p4=p2->next;
           p2->next=l->next;
           l->next=p2;
           p2=p4;

        }
    }
    while(p1!=NULL)
    {
         p3=p1->next;
           p1->next=l->next;
           l->next=p1;
           p1=p3;
    }
    while(p2!=NULL)
    {
        p4=p2->next;
           p2->next=l->next;
           l->next=p2;
           p2=p4;
    }
    return (l->next);
}

int main()  //main函数
{
    struct Node *p1,*p2,*p3;
    int a,b;
    scanf("%d%d",&a,&b);
    p1=create(a);
    p2=create(b);
    p3=hb(p1,p2);
    print(p3);
    return 0;
}


2023/3/9 20:47
加载中...