- 254.00 KB
- 2022-04-22 11:31:59 发布
- 1、本文档共5页,可阅读全部内容。
- 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
- 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
- 文档侵权举报电话:19940600175。
'课后习题答案第1章数据结构导论一、填空题1.集合结构,线性结构,树形结构,图状结构2.顺序存储结构,链式存储结构3.有限性,确定性,可行性,输入,输出4.时间复杂度,空间复杂度二、分析下面程序段的时间复杂度。1.O(m*n)2.O(n2)三、上机操作题1.解答:#includevoidmain(){floata[10];inti,n;floats=0;printf("输入数组中元素的个数n:");scanf("%d",&n);for(i=0;ivoidmain(){intx,y,z,t;printf("请依次输入x、y和z的值:n");scanf("%d%d%d",&x,&y,&z);if(xnext;//p为链表中第二个结点r=*list;while(p!=NULL){if(p->data>q->data){q=p;//保存当前最大值结点s=r;//保存当前最大值结点的前驱}r=p;//保存p所指结点位置p=p->next;//p指向下一结点}if(q==*list)//链表中第一个结点为最大值结点*list=(*list)->next;//删除第一个结点else//最大值结点不是链表的第一个结点s->next=q->next;//删除q所指结点free(q);//释放q所指结点的空间}33
课后习题答案3.解答:voidDeleteItem(DLinkList*list,ElemTypeitem){DNode*q;q=(*list)->next;//q指向头结点的后继while(q!=*list){if(q->data==item)//q所指结点的值为item{q->prior->next=q->next;//删除q所指结点q->next->prior=q->prior;free(q);//释放q所指结点的空间}q=q->next;//q指向下一结点}}4.解答:voidInsertItem(DLinkListq,ElemTypeitem){DLinkListp;p=(DLinkList)malloc(sizeof(DNode));//生成新结点p->data=item;//新结点的数据域赋值p->prior=q;//将新结点插入到q所指结点之后p->next=q->next;//修改p所指结点的指针域q->next=p;//修改q所指结点的后继p->next->prior=p;//修改p的后继结点的前驱}第3章栈和队列一、填空题1.线性2.栈顶,队尾,队头3.后进先出,先进先出4.数组,指示栈顶元素的位置5.将栈顶指针后移一个位置,将被插入元素放在修改后的栈顶指针所指出的位置6.链式33
课后习题答案二、选择题1.C2.D3.C4.C5.B6.A7.B8.D9.C10.D三、上机操作题1.解答:表3-1算术表达式求值的过程步骤OPTR栈OPND栈当前读入字符步骤OPTR栈OPND栈当前读入字符1#358#-/355022#35-9#-/35502+3#-35510#-3525+4#-355*11#10+5#-*3551012#+1056#-*35510/13#+105#7#-3550/14#15#2.解答:charop[7]={"+","-","*","/","(",")","#"};//算符数组charcmp[7][7]={{">",">","<","<","<",">",">"},{">",">","<","<","<",">",">"},{">",">",">",">","<",">",">"},{">",">",">",">","<",">",">"},{"<","<","<","<","<","=",""},{">",">",">",">","",">",">"},{"<","<","<","<","<","","="}};//算符优先关系数组//输入字符是否属于运算符集合,如果是,返回它在数组中的位置;否则,返回-1intIsoperator(charch){inti;for(i=0;i<7;i++){if(ch==op[i])returni;33
课后习题答案}return-1;}//比较两个运算符的优先级charCompare(charch1,charch2){intm,n;m=Isoperator(ch1);n=Isoperator(ch2);returncmp[m][n];}//将中缀表达式转换为后缀表达式intTransExpression(char*str1,char*str2){inti=0,k=0;charx,theta;SeqStackOPTR//定义运算符栈IniStack(&OPTR);//初始化运算符栈Push(&OPTR,"#");//将"#"压入栈底while(str1[i]!="#"||GetTop(OPTR)!="#"){if(str1[i]>="0"&&str1[i]<="9")//如果读入字符为数则复制到str2中str2[k++]=str1[i++];else{if(Isoperator(str1[i])==-1)//如果读入字符不是操作符,则出错returnERROR;switch(Compare(GetTop(OPTR),str[i]))//比较操作符优先级高低{case"<"://读入字符优先级高于栈顶字符优先级Push(&OPTR,str[i]);//操作符进栈i++;break;case"="://读入字符优先级等于栈顶字符优先级Pop(&OPTR,x);//栈顶元素出栈i++;break;case">"://读入字符优先级低于栈顶字符优先级Pop(&OPTR,&theta);//栈顶元素出栈33
课后习题答案str2[k++]=theta;//出栈元素复制到str2中break;}}}returnOK;}3.解答:intIsHuiwen(char*S){SeqStackT;inti,len;chart;InitStack(&T);len=StrLength(S);//求字符串长度for(i=0;i