关于pta上6-2 两个有序链表序列的合并
这是在dev上的源代码,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 ); /* 细节在此不表;空链表将输出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函数删了,就运行成功了,我不理解