- 65
- 0
- 约6.39千字
- 约 33页
- 2019-07-23 发布于广东
- 举报
* 习题课一 1.5 确定下列各程序段的程序步,确定划线语句的执行次数,计算它们的渐近时间复杂度。 习题一(第18页) (1) i=1; k=0; do { k=k+10*i; i++; } while(i=n-1) 答: 划线语句的执行次数为 n-1 。O(n) (2)i=1; x=0; i k(循环次数) 2*i do 1 1 21n { 2 2 22n x++; i=2*i; 2k-1 n 2kn } while (in); 2kn, klog2n, k=?log2n? 划线语句的执行次数为 ?log2n?。O(log2n) (3) for(int i=1;i=n;i++) for(int j=1;j=i;j++) for (int k=1;k=j;k++) x++; 划线语句的执行次数为 n(n+1)(n+2)/6 。O(n3) (4)x=n;y=0; while(x=(y+1)*(y+1)) y++; nk2 划线语句的执行次数为 ? ?。O( ) #include iostream.h #include seqlist0.h #include conio.h template class T void InterSection(SeqListT LA,SeqListT LB) { int m=LB.Length(); SeqListT LC(10); T x; for (int i=1;i=m;i++) { LB.Find(i,x); if (LA.Search(x)) LC.Insert(LC.Length()+1,x); } coutLC; } 2.1 利用线性表类LinearList提供的操作,实现两个集合的交和差运算。 习题二(第37页) template class T void Difference(SeqListT LA,SeqListT LB) { int m=LA.Length(); SeqListT LC(10); T x; for (int i=1;i=m;i++) { LA.Find(i,x); if (LB.Search(x)==0) LC.Insert(LC.Length()+1,x); } coutLC; } void main() { SeqList int LA(10),LB(10); for (int i=1;i=8;i++) LA.Insert(i,i); for (i=1;i=3;i++) LB.Insert(i,i+3); InterSection(LA,LB); Difference(LA,LB); } 2.2 (2) 在类LinearList 中增加一个成员函数,将顺序表逆置,实现该函数并分析算法的时间复杂度。不利用类SeqList 提供的操作直接实现。 template class T void SeqListT::Invert() { T e; for (int i=0;ilength/2;i++) { e=elements[i]; elements[i]=elements[length-i-1]; elements[length-i-1]=e; } } O(n) 2.5 在类SingleList中增加一个成员函数,将单链表逆置运算,直接实现该函数并分析其时间复杂度。 template class T void SingleListT::invert() { NodeT *p=first,*q; first=NULL; while (p) { q=p-link; p-link=first; first=p; p=q; } } 2.7 单链表中结点按元素值递增链接,在类SingleList中增加一个成员函数,直接实现删除结点值在a至b之间的结点(a?b)。 template class T void SingleListT::DeleteAb(T a,T b) { NodeT *p=first,*q=first; while (p p-datab)
原创力文档

文档评论(0)