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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

数据结构习题.docx

1、数据结构习题 数据结构习题与实验目录习题一 1习题二 4习题三 7一元多项式之和实验 8哈夫曼树实验 13求最小生成树算法实验 17拓扑排序算法 22求最短路径(迪接斯特算法) 28关键路径 35快速排序 43习题一、请设计一算法:已知顺序表L,表中元素为整型且递增有序,现有一值为e的元素要插入L表,使插入后L表仍然有序。、已知L为非递减的顺序表,请设计算法删除L中重复的元素(即删除后使L表变为一递增表)。#include#include#include#define LIST_SIZE 100#define OK 1typedef struct int *elem; int length;

2、int listsize;SqList;int InitList_Sq(SqList &L) L.elem=(int*)malloc(LIST_SIZE*sizeof(int); if(!L.elem) exit(0); L.length=0; L.listsize=LIST_SIZE; return OK;void ListCreate(SqList &L,int i) if(iL.listsize) exit(0); for(int j=0;ji;j+) scanf(%d,&L.elemj); L.length+; int SortInsert(SqList &L,int e) int i

3、,k=0; int *p,*q; p=L.elem; q=L.elem+L.length-1; for(i=1;i=L.length;i+) if(ep;q-) *q=*(q-1); *p=e; k=1; break; else p+; if(k=0) *(q+1)=e; L.length+; return OK;void main() void ListCreate(SqList &L,int i); int InitList_Sq(SqList &L); int SortInsert(SqList &L,int e); int i; SqList La; InitList_Sq(La);

4、ListCreate(La,3); int a; printf(需要插入的数字: ); scanf(%d,&a); SortInsert(La,a); printf(插入后的元素列表变为:n); for(i=0;iLa.length;i+) printf(%d ,La.elemi); printf(n);/*void SortDeleSame(SqList &L) int *i; int *p; int *q; p=L.elem; q=L.elem+L.length-1; while(pq) if(*p=*(p+1) for(i=p;iq;i+) *i=*(i+1); L.length-; q

5、-; else p+; void main() int InitList_Sq(SqList &L); void SortDeleSame(SqList &L); void ListCreate(SqList &L,int i); int i; SqList Lb; InitList_Sq(Lb); ListCreate(Lb,5); SortDeleSame(Lb); printf(n); for(i=0;iLb.length;i+) printf(%d ,Lb.elemi); */习题二、已知带头结点的动态单链表L中的结点是按整数值递增排列的,试写一算法将值x为的结点插入到表L中,使L仍然

6、有序。#include#include#includetypedef struct LNode int data; struct LNode *next;LNode,*LinkList;void main() void CreatList_LinkList(LinkList &L,int n); void ListInsert_LinkList(LinkList &L,int x); void Print_LinkList(LinkList &L,int n); LinkList La; int a,b; printf(请问要输入多少数:); scanf(%d,&a); printf(请输入这

7、%d个数:n,a); CreatList_LinkList(La,a); printf(请问要插入的数是:); scanf(%d,&b); ListInsert_LinkList(La,b); printf(插入后结果为:n); Print_LinkList(La,a+1); printf(n);void CreatList_LinkList(LinkList &L,int n) int i,a; LNode *s,*tail; L=(LinkList)malloc(sizeof(LNode); L-next=NULL; tail=L; for(i=0;idata); tail-next=s;

8、 tail=tail-next; tail-next=NULL;void Print_LinkList(LinkList &L,int n) LNode* p; p=L-next; for(int i=0;idata); p=p-next; void ListInsert_LinkList(LinkList &L,int x) int k=0; LNode *pre,*p,*s; pre=L; p=L-next; while(pre&p) if(xdata) s=(LinkList)malloc(sizeof(LNode); s-next=pre-next; pre-next=s; s-dat

9、a=x; k=1; break; pre=pre-next; p=p-next; if(k=0) s=(LinkList)malloc(sizeof(LNode); pre-next=s; s-data=x; s-next=NULL; 习题三、设计一算法,逆置带头结点的动态单链表L。/La为已知单链表,Lb为新建的一个单链表void Trunhead_LinkList(LinkList &La,LinkList &Lb,int n) LinkList p=La-next; LinkList s; Lb=(LinkList)malloc(sizeof(LNode); Lb-next=NULL;

10、for(i=0;idata=p-data; s-next=Lb-next; Lb-next=s; p=p-next; 一元多项式之和实验#include #include #include typedef struct polynode float coef; /系数 int expn; /指数 struct polynode *next;polynode,*polylist;void create(polylist &L) /创建链表 int m; polylist p; printf(请输入一元多项式项数:); scanf(%d,&m); L=(polylist)malloc(sizeof

11、(polynode); p=L; for(int i=1;inext=(polylist)malloc(sizeof(polynode); p=p-next; printf(请输入第%d项的系数和指数:,i); scanf(%f %d,&p-coef,&p-expn); p-next=NULL;void display(polylist L) /显示链表内容 polylist p; p=L-next; printf(%.0fx(%d),p-coef,p-expn); p=p-next; while(p) if(p-coefcoef,p-expn); else printf(+%.0fx(%d)

12、,p-coef,p-expn); p=p-next; printf(n);void add(polylist La, polylist Lb, polylist &Lc) /加法函数 polylist pa,pb,pc; float x; pa=La-next ; pb=Lb-next ; pc=(polylist)malloc(sizeof(polynode); Lc=pc; while (pa&pb) if(pa-expn=pb-expn) x=pa-coef+pb-coef; if(x!=0) pc-next=(polylist)malloc(sizeof(polynode); pc=p

13、c-next; pc-coef=x; pc-expn=pa-expn; pa=pa-next; pb=pb-next; else / 无同类项可合并,指数小者复制到C表 pc-next=(polylist)malloc(sizeof(polynode); pc=pc-next; if(pa-expn expn) /a的指数小于b的指数 pc-coef=pa-coef; pc-expn=pa-expn; pa=pa-next; else pc-coef=pb-coef; pc-expn=pb-expn; pb=pb-next; while(pa) /还剩下a多项式 pc-next=(polyli

14、st)malloc(sizeof(polynode); pc=pc-next; pc-coef=pa-coef; pc-expn=pa-expn; pa=pa-next; while(pb) /还剩下b多项式 pc-next=(polylist)malloc(sizeof(polynode); pc=pc-next; pc-coef=pb-coef; pc-expn=pb-expn; pb=pb-next; pc-next=NULL;void subtract(polylist La,polylist Lb,polylist &Lc) /减法函数 polylist pa,pb,pc; floa

15、t x; pa=La-next ; pb=Lb-next ; pc=(polylist)malloc(sizeof(polynode); Lc=pc; while(pa&pb) if(pa-expn=pb-expn) x=pa-coef-pb-coef; if(x!=0) pc-next=(polylist)malloc(sizeof(polynode); pc=pc-next; pc-coef=x; pc-expn=pa-expn; pa=pa-next; pb=pb-next; else /无同类项可合并,指数小者复制到C表 pc-next=(polylist)malloc(sizeof(

16、polynode); pc=pc-next; if (pa-expnexpn) /a的指数小于b的指数 pc-coef=pa-coef; pc-expn=pa-expn; pa=pa-next; else pc-coef=-pb-coef; pc-expn=pb-expn; pb=pb-next; while(pa) /还剩下a多项式 pc-next=(polylist)malloc(sizeof(polynode); pc=pc-next; pc-coef=pa-coef; pc-expn=pa-expn; pa=pa-next; while (pb) /还剩下b多项式 pc-next=(p

17、olylist)malloc(sizeof(polynode); pc=pc-next; pc-coef=-pb-coef; pc-expn=pb-expn; pb=pb-next; pc-next=NULL;void main() /主函数 polylist La,Lb,Lc,Ld; create(La); create(Lb); printf(一元多项式1:); display(La); printf(一元多项式2:); display(Lb); add(La,Lb,Lc); printf(加的结果:); display(Lc); subtract(La,Lb,Ld); printf(减的

18、结果); display(Ld);哈夫曼树实验#include#include#include#includetypedef struct char ch; int weight; int parent,lchild,rchild;HTNode,*HuffmanTree;typedef char*HuffmanCode;void CreateHuffmanTree(HuffmanTree &HT,int w,char ch,int n);void Select(HuffmanTree HT,int n,int &s1,int &s2);void HTCoding(HuffmanTree HT,

19、HuffmanCode &HC,int n);void PrintCode(HuffmanCode HC,int n,char ch);double AverageLength(HuffmanTree HT,HuffmanCode HC,int n);void DeCode(HuffmanTree HT,int n);void main() /主函数 int n; int i; char arrch20; int arrweight20; double avlength; char ch; HuffmanTree HT; HuffmanCode HC; printf(请输入要输入字母的个数:)

20、; scanf(%d,&n); while(ch=getchar()!=n); if(n20|n2) exit(0); for(i=0;in;i+) printf(请输入第%d个字母:,i+1); scanf(%c,&arrchi); printf(请输入该字母权重:); scanf(%d,&arrweighti); while(ch=getchar()!=n); CreateHuffmanTree(HT,arrweight,arrch,n); HTCoding(HT,HC,n); PrintCode(HC,n,arrch); avlength=AverageLength(HT,HC,n);

21、printf(平均编码长度为:%.2fn,avlength); printf(请输入要解码的数据:); DeCode(HT,n); for(i=0;in;i+) free(HCi); free(HC); free(HT);void CreateHuffmanTree(HuffmanTree &HT,int w,char ch,int n) int i,m,s1,s2; m=2*n-1; HT=(HuffmanTree)malloc(m+1)*sizeof(HTNode); for(i=1;i=n;i+) HTi.weight=wi-1; HTi.parent=0; HTi.lchild=0;

22、HTi.rchild=0; HTi.ch=chi-1; for(i=n+1;i=m;i+) HTi.weight=0; HTi.parent=0; HTi.lchild=0; HTi.rchild=0; HTi.ch=0; for(i=n+1;i=m;i+) Select(HT,i-1,s1,s2); HTs1.parent=i; HTs2.parent=i; HTi.lchild=s1; HTi.rchild=s2; HTi.weight=HTs1.weight+HTs2.weight; void Select(HuffmanTree HT,int n,int &s1,int &s2) in

23、t i; int min; min=1000; for(i=1;i=n;i+) if(HTi.parent=0&HTi.weight=min) min=HTi.weight; s1=i; min=1000; for(i=1;i=n;i+) if(HTi.parent=0&HTi.weight=min&i!=s1) min=HTi.weight; s2=i; void HTCoding(HuffmanTree HT,HuffmanCode &HC,int n) int i,j,k,start; int f; int c; char* cd; HC=(HuffmanCode)malloc(n)*s

24、izeof(char*); cd=(char*)malloc(n*sizeof(char); cdn-1=0; for(i=1;i=n;+i) start=n-1; for(c=i,f=HTi.parent;f!=0;c=f,f=HTf.parent) if(HTf.lchild=c) cd-start=0; else cd-start=1; HCi-1=(char*)malloc(n-start)*sizeof(char); for(j=start,k=0;jn;j+,k+) HCi-1k=cdj; free(cd);void PrintCode(HuffmanCode HC,int n,c

25、har ch) for(int i=0;in;i+) printf(%c的编码是%sn,chi,HCi); double AverageLength(HuffmanTree HT,HuffmanCode HC,int n) int i,j; int s1=0,s2=0; for(i=1;i=n;i+) s1=s1+HTi.weight; for(i=0,j=1;in;i+,j+) s2=s2+HTj.weight*strlen(HCi); return s2*1.0/s1;void DeCode(HuffmanTree HT,int n) int i; char endflag=#; char ch; i=2*n-1; scanf(%c,&ch); while(ch!=endflag) if(ch=0) i=HTi.lchild; else i=HTi.rchild; if(HTi.lchild=0) printf(%c

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

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