代码拉取完成,页面将自动刷新
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node, *LinkList;
void MergeList(LinkList a, LinkList b);
int main() {
int T = 0;
int t = 0;
scanf("%d", &T);
for( t = 0; t < T; t++ ) { // Loop for T times
int lenA = 0, lenB = 0;
int i = 0;
scanf("%d", &lenA);
scanf("%d", &lenB);
//Create La
LinkList La;
La = (LinkList)malloc(sizeof(Node));
La->next = NULL;
for ( i = 0; i < lenA; i++ ) {
Node* p = NULL;
p = (LinkList)malloc(sizeof(Node));
p->next = La->next;
La->next = p;
}
//Create Lb
LinkList Lb;
Lb = (LinkList)malloc(sizeof(Node));
Lb->next = NULL;
for ( i = 0; i < lenB; i++ ) {
Node* p = NULL;
p = (LinkList)malloc(sizeof(Node));
p->next = Lb->next;
Lb->next = p;
}
//Input La
LinkList ptrA = La;
for ( i = 0; i < lenA; i++) {
ptrA = ptrA->next;
scanf("%d", &(ptrA->data));
}
//Input Lb
LinkList ptrB = Lb;
for ( i = 0; i < lenB; i++) {
ptrB = ptrB->next;
scanf("%d", &(ptrB->data));
}
//Merge Lb to La
MergeList(La, Lb);
//Generate Lc
LinkList Lc = La;
//Print Lc
LinkList p = Lc->next;
for ( i = 0; i < (lenA + lenB); i++ ) {
printf("%d",p->data);
p = p->next;
if ( i == lenA + lenB - 1 ) {
printf ("\n");
}
else printf(" ");
}
}
return 0;
}
void MergeList(LinkList a, LinkList b) {
LinkList NewP, pa, pb;
NewP = a;//Travel through new list
pa = a->next;// Travel through La
pb = b->next;// Travel through Lb
while (pa && pb)
{
//Add a node from La
NewP->next = pa;
pa = pa->next;
NewP = NewP->next;
if (pa==0) break;//stop, if La is empty
//Add a node from Lb
NewP->next = pb;
pb = pb->next;
NewP = NewP->next;
if (pb==0) break;//stop, if Lb is empty
}
NewP->next = pa ? pa : pb;//Append the rest
free(b); //Free the header of Lb
return;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。