数据结构C语言版(清华大学_唐国民_第3版)多项式加法_循环链表方法

在这里插入图片描述

//数据结构C语言版(清华大学_唐国民_第3版)
//循环链表,书 P38 例子2.5.2 三元多项式加法,与书上代码有些许出入,不用在意,重要的是思路
//多项式按指数降序排序,这里的降序排序是指x,y,z分别对应的指数i,j,k,按i*100+j*10+k的和来降序排序
#include <stdio.h>
#include <stdlib.h>

typedef struct LNode* List;
struct LNode{
	int coef;
	int index;
	List Next;
};

List Create(List );
void Add(List,List);
void Printout(List);

int main()
{
	List Ptrl_p,Ptrl_q;
	Ptrl_p=Create(Ptrl_p);
	Ptrl_q=Create(Ptrl_q);
	Add(Ptrl_p,Ptrl_q);
	Printout(Ptrl_q);
	return 0;
}

List Create(List Ptrl)
{
	int n,i;
	List Pre,Cur;
	Ptrl=(List)malloc(sizeof(struct LNode));
	Pre = Ptrl;
	Ptrl->coef=0;
	Ptrl->index=-1;
	printf("How many numbers dou you want to create?  ");
	fflush(stdin);
	scanf("%d",&n);
	for(i=1;i<=n;++i){
		List Cur=(List)malloc(sizeof(struct LNode));
		printf("The %dth's coef is:\t",i);
		fflush(stdin);
		scanf("%d",&Cur->coef);
		printf("The %dth's xyz index is:\t",i);
		fflush(stdin);
		scanf("%d",&Cur->index);
		Pre->Next=Cur;
		Cur->Next=Ptrl;
		Pre=Cur;
	}
	return Ptrl;
}

void Add(List Ptrl_p,List Ptrl_q)
{
	
	List p,q,Pre_q,Crea_q;
	p=Ptrl_p->Next;
	q=Ptrl_q->Next;
	Pre_q=Ptrl_q;
	while(p->index != -1){
		while(p->index < q->index){
			q=q->Next;
			Pre_q=Pre_q->Next;
		}
		if(p->index==q->index){
			q->coef=p->coef + q->coef;
			if(q->coef == 0){
				Pre_q->Next = q->Next;
				free(q);
				q=Pre_q->Next;
				p=p->Next;
			}
			else{
				p=p->Next;
				q=q->Next;
				Pre_q=Pre_q->Next;
			}
		}
		else{
			Crea_q=(List)malloc(sizeof(struct LNode));
			Crea_q->coef=p->coef;
			Crea_q->index=p->index;
			Pre_q->Next=Crea_q;
			Crea_q->Next=q;
			p=p->Next;
			Pre_q=Pre_q->Next;
		}
	}
}

void Printout(List Ptrl_q){
	int i=0;
	List Rear=Ptrl_q->Next;
	while(Rear->index != -1){
		i++;
		printf("The %dth: coef is %d\t index of xyz is %d\n",i,Rear->coef,Rear->index);
		Rear=Rear->Next;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用链表来实现一元多项式加法,以下是示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义多项式节点结构体 typedef struct PolyNode { int coef; // 系数 int expon; // 指数 struct PolyNode *next; // 指向下一个节点的指针 } PolyNode, *Polynomial; // 创建一个新的多项式节点 PolyNode *createNode(int coef, int expon) { PolyNode *node = (PolyNode *)malloc(sizeof(PolyNode)); node->coef = coef; node->expon = expon; node->next = NULL; return node; } // 将多项式插入到链表中 void insert(Polynomial *p, int coef, int expon) { PolyNode *node = createNode(coef, expon); if (*p == NULL) { *p = node; } else { PolyNode *cur = *p; while (cur->next != NULL) { cur = cur->next; } cur->next = node; } } // 一元多项式加法 Polynomial add(Polynomial p1, Polynomial p2) { Polynomial result = NULL; PolyNode *cur1 = p1, *cur2 = p2; while (cur1 != NULL && cur2 != NULL) { if (cur1->expon > cur2->expon) { insert(&result, cur1->coef, cur1->expon); cur1 = cur1->next; } else if (cur1->expon < cur2->expon) { insert(&result, cur2->coef, cur2->expon); cur2 = cur2->next; } else { int sum = cur1->coef + cur2->coef; if (sum != 0) { insert(&result, sum, cur1->expon); } cur1 = cur1->next; cur2 = cur2->next; } } while (cur1 != NULL) { insert(&result, cur1->coef, cur1->expon); cur1 = cur1->next; } while (cur2 != NULL) { insert(&result, cur2->coef, cur2->expon); cur2 = cur2->next; } return result; } // 打印多项式 void print(Polynomial p) { if (p == NULL) { printf("0\n"); return; } PolyNode *cur = p; while (cur != NULL) { printf("%dX^%d", cur->coef, cur->expon); if (cur->next != NULL) { printf(" + "); } cur = cur->next; } printf("\n"); } int main() { Polynomial p1 = NULL, p2 = NULL; insert(&p1, 3, 5); insert(&p1, 2, 3); insert(&p1, 1, 1); print(p1); insert(&p2, 2, 4); insert(&p2, 4, 3); insert(&p2, 2, 1); print(p2); Polynomial result = add(p1, p2); print(result); return 0; } ``` 以上代码实现了一元多项式加法,使用了链表数据结构存储多项式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值