当前位置:文档之家› 数据结构上机答案

数据结构上机答案

第一章 16 void Descend(int &x, int &y, int &z) { int t; if(x<y) {t=x;x=y;y=t;} if(x<z) {t=x;x=z;z=t;} if(y<z) {t=y;y=z;z=t;} } 17 Status Fibonacci(int k, int m, int &f) /* 求k阶斐波那契序列的第m项的值f */ { int i,j,sum,temp[20]; if(k<2||m<0) return ERROR; if(m<k-1) f=0; else if(m==k-1) f=1; else {for(i=0;i<=k-2;i++) temp[i]=0; temp[k-1]=1; for(i=k;i<=m;i++) {sum=0; for(j=i-k;j<i;j++) sum+=temp[j]; temp[i]=sum; } f=temp[m]; } return OK; }// Fibonacci 18
*/
/* maxk from the single sorted LinkList with head pointer L.*/ { LinkList p,q,pre; pre=L; while(pre->next&&pre->next->data<=mink) pre=pre->next; p=pre->next; while(p&&p->data<maxk) { q=p; p=p->next; free(q); } pre->next=p; } 21 void Inverse(SqList &L) { int i,j; ElemType p; for(i=0,j=L.length-1;i<j;i++,j--) { p=L.elem[j]; L.elem[j]=L.elem[i]; L.elem[i]= p; } } 21 void Inverse(LinkList &L) /* 对带头结点的单链表L实现就地逆置 */ { LinkList p,q,s; p=L->next; q=p->next; if(p->next!=NULL&&q->next!=NULL)
for(j=n;j>0;j--) y=x*(y+a[j]); s=y+a[0]; return(s); } 第二章 12 char Compare(SqList A, SqList B) // 比较顺序表A和B, // 返回'<', 若A<B; // '=', 若A=B; // '>', 若A>B { int i; for(i=1;i<=A.length&&i<=B.length;i++) if(A.elem[i]!=B.elem[i]) return A.elem[i]>B.elem[i]?'>':'<'; if(A.length==B.length) return '='; return A.length>B.length?'>':'<'; } 14 int Length(LinkList L) // Return the length of the linked list // whose head node is pointed by 'L' { LNode *p; int k; p=L; for(k=0;(p->next)!=NULL;p=p->next,k++); return k; } 19 void DeleteSome(LinkList &L, ElemType mink, ElemType maxk) /* Delete all the elements which value is between mink and
பைடு நூலகம்
t->next=lb; la=s; lb=t; while(s->next!=NULL && x<(i-1)) { s=s->next; x++; } if(s->next==NULL && x<(i-1)) { s=la; t=lb; la=la->next; lb=lb->next; free(s); free(t); return INFEASIBLE; } r=o=s->next; while(o!=NULL && z<len) { o=o->next; z++; } if(o==NULL || z<len) { s=la; t=lb; la=la->next; lb=lb->next; free(s); free(t); return INFEASIBLE; } while(t->next!=NULL && y<(j-1)) { t=t->next;
} i++; } for(s='A';s<='E';s++) { printf("School %c:\n",s); printf("Total score of male:%d\n",score[i].malescore); printf("Total score of female:%d\n",score[i].femalescore); printf("Total score of all:%d\n\n",score[i].totalscore); } } 19 Status Series(int ARRSIZE, int a[]) /* 求i!*2^i序列的值并依次存入长度为ARRSIZE的数组a; */ /* 若所有值均不超过MAXINT,则返回OK,否则返回OVERFLOW */ { int last,i; last=1; for (i=1;i<=ARRSIZE;i++) { a[i-1]=last*2*i; if (a[i-1]>MAXINT) return OVERFLOW; last=a[i-1]; } return OK; } 20 float Polynomial(int n, int a[], float x) /* 求一元多项式的值P(x)。 */ /* 数组a的元素a[i]为i次项的系数,i=0,...,n */ { int j,i; float y,s; y=0;
p=q->next; t->next=p; free(q); } p->coef*=p->exp; p->exp--; t=p; p=p->next; } } 13 LinkList Locate(LinkList &L, ElemType x) // If 'x' in the linked list whose head node is pointed // by 'L', then return pointer ha pointing node 'x', // otherwise return 'NULL' { LNode*p; for(p=L->next;p&&p->data!=x;p=p->next); return p; } 16 Status DeleteAndInsertSub(LinkList &la, LinkList &lb, int i, int j, int len) // la和lb分别指向两个单链表中第一个结点, */ /* 本算法是从la表中删去自第i个元素起共len个元素,*/ /* 并将它们插入到lb表中第j个元素之前, */ /* 若lb表中只有j-1个元素,则插在表尾。 */ { if(i<=0 || j<=0 || len<=0) return INFEASIBLE; LNode *s,*t,*o,*r; int x=0,y=0,z=1; s=(LinkList)malloc(sizeof(LNode)); t=(LinkList)malloc(sizeof(LNode)); s->next=la;
{ s=q->next;p->next=NULL; while(s->next) { q->next=p;p=q;q=s;s=s->next; } q->next=p;s->next=q;L->next=s; } } 31 ElemType DeleteNode(LinkList s) /* 删除指针s所指结点的前驱结点,并返回被删结点的元素值 */ { LNode *p; int e; p=s; while(p->next->next!=s) p=p->next; e=p->next->data; p->next=s; return e; } 41 void Difference(LinkedPoly &pa) /* 稀疏多项式 pa 以循环链表作存储结构, */ /* 将此链表修改成它的导函数,并释放无用结点 */ { LinkedPoly p,q,t; p=pa->next; t=pa; while(p!=pa) { if(p->exp==0) { q=p;
void Scores(ResultType *result, ScoreType *score) /* 求各校的男、女总分和团体总分, 并依次存入数组score */ /* 假设比赛结果已经储存在result[ ]数组中, */ /* 并以特殊记录 {"", male, ' ', "", 0 }(域scorce=0)*/ /* 表示结束 */ { int i=0; char s; while(result[i].sport!=NULL) { switch(result[i].schoolname) { case 'A': score[0].totalscore+=result[i].score; if(result[i].gender==male) score[0].malescore+=result[i].score; else score[0].femalescore+=result[i].score; break; case 'B': score[1].totalscore+=result[i].score; if(result[i].gender==male) score[1].malescore+=result[i].score; else score[1].femalescore+=result[i].score; break; case 'C': score[2].totalscore+=result[i].score; if(result[i].gender==male) score[2].malescore+=result[i].score; else score[2].femalescore+=result[i].score; break; case 'D': score[3].totalscore+=result[i].score; if(result[i].gender==male) score[3].malescore+=result[i].score; else score[3].femalescore+=result[i].score; break; case 'E': score[4].totalscore+=result[i].score; if(result[i].gender==male) score[4].malescore+=result[i].score; else score[4].femalescore+=result[i].score; break;
相关主题