关闭

一元多项式链表实现

131人阅读 评论(0) 收藏 举报
分类:

链表模拟多项式相加,相乘,相减。。。

#include <stdio.h>
#include <stdlib.h>

typedef struct tag_node{
	int p;
	int e;
	struct tag_node *next;
}node;

//链表创建,返回头结点指针
node* CreatPolyn(int m)
{
	node *list = (node*)malloc(sizeof(node));
	if (list==NULL)
	{
		printf("Memory Allocate Error!\n");
		exit(-1);
	}

	list->e =-1;
	list->p = -1;
	list->next = NULL;
	
	int i, a, b;
	for (i=0; i<m; i++)
	{
		scanf("%d%d", &a, &b);
		node *tmp = (node*)malloc(sizeof(node));	
		if (tmp==NULL)
		{
			printf("Memory allocate Error!\n");
			exit(-1);
		}
		tmp->p = a;
		tmp->e = b;
		tmp->next = list->next;
		list->next = tmp;
	}
	return list;
}

//销毁链表
void DestroyPolyn(node *list)
{
	node *tmp, *pos = list;
	while (pos)
	{
		tmp = pos;
		pos = pos->next;
		free(tmp);
	}
}

//打印链表
void PrintPolyn(node *list)
{
	printf("\n");
	printf("f(x) = ");
	node *pos = list->next;
	int flag = 0;
	while (pos)
	{
		if (pos->p!=0)
		{
			if (flag) printf("%+dX%d", pos->p, pos->e);
			else
				printf("%dX%d", pos->p, pos->e);
			flag = 1;
		}	
		pos = pos->next;
	}
	printf("\n\n");
}

//链表排序
void SortPolyn(node *list)
{
    node *ha = (node *)malloc(sizeof(node));
    if (ha==NULL)
	{
		printf("Memory Allocate Error!\n");
		exit(-1);
	}
    ha->next = NULL;

    node *pos = list->next;
    while (pos)
    {
        node *pa = ha;
        while ((pa->next!=NULL) && (pa->next->e)>(pos->e))
        {
            pa = pa->next;
        }
		if ((pa->next!=NULL) && pa->next->e==pos->e)
		{
			pa->next->p = pos->p + pa->next->p;
			list->next = pos->next;
			free(pos);
		}
		else{
			list->next = pos->next;
			pos->next = pa->next;
			pa->next = pos;
		}
        pos = list->next;
    }
    list->next = ha->next;
    ha->next = NULL;
    free(ha);
}

//链表相加,结果为pa并销毁pb(忘记pb会销毁导致乘法调试很久)
void AddPolyn(node *pa, node *pb)
{
	node *tmp, *pre_a;
	node *hb = pb;
	pre_a = pa; 
	pa = pa->next; 
	pb = pb->next;

	while (pa && pb)
	{
		if (pa->e > pb->e)
		{
			pre_a = pa;
			pa = pa->next;
		}
		else if (pa->e == pb->e)
		{
			pa->p = pa->p+pb->p;
			pre_a = pa;
			tmp = pb;
			pb = pb->next;
			pa = pa->next;
			free(tmp);
		}
		else
		{
			tmp = pb->next;
			pb->next = pa;
			pre_a->next = pb;
			pre_a = pb;
			pb = tmp;		
		}
	}

	if (pb)
	{
		pre_a->next = pb;
	}

	free(hb);
}

//链表相减, 并把结果保存在pa,pb销毁
void SubtractPolyn(node *pa, node *pb)
{
	node *tmp, *pre_a;
	node *hb = pb;
	pre_a = pa; 
	pa = pa->next; 
	pb = pb->next;

	while (pa && pb)
	{
		if (pa->e > pb->e)
		{
			pre_a = pa;
			pa = pa->next;
		}
		else if (pa->e == pb->e)
		{
			pa->p = pa->p-pb->p;
			pre_a = pa;
			tmp = pb;
			pb = pb->next;
			pa = pa->next;
			free(tmp);
		}
		else
		{
			pb->p = -(pb->p);
			tmp = pb->next;
			pb->next = pa;
			pre_a->next = pb;
			pre_a = pb;
			pb = tmp;		
		}
	}

	if (pb)
	{
		pre_a->next = pb;
	}

	free(hb);
}

//链表相乘
void MultiplyPolyn(node *pa, node *pb)
{
	node *ha = pa, *hb = pb;
	pa = pa->next;
	pb = pb->next;
	
	node *p_sum, *p_add, *tmp, *p1, *p2, *pos;

	p_sum = (node *)malloc(sizeof(node));
	p_sum->next = NULL;

	for (p1=pa; p1!=NULL; p1=p1->next)
	{
		p_add = (node *)malloc(sizeof(node));
		p_add->next = NULL;
		pos = p_add;

		for (p2=pb; p2!=NULL; p2=p2->next)
		{	
			tmp = (node *)malloc(sizeof(node));
			tmp->p = p1->p * p2->p;
			tmp->e = p1->e + p2->e;
			tmp->next = NULL;
			pos->next = tmp;
			pos = pos->next;
		}

		AddPolyn(p_sum, p_add);
	}

	DestroyPolyn(ha->next);
	DestroyPolyn(hb);
	ha->next = p_sum->next;
}

int main(void)
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	node *list_a, *list_b;
	int m, n;
	printf("输入m, n(m项a表,n项b表):\n");
	
	while (scanf("%d%d", &m, &n)!=EOF)
	{
		printf("输入%d项a表(系数+空格+指数):\n", m);
		list_a = CreatPolyn(m);
		
		printf("输入%d项b表(系数+空格+指数):\n", n);
		list_b = CreatPolyn(n);
		
		SortPolyn(list_a);
		SortPolyn(list_b);

		printf("输入命令:\n");
		printf("1.相加\n2.相减\n3.相乘\n");
		int com;
		scanf("%d", &com);
		printf("a表: ");
		PrintPolyn(list_a);
		printf("b表: ");
		PrintPolyn(list_b);
		switch (com)
		{
		case 1: AddPolyn(list_a, list_b);break;
		case 2: SubtractPolyn(list_a, list_b);break;
		case 3: MultiplyPolyn(list_a, list_b);break;
		}
		printf("结果: ");
		PrintPolyn(list_a);
		printf("输入m, n(m项a表,n项b表):\n");
		DestroyPolyn(list_a);
	}
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5981次
    • 积分:250
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条