一元多项式相加

Posted 梦西空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一元多项式相加相关的知识,希望对你有一定的参考价值。


```cpp
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef int typelem; 
typedef struct node1
	int length;//表长 
	struct node2 *next; 
headlist,*head_list; //头结点
 
typedef struct node2
	typelem data1;//指数
	typelem data2;//系数 
	struct node2 *next;//指针域
nodelist;//中间结点 

int Initlist(headlist &L)//初始化一个空链表 

	L.length=0;
	L.next=NULL;
	return 1; 
 
int paixu(headlist *L)//将链表的数据按降序排列 ,失败返回0 

	if(!L)
	return 0;
	int i, j;nodelist *p,*p1;
    typelem t;
    for(i=0;i<L->length-1;i++)//冒泡排序
		
	    p=L->next;
	    p1=p->next;
		for(j=0;j<L->length-1-i;j++)
		
			if(p->data1<p1->data1) 
			
				t=p->data1;
				p->data1=p1->data1;
				p1->data1=t;
				t=p->data2;
				p->data2=p1->data2;
				p1->data2=t;
			
			p=p->next;
			p1=p1->next;
		
	 
	 return 1; 

nodelist *find(headlist *L,typelem e1)//判断L中是否存在e1,在就返回地址指针,不在返回0

	 nodelist *p1=L->next;
	 while(p1)
	 
	 	  if(p1->data1==e1)
	 	  break;
	 	  p1=p1->next;
	 
	 if(p1==NULL)
	 return NULL;
	 else
	 return p1;
 
int Listsert(headlist *p,typelem e1,typelem e2)//在表中插入元素

	if(!p)
	return 0;
	nodelist *p1;
	p1=(nodelist *)malloc(sizeof(nodelist));
	p1->data1=e1;p1->data2=e2;
	p->length++;
	p1->next=p->next;
	p->next=p1;
	return 1;

int Listcreate(headlist *L)//给链表赋值 

	if(!L)
	return 0;
	typelem e1,e2;int len,i;
	cout<<"Input length:";
	cin>>len;
	for(i=0;i<len;i++)
	   cout<<"input data1(指数):";
	 	cin>>e1;
	 	cout<<"input data2(系数):";
		cin>>e2;
		if(find(L,e1)) 
		
	 	    cout<<"指数已存在,请重新输入!\\n";
	 	    i--;
	 	
		else
		Listsert(L,e1,e2);
	
	paixu(L);
	return 1;
  
int Listdelete(headlist *L,typelem e1)//删除表中data1为e1的结点,失败则返回0 

	if(!L||L->length==0)
	return 0;
	if(L->next->data1==e1)
	
		nodelist *p6=L->next;
		L->next=p6->next;
		free(p6);
		L->length--;
		return 1;
	
	if(L->length==1)
	
		return 0;
	
	nodelist *p,*p1;
	p=L->next;
	p1=p->next;
	while(p1->data1!=e1)
	   
		p=p->next;
		p1=p1->next;
        if(p1==NULL)
        return 0;
	
	p->next=p1->next;
	L->length--;
	free(p1);
	return 1;

int Listadd(headlist *L1,headlist *L2,headlist *L3)//(L1,L2按指数递增小的序列)将L2,L1两个多项式加到L3

	if(!(L1&&L2&&L3))
	return 0;
	if(L3->length||L1->length==0||L2->length==0)//保证L3为空表,L1,L2不为空表 
	return 0;
	nodelist *p1,*p2,*p3,*p4;
	p1=L1->next;p2=L2->next;
	while(!(p1==NULL&&p2==NULL))
	
		p4=(nodelist *)malloc(sizeof(nodelist));
		if(p2==NULL)
		
			goto look1;
		
		else if(p1==NULL)
		
			goto look2;
		
		else if(p1->data1>p2->data1)
		look1:
			p4->data1=p1->data1;
			p4->data2=p1->data2;
			p1=p1->next;
		
		else if(p1->data1<p2->data1)
		look2:
			p4->data1=p2->data1;
			p4->data2=p2->data2;
			p2=p2->next;
		
		else 
		
			p4->data1=p1->data1;
			p4->data2=p1->data2+p2->data2;
			p1=p1->next;p2=p2->next;
		
		if(L3->length==0)
		L3->next=p4;
		else
		
			p3->next=p4;
		
		p3=p4;
		L3->length++;
	
	p3->next=NULL;
	return 1;
 

/*(int Listadd2(headlist *L1,headlist *L2,headlist *L3)//(L1,L2按指数递增加的序列)将L2,L1两个多项式加到L3

	if(!(L1&&L2&&L3))
	return 0;
	if(L3->length||L1->length==0||L2->length==0)//保证L3为空表,L1,L2不为空表 
	return 0;
	nodelist *p1;
	L1=L1->next;L2=L2->next;
	while(!(L1==NULL&&L2==NULL))
	
		p1=(nodelist *)malloc(sizeof(nodelist));
		if(L2==NULL)
		
			p1->data1=L1->data1;
			p1->data2=L1->data2;
			L1=L1->next;
		
		else if(L1==NULL)
		
			p1->data1=L2->data1;
			p1->data2=L2->data2;
			L2=L2->next;
		
		else if(p1->data1>p2->data1)
		
			p1->data1=L1->data1;
			p1->data2=L1->data2;
			L1=L1->next;
		
		else if(p1->data1<p2->data1)
		
			p1->data1=L2->data1;
			p1->data2=L2->data2;
			L2=L2->next;
		
		else //合并同类项 
		
			p1->data1=L1->data1;
			p1->data2=L1->data2+L2->data2;
			L1=L1->next;L2=L2->next;
		
		p1->next=L3->next;
		L3->next=p1;
		L3->length++;
	
	return 1;
*/
void print(headlist *L)

	nodelist *p1=L->next;int j=1;
	while(p1!=NULL)
	
		cout<<j++<<".data1:"<<p1->data1<<"   "<<"data2:"<<p1->data2<<'\\n';
		p1=p1->next;
	

void MergeList(LinkList la, LinkList lb, LinkList lc)//lc已经在主函数初始化 

	LNode *p;
	while(la != null && lb != null)
		p = (LNode *) malloc (sizeof(LNode));		
		if(la->data > lb->data)
			p->data = lb->data;
			lb = lb->next;
		
		else
			p->data = la->data;
			la = la->next;
		
		p->next=lc->next;
		lc->next=p;	
	 
	while(la != null)
		p = (LNode *) malloc (sizeof(LNode));
		p->data = la->data;
		la = la->next;
		lc->next = p;
		lc = p;	
	
	while(lb != null)
		p = (LNode *) malloc (sizeof(LNode));
		p->data = lb->data;
		la = lb->next;
		lc->next = p;
		lc = p;	
	

int main()

	headlist L1,L2,L3;
	Initlist(L1);Initlist(L2);Initlist(L3);
	Listcreate(&L1);Listcreate(&L2);
	Listadd2(&L1,&L2,&L3);
	print(&L3);
  

以上是关于一元多项式相加的主要内容,如果未能解决你的问题,请参考以下文章

实现一元多项式

链表实现一元多项式相加

两个一元多项式相加

一元多项式相加(链式实现)

一元多项式的表示及相加

第二章:5.线性表应用---一元多项式的表示及相加