多项式加法(链表)

本文介绍如何使用双向链表来实现多项式的加法操作,用户输入系数和指数,程序将升序和降序输出结果。代码示例中详细展示了链表节点结构和多项式加法的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用双向链表实现多次项的加法,最后分别升序降序输出
要求先输入,两个多次项的系数,再输入多次项的常数项和次数。
最后打印输出升序降序的结果
例
代码如下:
#include
#include
using namespace std;

class node{
public:
int coe;
int index;

node(int coeValue,int indexValue)
{
	coe = coeValue;
	index = indexValue;
}
node(const node& it)
{
	this->coe = it.coe;
	this->index = it.index;
}
node()
{
	coe = index = 0;
}

void operator =(const node& it)
{
	this->coe = it.coe;
	this->index = it.index;
}

};

template class Link{
private:
static Link* freelist;
public:
E element;
Link* next;
Link* prev;

	Link(const E& it, Link* prevp, Link* nextp)
	:element(it),
	prev(prevp),
	next(nextp)
	{
		
	}
	
	Link(Link* prevp = NULL,Link* nextp = NULL)
	:prev(prevp),
	next(nextp)
	{
		
	}
	
	void* operator new(size_t)
	{
		if(freelist == NULL)
			return ::new Link;
		
		Link<E>* temp = freelist;
		freelist = freelist->next;
		return temp;
	}
	
	void operator delete(void* ptr)
	{
		((Link<E>*)ptr)->next = freelist;
		freelist = (Link<E>*)ptr;
	}

};

template
Link* Link::freelist = NULL;

template class Dlist{
private:
Link* head;
Link* tail;
Link* curr;
int cnt;
public:
Dlist()
{
curr = head = new Link;
tail = new Link(head,NULL);
head->next = tail;
cnt = 0;
}

void insert(const E& it)
{
	curr->next = curr->next->prev = 
		new Link<E>(it,curr,curr->next);
	
	cnt++;
}

void append(const E& it)
{
	tail->prev = tail->prev->next = 
		new Link<E>(it,tail->prev,tail);
	cnt++;	
	
}

E remove()
{
	if(curr->next!=tail)
	{
	
	E it = curr->next->element;
	Link<E>* ltemp = curr->next;
	curr->next->next->prev = curr;
	curr->next = curr->next->next;
	delete ltemp;
	cnt--;
	return it;
	}
}

void prev()
{
	if(curr->prev != head)
		curr = curr->prev;
}

void next()
{
	if(curr->next != tail)
		curr = curr->next;
}

int currPos()
{
	Link<E>* temp = head;
	int i;
	for(i = 0;temp != curr;i++)
		temp = temp->next;
	return i;
}
void moveToPos(int pos)
{
	if(pos <0 && pos > cnt)
		cerr << "Position out of range" << endl;
	curr = head;
	for(int i = 0;i<pos;i++)
		curr = curr->next;
}

void moveToStart() 
{
	curr = head;
}

void moveToEnd()
{
	curr = tail;
}

E& getValue()
{
	if(curr->next != tail)
		return curr->next->element; 
}
E& getPrevValue()
{
	if(curr->prev != head)
		return curr->prev->element; 
}

E& getTwoValue()
{
	if(curr->next->next != tail)
		return curr->next->next->element;
}

int length()
{
	return cnt;
}

void sort()
{
	Link<E>* p;
	Link<E>* q;
	
	E temp;
	for(p = head->next;p->next != tail;p = p->next)
		{
			for(q = p->next;q != tail;q= q->next)
				{
					if(p->element.index > q->element.index)
					{
						temp = p->element;
						p->element = q->element;
						q->element = temp;				
					}
				}
		}
} 

};

int main()
{
int m,n;
cin >> m >> n;

int coeValue;
int indexValue;
Dlist<node> A;
Dlist<node> B;
Dlist<node> res;

for(int i = 0;i<m;i++)
{
	cin >> coeValue >> indexValue;
	node temp(coeValue,indexValue);
	A.append(temp);
}

for(int j = 0;j<n;j++)
{
	cin >> coeValue >> indexValue;
	node temp(coeValue,indexValue);
	B.append(temp);
}//读入链表 

A.sort();
B.sort();//排序 

A.moveToStart();
B.moveToStart();

while(A.length()>0)//两个链表合并 (将A插入到B中) 
{
	if(A.getValue().index < B.getValue().index || B.currPos() == B.length())
		B.insert(A.remove());
		
	B.next();
}


B.moveToStart();//合并指数相同项 
while(B.currPos() != B.length())
{
	node temp1 = B.getValue();
	node temp2 = B.getTwoValue();

	if(temp1.index == temp2.index)
	{
		node res(temp1.coe+temp2.coe,temp1.index);
		B.remove();
		B.remove();
		B.insert(res);
	}
	else
	{
		B.next();
	}
}

B.moveToStart();//正序输出 
node a;
while(B.currPos() !=B.length())
{
		if(B.currPos()!=B.length()-1)
		{
			a = B.getValue();
			cout <<a.coe << "x^"  << a.index<<"+";
			B.next();
		}
		else
		{
			a = B.getValue();
			cout <<a.coe << "x^"  << a.index<<endl;
			B.next();
		}
}

B.moveToEnd();//反序输出 
while(B.currPos() !=1)
{
		if(B.currPos()!= 2)
		{
			a = B.getPrevValue();
			cout <<a.coe << "x^"  << a.index<<"+";
			B.prev();
		}
		else
		{
			a = B.getPrevValue();
			cout <<a.coe << "x^"  << a.index<<endl;
			B.prev();
		}
}	

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值