一元多项式加法链表实现

一元多项式加法的链表实现

作者:Duder

1.要求

使用链表实现一元多项式加法
并使用已有链表返回运算结果

2.算法分析

1.幂次有序算法

//一元多项式求和
void SumList(SList* &A,SList* B)
{
	SList *m,*n,*t;
	if(GetLine(A) > GetLine(B) )	
		{m=A;n=B;}
	else 
		{m=B;n=A;}
	//OutPut(m);OutPut(n);
	t = m->next;
	
	while(m->next != NULL && n->next != NULL)
	{
		if(m->next->exp == n->next->exp)
		{
			m->next->coef = m->next->coef + n->next->coef;
			m = m->next;n = n->next;
		}
		else if(m->next->exp < n->next->exp )
		{
			m=m->next;
		}
		else if(m->next->exp > n->next->exp)
		{
			n=n->next;
			Insert(m,n);
		}
	}
			
	while(n != NULL)
	{
		m->next = n->next;
		m = m->next;
		n = n->next;
	}
	
	A->next= t;
}

输入函数A:
f ( x ) = 1 + 2 x 1 + 3 x 3 + 4 x 5 f(x) = 1+ 2x^1 + 3x^3 + 4x^5 f(x)=1+2x1+3x3+4x5
输入函数B:
f ( x ) = 2 + x 2 f(x) = 2 + x^2 f(x)=2+x2

运行结果
一元多项式加法链表实现_第1张图片

2.使用指针的有序相加算法

//一元多项式求和
void SumList(SList* &A,SList* B)
{
	SList *m,*n,*t,*p;
	if(GetLine(A) > GetLine(B) )	
		{m=A;n=B;}
	else 
		{m=B;n=A;}
	t = m->next;
	
	while(m->next != NULL && n->next != NULL)
	{
		if(m->next->exp == n->next->exp)
		{
			m->next->coef = m->next->coef + n->next->coef;
			m = m->next;n = n->next;
		}
		else if(m->next->exp < n->next->exp )
		{
			m=m->next;
		}
		else if(m->next->exp > n->next->exp)
		{
			p = n->next->next;
			n->next->next = m->next;
			m->next = n->next;
			n->next = p; 
		}
	}	
	while(n != NULL)
	{
		m->next = n->next;
		m = m->next;
		n = n->next;
	}
	
	A->next= t;
}

3.乱序幂次算法

//冒泡排序
void SortList(SList *head) 
{
    SList *p,*q,*t;
    for(p=head->next; p->next!=NULL; p=p->next)
	{
        for(q=p->next; q!=NULL; q=q->next)
        {
			if (p->exp > q->exp)
            {
                int t = p->coef; 
				p->coef = q->coef; 
				q->coef = t;
				
                t = p->exp;
				p->exp = q->exp;
				q->exp = t;
            }
            else {}
		}
	}
}

输入函数A:
f ( x ) = 1 x + 2 x 2 + 3 x 3 + 4 x 4 f(x) = 1x + 2x^2 +3x^3 + 4x^4 f(x)=1x+2x2+3x3+4x4

输入函数B:
f ( x ) = 5 x 5 + 4 x 7 + 5 x 2 f(x) = 5x^5 + 4x^7 + 5x^2 f(x)=5x5+4x7+5x2

一元多项式加法链表实现_第2张图片

3.完整代码

func.h

#include 
#include 

using namespace std;
//#define MaxSize 20
typedef int ElemType;

//定义链表结构 
typedef struct SList{
	ElemType coef;
	int exp;
	struct SList *next;
}SList;

//链表的初始化
void InitList(SList* &head,int n);

//输出链表 
void OutPut(SList *head);

//释放链表 
void FreeList(SList *head);

//返回第n个节点的指针 
SList* GetPoint(SList* head);

//一元多项式求和
void SumList(SList* &A, SList* B);

//返回表长度 
int GetLine(SList* head);

//插入某处某数据
void Insert(SList* &m,SList* n);

void SortList(SList *head) ;

func.cpp

#include "func.h"
//#define MaxSize 20
//链表的初始化
void InitList(SList* &head,int n) 
{
	cout<<"please input the function:"<> node->coef;
		cin >> node->exp;
		end->next = node;
		end = node;
	}
	end->next = NULL; 
	cout << "The input is over"<next;
	while(p != NULL)
	{
		cout <coef<<"x^"<exp<<" ";
		p = p->next; 
	}
	cout<next != NULL)
	{
		t = p->next;
		p->next = t->next;
		free(t);
		t=NULL;
	}	
	else
		cout << "该链表不存在"; 
	cout << endl;
}


int GetLine(SList *head)
{
	SList *p = head;
	SList *q;
	int i=0;
	while(p->next != NULL)
	{
		p=p->next;
		i++;
	}
	if(i == 0)	cout << "该链表不存在"< GetLine(B) )	
		{m=A;n=B;}
	else 
		{m=B;n=A;}
	//OutPut(m);OutPut(n);
	t = m->next;
	
	while(m->next != NULL && n->next != NULL)
	{
		if(m->next->exp == n->next->exp)
		{
			m->next->coef = n->next->coef + n->next->coef;
			m = m->next;n = n->next;
		}
		else if(m->next->exp < n->next->exp )
		{
			m=m->next;
		}
		else if(m->next->exp > n->next->exp)
		{
			n=n->next;
			Insert(m,n);
		}
	}
			
	while(n != NULL)
	{
		m->next = n->next;
		m = m->next;
		n = n->next;
	}
	
	A->next= t;
}

//将n指针指向的数据插入到m指针后 
void Insert(SList* &m,SList* n)
{
	SList *note;
	note = (SList*)malloc(sizeof(SList));
	note->coef = n->coef;
	note->exp = n->exp;
	note->next = m->next;
	m->next = note;
}


//冒泡排序 节点值交换
void SortList(SList *head) 
{
    SList *p,*q,*t;
    for(p=head->next; p->next!=NULL; p=p->next)
	{
        for(q=p->next; q!=NULL; q=q->next)
        {
			if (p->exp > q->exp)
            {
                int t = p->coef; 
				p->coef = q->coef; 
				q->coef = t;
				
                t = p->exp;
				p->exp = q->exp;
				q->exp = t;
            }
            else {}
		}
	}
}

main.cpp

#include 
#include "func.h"
int main() 
{
	
	SList *A,*B;
	//FreeList(A);FreeList(B); 
	InitList(A,4);InitList(B,3);
	//cout<

你可能感兴趣的:(计算机算法)