链表 多项式相加

版权声明:若想转载,请在显眼的地方附上文章来源。 https://blog.csdn.net/Abudula__/article/details/82749579

 在链表中有一类经典的题性,那就是多项式相加,这里提供了一个思路。

//#include "pch.h"
#include <iostream>
using namespace std;

struct Node {
	int data;
	int expo;
	Node *next;
	Node(int _data = 0, int _expo = 0, Node *_next = NULL) :data(_data), expo(_expo), next(_next) {}
};

Node *create(int n, int **arraynum)
{
	Node *tail, *p, *head;
	head = new Node;
	tail = head;
	for (int i = 0; i < n; i++)
	{
		p = new Node(arraynum[i][0], arraynum[i][1], NULL);
		tail->next = p;
		tail = p;
	}
	return head;
}

void display(Node *head)
{
	Node *p = head->next;
	
	if (p->data < 0)
		cout << "(" << p->data << ")";
	if (p->data > 0)
		cout << p->data;

	if (p->data != 0)
	{
		if (p->expo < 0)
			cout << "x^(" << p->expo << ")";

		if (p->expo > 0)
			cout << "x^" << p->expo;
	}
	p = p->next;
	while (p)
	{
		if (p->data < 0)
			cout << " + (" << p->data << ")";
		if (p->data > 0)
			cout <<" + "<<p->data;
		if (p->data != 0)
		{
			if (p->expo < 0)
				cout << "x^(" << p->expo << ")";

			if (p->expo > 0)
				cout << "x^" << p->expo;
		}
		p = p->next;
	}
	cout << endl;
}

int getcurexpo(Node *p)
{
	return p->expo;
}

int cmp(int a, int b)
{
	if (a < b)
		return -1;
	else if (a == b)
		return 0;
	else
		return 1;
}

Node *add(int n1, Node *head1, int n2, Node *head2)
{
	Node *head3 = new Node;
	Node *tail3, *pc;
	tail3 = head3;

	Node *pa;
	pa = head1->next;
	Node *pb;
	pb = head2->next;
	while (pa&&pb)
	{
		int a = getcurexpo(pa);
		int b = getcurexpo(pb);
		switch (cmp(a, b))
		{
		case -1:
			pc = new Node;
			tail3->next = pc;
			tail3 = pc;
			tail3->data = pa->data;
			tail3->expo = pa->expo;
			pa = pa->next;
			break;

		case 0:
			if ((pa->data + pb->data) != 0)
			{
				pc = new Node;
				tail3->next = pc;
				tail3 = pc;
				tail3->data = pa->data + pb->data;
				tail3->expo = pa->expo;
				pa = pa->next;
				pb = pb->next;
				break;
			}
			else
			{
				pa = pa->next;
				pb = pb->next;
				break;
			}
		case 1:
			pc = new Node;
			tail3->next = pc;
			tail3 = pc;
			tail3->data = pb->data;
			tail3->expo = pb->expo;
			pb = pb->next;
			break;
		}
	}
	while (pa)
	{
		pc = new Node;
		tail3->next = pc;
		tail3 = pc;
		tail3->data = pa->data;
		tail3->expo = pa->expo;
		pa = pa->next;
	}
	while (pb)
	{
		pc = new Node;
		tail3->next = pc;
		tail3 = pc;
		tail3->data = pb->data;
		tail3->expo = pb->expo;
		pb = pb->next;
	}
	return head3;
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n1;
		cin >> n1;
		int **arraynum1 = new int*[n1];
		for (int i = 0; i < n1; i++)
			arraynum1[i] = new int[2];

		for (int i = 0; i < n1; i++)
			for (int j = 0; j < 2; j++)
				cin >> arraynum1[i][j];

		Node *head1 = create(n1, arraynum1);
		display(head1);

		int n2;
		cin >> n2;
		int **arraynum2 = new int*[n1];
		for (int i = 0; i < n2; i++)
			arraynum2[i] = new int[2];

		for (int i = 0; i < n2; i++)
			for (int j = 0; j < 2; j++)
				cin >> arraynum2[i][j];

		Node *head2 = create(n2, arraynum2);
		display(head2);

		Node *head3 = add(n1, head1, n2, head2);
		display(head3);

	}
}

猜你喜欢

转载自blog.csdn.net/Abudula__/article/details/82749579