我也不知道写的对不对(笑)

vscode的C语言我就没配置好过(笑)

#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;
List Read(); 
void Print( List L ); 
List Merge( List L1, List L2 ); 
int main()
{
    List L1, L2, L;
    L1 = Read();  
    L2 = Read();  
    L = Merge(L1, L2);  
    Print(L);
    Print(L1);
    Print(L2);
    return 0;
}
List Merge( List L1, List L2 )
{
    List L, p1, p2, r ;
    p1 = L1->Next;
    p2 = L2->Next;
    L = (List)malloc(sizeof(struct Node));
    L->Next = NULL;
    r = L;
    while (p1 && p2){
        if(p1->Data < p2->Data){
            r->Next = p1;
            r = p1;
            p1 = p1->Next;
        }
        else{
            r->Next = p2;
            r = p2;
            p2 = p2->Next;
        }
    }
    if(p1)
        r->Next = p1;
    else
        r->Next = p2;
    L1->Next = NULL;
    L2->Next = NULL;
    return L;
}
List Read()
{
    int n, i;
    List L, p, s;
    scanf("%d",&n);
    L = (List)malloc(sizeof(struct Node));
    L->Next = NULL;
    p = L;
    for(i = 0;i < n; ++i){
        s = (List)malloc(sizeof(struct Node));
        scanf("%d",&s->Data);
        s->Next = p->Next;
        p->Next = s;
        p = s;
    }
    return L;
}
void Print(List L)
{
    List p;
    p = L->Next;
    if (L->Next==NULL)
    {
    printf("NULL");
    }
    while(p)
    {
    printf("%d ", p->Data);
    p = p->Next;
    }
    printf("\n");
}

优化(?)

#include <stdio.h>
#include <stdlib.h>
typedef struct  Node
{
    int data;
    struct Node* next;
}Node,* Linklist;
void Initlist(Linklist* L)
{
    *L = (Linklist)malloc(sizeof(Node));
    (*L)->next = NULL;
}
void CreateFromTall(Linklist L)
{
    Node* r, * s;
    r = L;
    int flag = 1;
    int num;
    while (flag)
    {
        scanf_s("%d",&num);
        if (num != -1)
        {
            s = (Node*)malloc(sizeof(Node));
            s->data = num;
            r->next = s;
            r = s;
        }
        else
        {
            flag = 0;
            r->next = NULL;
        }
        
    }
}
void Output(Linklist L)
{
    Node* temp;
    temp = L->next;
    while(temp != NULL)
    {
        printf("%d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}
Linklist Unionlist(Linklist LA,Linklist LB)
{
    Linklist LC;
    Initlist(&LC);
    Node* pa,* pb,* r;
    pa = LA->next;
    pb = LB->next;
    r = LC;
    while(pa != NULL && pb != NULL)
    {
        if(pa->data <= pb->data)
        {
            r->next =pa;
            r = pa;
            pa = pa->next;
        }
        else
        {
            r->next =pb;
            r = pb;
            pb = pb->next;
        }
    }
    if(pa)
    {
        r->next = pa;
    }
    else
    {
        r->next = pb;
    }
    free(LB);
    free(LA);
    return(LC);
}
int main()
{
    Linklist LA, LB, LC;
    Initlist(&LA);
    Initlist(&LB);
    printf("输入递增单表链LA,以-1作为结束标志\n");
    CreateFromTall(LA);
    Output(LA);
    printf("输入递增单表链LB,以-1作为结束标志\n");
    CreateFromTall(LB);
    Output(LB);
    if(LA->next == NULL)
    {
        printf("输入有误\n");
    }
    LC = Unionlist(LA, LB);
    printf("合并为一个有序单表链为\n");
    Output(LC);
}
最后修改:2020 年 08 月 26 日 03 : 03 PM
如果觉得我的文章对你有用,请随意赞赏