用单链表实现多项式运算(ANSI C)

1、多项式结构定义polynomial.h

#ifndef _polynomial_h
#define _polynomial_h

struct poly_term
{
	int expn;
	int coef;
};

typedef struct poly_term poly_term;
typedef poly_term * poly_term_ptr;

struct poly_node;
typedef struct poly_node poly_node;

struct poly_node
{
	poly_term term;
	poly_node * next;
};

typedef struct poly_node poly_node;
typedef poly_node * poly_node_ptr;
typedef poly_node * polynomial;

void create_polyn( polynomial * polyn, int n_order );
void free_polyn( polynomial * polyn );

void print_polyn( polynomial polyn );
int polyn_order( polynomial polyn );

void add_polyn( polynomial * pa, polynomial * pb );
void neg_polyn( polynomial * polyn );
void multiply_polyn( polynomial * pa, polynomial * pb );

int locate_poly_term( polynomial polyn, poly_term term );
void order_insert_term( polynomial * polyn, poly_term term );

#endif

2、多项式的实现

#include "polynomial.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

void create_polyn( polynomial * polyn, int n_order )
{
	int coef, expn;
	poly_node_ptr h = NULL, s = NULL, p = NULL;
	assert( n_order >= 0 );

	h = ( poly_node * )malloc( sizeof( poly_node ) );
	assert( h != NULL );
	h->next = NULL;
	p = h;

	while( n_order-- > 0 )
	{
		s = ( poly_node_ptr )malloc( sizeof( poly_node ) );
		s->next = NULL;
		assert( s != NULL );
		scanf( "%d %d", &s->term.coef, &s->term.expn );
		if( s->term.coef == 0 ) 
			free( s );
		else
		{
			p->next = s;
			p = s;
		}
	}
	*polyn = h;
}

void free_polyn( polynomial * polyn )
{
	poly_node_ptr h = *polyn, p = h->next;
	while( p != NULL )
	{
		h->next = p->next;
		free( p );
		p = h->next;
	}
	free( h );
	polyn = NULL;
}

void print_polyn( polynomial polyn )
{
	poly_node_ptr h = polyn, p = h->next;
	while( p != NULL )
	{
		printf( "(%d, %d), ", p->term.coef, p->term.expn );
		p = p->next;
	}
	putchar( '\n' );
}

int polyn_order( polynomial polyn )
{
	int n_order = 0;
	poly_node_ptr p = polyn->next;
	while( p != NULL )
	{
		++n_order; p = p->next;
	}
	return n_order;
}

void neg_polyn( polynomial * polyn )
{
	poly_node_ptr h = *polyn, p = h->next;
	while( p != NULL )
	{
		p->term.coef *= -1;
		p = p->next;
	}
}

int term_equal( poly_term t1, poly_term t2 )
{
	return ( t1.coef == t2.coef ) && ( t1.expn == t2.expn );
}

int locate_poly_term( polynomial polyn, poly_term term )
{
	int pos = 1;
	poly_node_ptr h = polyn, p = h->next;
	while( p != NULL && !term_equal( term, p->term ) )
	{
		++pos; p = p->next;
	}
	return p == NULL ? -1 : pos;
}

void order_insert_term( polynomial * polyn, poly_term term )
{
	poly_node_ptr p = *polyn, q = NULL;
	while( p->next != NULL && term.expn > p->next->term.expn )
		p = p->next;
	if( p->next != NULL && p->next->term.expn == term.expn )
	{
		if( p->next->term.coef + term.coef != 0 )
			p->next->term.coef += term.coef;
		else
		{
			q = p->next;
			p->next = q->next;
			free( q );
		}
	}
	else 
	{
		q = ( poly_node_ptr )malloc( sizeof( poly_node ) );
		assert( q != NULL );
		q->term = term;
		q->next = p->next;
		p->next = q;
	}
}

void add_polyn(  polynomial * poly_a, polynomial * poly_b )
{
	poly_node_ptr ha = *poly_a, pa = ha, hb = *poly_b, pb = hb->next, q;
	
	while( pa->next != NULL && pb != NULL )
	{
		if( pa->next->term.expn < pb->term.expn )
			pa = pa->next;
		else if( pa->next->term.expn > pb->term.expn )
		{
			hb->next = pb->next; pb->next = pa->next;
			pa->next = pb; pb = hb->next; pa = pa->next;
		}
		else
		{
			if( pa->next->term.coef + pb->term.coef != 0 )
			{
				pa->next->term.coef += pb->term.coef;
				pa = pa->next;
			}
			else
			{
				q = pa->next;
				pa->next = q->next;
				free( q );				
			}
			hb->next = pb->next;
			free( pb );
			pb = hb->next;
		}
	}

	while( pb != NULL )
		pa->next = pb;
}


你可能感兴趣的:(用单链表实现多项式运算(ANSI C))