PTA 6-4 两个有序链表序列的合并 (15分)

全部代码

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;

List Read(); /* 细节在此不表 */
//同样是自己写了一个 
List Read(){
	int n,a;
	List head,tail,p;
	head = (List)malloc(sizeof(struct Node));
	tail = head;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a);
		p = (List)malloc(sizeof(struct Node));
		p->Data = a;
		p->Next = NULL;
		tail->Next = p;
		tail = p;
	}
	return head;
}
 
void Print( List L ); /* 细节在此不表;空链表将输出NULL */
//自己写了一个 
void Print( List L){
	if(L->Next==NULL){
		printf("NULL\n");
		return ;
	}
	List temp = L->Next;
	while(temp){
		printf("%d ",temp->Data);
		temp = temp->Next;
	}
	printf("\n");
}

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 ){
	struct Node *newlist,*temp1,*temp2,*p,*newtemp;
	newlist = (struct Node *)malloc(sizeof(struct Node));
	newlist->Next = NULL;
	newlist->Data = 0;
	newtemp = newlist;
	temp1 = L1->Next,temp2 = L2->Next;
	
	while(temp1!=NULL || temp2!=NULL){
		if(temp1==NULL||temp2==NULL){
			if(temp1==NULL){
				p = (struct Node *)malloc(sizeof(struct Node));
				p->Data = temp2->Data;
				p->Next = NULL;
				newtemp->Next = p;
				newtemp = p;
				temp2 = temp2->Next;
			}
			else if(temp2==NULL){
				p = (struct Node *)malloc(sizeof(struct Node));
				p->Data = temp1->Data;
				p->Next = NULL;
				newtemp->Next = p;
				newtemp = p;
				temp1 = temp1->Next;
			}
		}
		else if(temp1->Data < temp2->Data){
			p = (struct Node *)malloc(sizeof(struct Node));
			p->Data = temp1->Data;
			p->Next = NULL;
			newtemp->Next = p;
			newtemp = p;
			temp1 = temp1->Next;
		}
		else if(temp2->Data <= temp1->Data){
			p = (struct Node *)malloc(sizeof(struct Node));
			p->Data = temp2->Data;
			p->Next = NULL;
			newtemp->Next = p;
			newtemp = p;
			temp2 = temp2->Next;
		}
	}
	L1->Next = NULL,L2->Next = NULL;
	return newlist;
}

提交代码


List Merge( List L1, List L2 ){//T
	struct Node *newlist,*temp1,*temp2,*p,*newtemp;
	newlist = (struct Node *)malloc(sizeof(struct Node));
	newlist->Next = NULL;
	newlist->Data = 0;
	newtemp = newlist;
	temp1 = L1->Next,temp2 = L2->Next;
	
	while(temp1!=NULL || temp2!=NULL){
		if(temp1==NULL||temp2==NULL){
			if(temp1==NULL){
				p = (struct Node *)malloc(sizeof(struct Node));
				p->Data = temp2->Data;
				p->Next = NULL;
				newtemp->Next = p;
				newtemp = p;
				temp2 = temp2->Next;
			}
			else if(temp2==NULL){
				p = (struct Node *)malloc(sizeof(struct Node));
				p->Data = temp1->Data;
				p->Next = NULL;
				newtemp->Next = p;
				newtemp = p;
				temp1 = temp1->Next;
			}
		}
		else if(temp1->Data < temp2->Data){
			p = (struct Node *)malloc(sizeof(struct Node));
			p->Data = temp1->Data;
			p->Next = NULL;
			newtemp->Next = p;
			newtemp = p;
			temp1 = temp1->Next;
		}
		else if(temp2->Data <= temp1->Data){
			p = (struct Node *)malloc(sizeof(struct Node));
			p->Data = temp2->Data;
			p->Next = NULL;
			newtemp->Next = p;
			newtemp = p;
			temp2 = temp2->Next;
		}
	}
	L1->Next = NULL,L2->Next = NULL;
	return newlist;
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值