关于pta上6-2 两个有序链表序列的合并

这是在dev上的源代码,C语言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#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 ); /* 细节在此不表;空链表将输出NULL */
 
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 Read()
{
    int n;
    List L=(List)malloc(sizeof(PtrToNode));
    L->Next=NULL;
    scanf("%d",&n);
    struct Node* r=(struct Node*)malloc(sizeof(struct Node));
    r=L;
    int i;
    for(i=0;i<n;i++)
    {
        struct Node* s=(struct Node*)malloc(sizeof(struct Node));
        scanf("%d",&(s->Data));
        s->Next=NULL;
        r->Next=s;
        r=s;
    }
    return L;
}
void Print(List L)
{
    struct Node* p=(struct Node*)malloc(sizeof(struct Node));
    p=L->Next;
    if(!p)
        printf("NULL");
    while(p)
    {
        printf("%d ",p->Data);
        p=p->Next;
    }    printf("\n");
}
List Merge(List L1,List L2)
{
    List L=(List)malloc(sizeof(PtrToNode));
    struct Node* p=(struct Node*)malloc(sizeof(struct Node));
    struct Node* p1=(struct Node*)malloc(sizeof(struct Node));
    struct Node* p2=(struct Node*)malloc(sizeof(struct Node));
    L->Next=NULL;
    p=L;
    p1=L1->Next;
    p2=L2->Next;
    if(!p1)
        return L2;
    if(!p2)
        return L1;
    while(p1&&p2)
    {
        if((p1->Data)<=(p2->Data))
        {
            p->Next=p1;
            p1=p1->Next;
            p=p->Next;
        }
        else
        {
            p->Next=p2;
            p2=p2->Next;
            p=p->Next;
        }
    }
    if(!p1)
        p->Next=p2;
    else
        p->Next=p1;
    L1->Next=NULL;
    L2->Next=NULL;
    return L;
}

  

然后在pta上运行出现了错误

 

错误显示重复定义了Read和Print函数,但是我实在是不知道他在哪定义了,弄了半天最后把Read和Print函数删了,就运行成功了,我不理解

 

posted @   umiQa  阅读(166)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示