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