2019年PAT甲级考试第二题

2019年PAT甲级考试第二题

题目大意是给你两个链表L1和L2,然后在L1中每隔两个节点插入一个L2中的节点。
节点结构为Address data next,其中Addressnext均是以字符串的格式给出。当next-1表示下一节点为空

输入样例:

首行给出两个链表的首地址和节点数N,接着是N行节点信息

00100 01000 4
38975 5 00102
00100 6 38975
00102 7 -1
01000 3 -1

输出样例:

输出合并后的链表

00100 6 38975
38975 5 01000
01000 3 00102
00102 7 -1

参考代码

#include<iostream>
#include<map>
#include<string>

using namespace std;

struct Node{
	int data;
	string next;
};

int main()
{
	map<string,Node> List;
	string star1,star2;
	int n;
	cin>>star1>>star2>>n;
	
	//输入模块 
	for(int i=0;i<n;i++)
	{
		string addressbuf;
		Node inputbuf;
		cin>>addressbuf>>inputbuf.data>>inputbuf.next;
		List[addressbuf] = inputbuf;
	}
	
	//工作模块,修改静态链表指针 
	string p=star1,pre;
	while(star2!="-1")
	{
		for(int i=0;i<2;i++)
		{
			pre.replace(0,pre.length(),p);
			p.replace(0,p.length(),List[p].next);
		}
		
		string m=List[star2].next;
		List[star2].next.replace(0,List[star2].next.length(),p);
		List[pre].next.replace(0,List[pre].next.length(),star2);
		star2.replace(0,star2.length(),m);
	}
	
	//输出模块 
	if(star1!="-1"){
		cout<<star1<<" "<<List[star1].data<<" "<<List[star1].next;
		star1.replace(0,star1.length(),List[star1].next);
		while(star1!="-1")
		{
			cout<<endl;
			cout<<star1<<" "<<List[star1].data<<" "<<List[star1].next;
			star1.replace(0,star1.length(),List[star1].next);	
		}
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值