ImageVerifierCode 换一换
格式:DOCX , 页数:22 ,大小:88.29KB ,
资源ID:8945045      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8945045.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(线性表及多项式操作.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

线性表及多项式操作.docx

1、线性表及多项式操作线性表及多项式操作实 验 报 告实验名称线性表及多项式的运算指导教师邹志强实验类型验证实验学时2+2实验时间2016.9.16一、实验目的和要求1.掌握线性表的两种基本存储结构及其应用场合:顺序存储和链接存储。2.掌握顺序表和链表的各种基本操作算法。3.理解线性表应用于多项式的实现算法。二、实验环境(实验设备)Dev-C+三、实验原理及内容内容:1.参照程序2.1程序2.7,编写程序,完成顺序表的初始化、查找、插入、删除、输出、撤销等操作。2.已知代表头节点的单链表的类型定义,参照程序2.8程序2.14,编写程序,完成带表头节点的单链表的初始化、查找、插入、删除、输出、撤销等

2、操作。3.以第2题所示带表头节点的单链表为例,编写程序实现单链表的逆置操作(原单链表为(a0,a1,.an-1),逆置后为(an-1,an-2,.,a0),要求不引入新的存储空间。)4.以第2题所示带表头节点的单链表为存储结构,编写程序实现将单链表排序成为有序单链表的操作。5.已知带表头节点一元多项式的类型定义,编写程序实现一元多项式的创建、输出、撤销以及两个一元多项式相加和相乘的操作。实 验 报 告三、实验过程及代码等1.顺序表的基本运算顺序表的类型定义:typedef struct int n; int maxLength; int *element;SeqList;顺序表的初始化:typ

3、edef int Status;Status Init(SeqList *L,int mSize) L-maxLength=mSize; L-n=0; L-element=(int*)malloc(sizeof(Status)*mSize); if(!L-element) / 判断顺序表是否申请成功 return ERROR; return OK;顺序表的查找Status Find(SeqList L,int i,int *x) if(iL.n-1) /越界判断 return ERROR; *x=L.elementi; return OK;顺序表的插入:Status Insert(SeqLis

4、t *L,int i,int x) int j; if(iL-n-1) return ERROR; if(L-n=L-maxLength) return ERROR; for(j=L-n-1;ji;j-) L-elementj+1=L-elementj; L-elementi+1=x; L-n+; return OK;顺序表的删除:Status Delete(SeqList *L,int i) int j; if (iL-n-1) return ERROR; if(!L-n) return ERROR; for(j=i+1;jn;j+) L-elementj-1=L-elementj; L-n

5、-; return OK;顺序表的输出:Status Output(SeqList L)/输出 int i; if(!L.n) return ERROR; for(i=0;in=0; L-maxLength=0; free(L-element);用主函数进行测试:#include #include #define ERROR 0#define OK 1int main() int i; SeqList list; Init(&list,10); for(i=0;ihead=(Node*)malloc(sizeof(Node);if(!L-head)return ERROR;L-head-lin

6、k=NULL;L-n=0;return OK;单链表的查找(Find.c):status Find(headerList L, int i,int *x)Node *p;int j;if (iL.n-1) /对i 进行越界检查return ERROR;p=L.head;for ( j=0; jlink; /从头结点开始查找ai*x=p-element; /通过x 返回ai 的值return OK;单链表的插入(Insert.c):status Insert(headerList *L,int i,int x) Node *p,*q; int j;if (iL-n-1)return ERROR;

7、p=L-head;for(j=0;jlink;q=malloc(sizeof(Node);q-element=x;q-link=p-link;p-link=q; L-n+;return OK;单链表的删除(Delate.c):status Delete(headerList *L,int i)int j;Node *p,*q;if (!L-n)return ERROR;if (iL-n-1)return ERROR;q=L-head;for (j=0; jlink;p=q-link; /p 指向aiq-link=p-link; /从单链表中删除p 所指向的结点free(p); /释放p 所指结

8、点的存储空间L-n-;return OK;单链表的输出(Output.c):status Output(headerList L)Node *p;if (!L.n) /判断顺序表是否为空return ERROR;p=L.head-link;while(p)printf(%d ,p-element);p=p-link;return OK;单链表的析构(Destroy.c):void Destroy (headerList *L) Node *p;while(L- head ) p= L- head-link;free(L-head);L- head=p;测试所用主函数(main.c): void

9、 main()int i;int x;headerList list;Init(&list); /对线性表初始化for(i=0;ihead; q=p-link; while(q) r=q-link; q-link=p; p=q; q=r; L-head-link-link=NULL; L-head-link=p; return 0;调用结果:4.单链表排序操作(冒泡)#include#includetypedef struct node int element; struct node *next;Node,*LinkList;LinkList Creat(void) /*创建链表,输入数据,

10、结束标志为当输入的数据为0*/ LinkList H,p,q; int n; n=0; p=q=(LinkList)malloc(sizeof(Node); printf(输入数据: (输入0标志着输入完成)n); scanf(%d,&p-element); H=NULL; while(p-element!=0) n=n+1; if(n=1) H=p; else q-next=p; q=p; p=(LinkList)malloc(sizeof(Node); scanf(%d,&p-element); q-next=NULL; return(H);LinkList Paixu(LinkList

11、l) /*排序*/ LinkList p,q; int temp; for(p=l;p!=NULL;p=p-next) for(q=p-next;q!=NULL;q=q-next) if(p-elementq-element) temp=q-element; q-element=p-element; p-element=temp; return l;int main() LinkList L,S,K; L=Creat(); printf(初始化的单链表为:n); for(S=L;S!=NULL;S=S-next) printf(%d ,S-element); Paixu(L); printf(

12、n按递增排序后的链表为:n); for(K=L;K!=NULL;K=K-next) printf(%d ,K-element); return 0;调用结果:5.多项式操作多项式的类型定义(struct.h):typedef struct PNode int coef; int exp; struct PNode* link; PNode;typedef struct struct PNode *head;polynominal;多项式的创建(Create.c):void Create(polynominal *p) PNode *pn,*pre,*q; p-head=(void*)mallo

13、c(sizeof(PNode); p-head-exp=-1; p-head-link=NULL; printf(请输入多项式n); for( ; ; ) pn=(void*)malloc(sizeof(PNode); printf(coef:n); scanf(%d,&pn-coef); printf(exp:n); scanf(%d,&pn-exp); if(pn-exphead; q=p-head-link; while(q&q-exppn-exp) pre=q; q=q-link; pn-link=q; pre-link=pn; 多项式的输出(Output.c):void Output

14、(polynominal L)PNode *p;p=L.head-link; while(p-link!=NULL) printf(%d*X%d+,p-coef,p-exp); p=p-link; printf(%d*X%dn,p-coef,p-exp); 多项式的析构(Destroy.h):void Destroy (polynominal *L) PNode *p; while(L- head ) p= L- head-link; free(L-head); L- head=p; 两个多项式的加法(Add.c):void Add(polynominal *px,polynominal *q

15、x) PNode *q,*q1=qx-head,*p,*temp; p=px-head-link; q=q1-link; while(p&q) while(p-expexp) q1=q;q=q-link; if(p-exp=q-exp) q-coef=q-coef+p-coef; if(q-coef=0) q1-link=q-link; free(q); q=q1-link; p=p-link; else q1=q; q=q-link; p=p-link; else temp=(void*)malloc(sizeof(PNode); temp-coef=p-coef; temp-exp=p-e

16、xp; temp-link=q1-link; q1-link=temp; p=p-link; 两个多项式的乘法(Mult.c):polynominal* Mult(polynominal *a,polynominal *b) PNode *an,*bn; polynominal temp; printf(初始化temp,输入0 0 0 -1); Create(&temp); an=a-head; bn=b-head; while(an-link) an=an-link; while(bn-link) bn=bn-link; bn-exp+=an-exp; bn-coef*=an-coef; A

17、dd(b,&temp); bn=b-head; while(bn-link) bn=bn-link; bn-exp-=an-exp; bn-coef/=an-coef; bn=b-head; return &temp;调用主函数测试:void main() polynominal listA,listB,listC,listD,temp; int choose=0; printf(请选择操作: 1.多项式相加 2.多项式相乘 n); scanf(%d,&choose); switch(choose) case 1: Create(&listA); Create(&listB); printf(

18、多项式A为: ); Output(listA); printf(多项式B为: ); Output(listB); Add(&listA,&listB); printf(n); printf(A与B相加得:); Output(listB); Destroy(&listA); Destroy(&listB); break; case 2: Create(&listC); Create(&listD); printf(多项式C为: ); Output(listC); printf(多项式D为: ); Output(listD); /Mult(&listC,&listD); Output(*Mult(

19、&listC,&listD); Destroy(&listC); Destroy(&listD); break; 运行结果:(1) 多项式加法:(2) 多项式乘法:实 验 报 告四、实验小结(包括问题和解决方法、心得体会、意见与建议等)一、前面写顺序表的时候没有遇到什么问题,只是有两个地方有点小问题但很快解决了;(1)(void*)malloc(sizeof(PNode)在malloc前面漏掉强制类型转换。(2)for(int i=1,in,i+)类似上式,有些编译器不支持for循环类定义变量名。二、刚开始写链表的代码时,总是搞不清一个链表内的成员和关系,后来一直到完成多项式加法时才对链表有了一个较为全面的认识。三、在写多项式乘法时,在网上找了一个乘法的算法,可是改来改去还是会出问题,在输出最终结果时输出的系数和指数是地址而不是数,和舍友讨论了很久也没有解决,我后来在舍友的帮助下完成了多项式的乘法;但是之前的那段没有找出问题的代码还是没有找到答案,而且目前还以注释的形式保留在源代码中。四、希望自己能够在实验和作业中有所收获,使得自己的代码水平有一定的提升。五、指导教师评语 成 绩批阅人日 期

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1