尾插法建立一元多项式的链表

更新时间:2023-12-09 14:34:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验报告

2009级 数学与应用数学班 2011年11月19日 姓名: 学号: 1、 实验目的

用尾插法建立一元多项式的链表,验证是否实现,并进行两个多项式的相加运算 2、 需求分析

本程序用TC编写,完成尾插法一元多项式的链表,并验证,及完成相加运算。

① 输入的形式和输入值的范围:输入多项式每一项的系数、指数,输入的所有系数

和指数均为整数。 ② 输入的形式:输出多项式各项的系数、指数。

③ 程序所能达到的功能:完成单链表的建立,及相加操作。 ④ 测试数据:1+1

多项式1: 2x^0+3x^3+7x^5 多项式2: 4x^1+3x^3+-4x^5

3、 概要设计

本程序有两个程序段:

a. 主函数 main;

b. 创建多项式函数 polycreate; c. 多项式相加函数 polyadd;

d. 多项式输出函数 print;

4、 详细设计 #include #define OK 1; #define error 0; #define TURE 1; #define FALSE 0;

typedef struct Polynode {

int coef; int exp;

struct Polynode *next; }Polynode ,*Polylist; Polylist polycreate() {

Polynode *head,*rear,*s; int c,e;

head=(Polynode*)malloc(sizeof(Polynode)); rear=head;

scanf(\ while(c!=0) {

s=(Polynode*)malloc(sizeof(Polynode)); s->coef=c; s->exp=e;

rear->next=s;

rear=s; scanf(\ }

rear->next=NULL;

return(head); } main() {

printf(\

用尾插法建一个一元多项式链表

Void polyadd(Polylist polya,Polylist polyb) {

Polynode *p,*q,*tail,*temp; int sum;

P=polya->next; q=polyb->next; tail=polya; While(p!=NULL&&q!=NULL) {

If(p->expexp)

{tail->exp=p;tail=p;p=p->next;} Else if(p->exp==q->exp)

{sum=p->coef+q->coef; If(sum!=0)

{p->coef=sum;tail->next=p;tail=p;

P=p->next;temp=q;q=q->next;free(temp);} else

{temp=p;p=p->next;free(temp); Temp=q;q=q->next;free(temp); } }

else

{tail->next=q;tail=q;q=q->next;} }

If (p!=NULL) tail->next=p; else tail->next=q; }

多项式相加函数 Void printf (polylist p) {

While (p!=NULL)

{printf (“%dx^%d+”,p->exp); P=p->next;} }

多项式输出函数

5、 测试结果 2 2,0

3,3 7,5 0,0 4,1 3,3 -4,5 0,0

2x^0+4x^1+6x^3+3x^5

本文来源:https://www.bwwdw.com/article/lnc5.html

Top