没啥好说的,写一下指针练一练,用指针挺容易出错的细节挺多
#include<bits/stdc++.h>
#define ULL unsigned long long
#define DB double
#define LL long long
using namespace std;
int n,m,i,j;
struct QNode
{
int shu,ci;
QNode* nt;
}*headA,*headB,*tailA,*tailB,*headAns,*tailAns;
void InitQ(QNode *&b,QNode *&e)
{
b=e=new QNode;
b->nt=e->nt=NULL;
}
void ru(QNode *&l,int shu,int ci)
{
QNode *p=new QNode;
p->shu=shu;p->ci=ci;p->nt=NULL;
l->nt=p;l=p;
}
int main()
{
cin>>n>>m;
InitQ(headA,tailA);InitQ(headB,tailB);InitQ(headAns,tailAns);
for(int i=1,shu,ci;i<=n;++i)scanf("%d%d",&shu,&ci),ru(tailA,shu,ci);
for(int i=1,shu,ci;i<=m;++i)scanf("%d%d",&shu,&ci),ru(tailB,shu,ci);
i=j=1;
headA=headA->nt;headB=headB->nt;
while(headA!=NULL&&headB!=NULL)
{
if(headA->ci==headB->ci)ru(tailAns,headA->shu+headB->shu,headA->ci),headA=headA->nt,headB=headB->nt;
else if(headA->ci<headB->ci)ru(tailAns,headA->shu,headA->ci),headA=headA->nt;
else ru(tailAns,headB->shu,headB->ci),headB=headB->nt;
}
while(headA!=NULL)ru(tailAns,headA->shu,headA->ci),headA=headA->nt;
while(headB!=NULL)ru(tailAns,headB->shu,headB->ci),headB=headB->nt;
headAns=headAns->nt;
while(headAns!=NULL)
{
if(headAns->shu)printf("%d %d\n",headAns->shu,headAns->ci);
headAns=headAns->nt;
}
return 0;
}