【QBKbupt】第二章 线性表 C.线性表练习3

在这里插入图片描述
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
struct Node
{
	int in;
	int cn;
	Node *next;
};
void init(Node **);
void create(Node *,int);
void calculate(Node *,Node *,Node *);
void print(Node *);
int main()
{
	int n,m;
	Node *head1,*head2,*head3;
	init(&head1);
	init(&head2);
	init(&head3);
	head3->in=2147483647;
	scanf("%d",&n);
	create(head1,n);
	scanf("%d",&m);
	create(head2,m);
	calculate(head1,head2,head3);
	print(head3);
	return 0;
}
void init(Node **ptr)
{
	*ptr=(Node*)malloc(sizeof(Node));
	(*ptr)->next=NULL;
}
void create(Node *ptr,int x)
{
	for(int i=1;i<=x;i++)
	{
		ptr->next=(Node*)malloc(sizeof(Node));
		ptr=ptr->next;
		scanf("%d%d",&ptr->cn,&ptr->in);
		ptr->next=NULL;
	}
}
void calculate(Node *ptrA,Node *ptrB,Node *ptrC)
{
	int a,b;
	Node *p1,*p2,*p3,*p;
	p1=ptrA->next;
	while(p1!=NULL)
	{
		p2=ptrB->next;
		while(p2!=NULL)
		{
			a=p1->cn*p2->cn;
			b=p1->in+p2->in;
			p3=ptrC;
			p=ptrC;
			while(p3!=NULL)
			{
				if(b<p3->in)
				{
					if(p3->next==NULL)
					{
						p3->next=(Node*)malloc(sizeof(Node));
						p3=p3->next;
						p3->cn=a;
						p3->in=b;
						p3->next=NULL;
						break;
					}
					else p3=p3->next;
				}
				else if(b==p3->in)
				{
					p3->cn+=a;
					break;
				}
				else
				{
					p->next=(Node*)malloc(sizeof(Node));
					p=p->next;
					p->cn=a;
					p->in=b;
					p->next=p3;
					break;
				}
				
			}
			p2=p2->next;
		}
		p1=p1->next;
	}
}
void print(Node *ptr)
{
	int sum=0;
	Node *p;
	p=ptr->next;
	while(p!=NULL)
	{
		if(p->cn!=0) sum++;
		p=p->next;
	}
	printf("%d\n",sum);
	p=ptr->next;
	while(p!=NULL)
	{
		if(p->cn!=0) printf("%d %d\n",p->cn,p->in);
		p=p->next;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值