您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 数据结构课程实验报告(回文篇)
数据结构课程实验报告要求实验题目:回文判断算法班级通信143姓名刘海波学号2014101114日期2015.6.17一、需求分析1.程序的功能;利用栈和队列的操作来实现对字符序列是否是一个回文序列的判断。设计和验证入栈、出栈及入队、出队的算法。2.输入输出的要求;从键盘读入一组字符序列,按输入顺序入队列到链式队列A中。并将创建好的A队列中元素依次遍历,打印在屏幕上。将字符序列从A队列出队列,压入到一个顺序栈中。再将字符序列从顺序栈中出栈,入队到另一个链式队列B中。将创建好的B队列中元素依次遍历,打印在屏幕上。将A,B队列中的元素出队逐一比较,判断是否一致。若一致则是回文,并将判定结果打印到屏幕上。3.测试数据:输入一组字符串进行判断。二、概要设计1.本程序所用的抽象数据类型的定义;typedefstruct{charitem[STACKSIZE];inttop;}SqStack;typedefstructQNode{chardata;structQNode*next;}LQNode,*PQNode;typedefstruct{PQNodefront,rear;}LinkQueue;2.主程序的流程及各程序模块之间的层次关系。从键盘上读取一个字符,同时存储在顺序栈与链队列之中,直到字符序列的最后一个字符为*停止插入。在程序中设置了一个标志位flag,将输入的序列分别做入栈、出栈、入队、出队操作,若出栈与出队的数据完全一致,则将flag标志为1,否则为零。Flag为1,则表示该序列是回文序列,否则,为非回文序列。三、详细设计1.采用c语言定义相关的数据类型;typedefstruct{charitem[STACKSIZE];inttop;}SqStack;typedefstructQNode{chardata;structQNode*next;}LQNode,*PQNode;typedefstruct{PQNodefront,rear;}LinkQueue;2.写出各模块的伪码算法;intInitStack(SqStack*S)intStackEmpty(SqStackS)intPush(SqStack*s,chardata)intPop(SqStack*s,char*data)intInitQueue(LinkQueue*q)intQueueEmpty(LinkQueueq)intEnQueue(LinkQueue*q,charitem)intDeQueue(LinkQueue*q,char*item)intPutOutQueue(LinkQueueq)四、调试分析1.调试中遇到的问题及对问题的解决方法;对于语句中的一般回文单词能正常输出,句末跟标点符号连在一起的回文单词也能通过程序把字符串末尾的标点给去掉并正常输出,而字符串中的连接符可以作为回文单词的组成部分一起输出。2.算法的时间复杂度和空间复杂度。时间复杂度为O(n);空间复杂度为O(n)。五、使用说明及测试结果程序执行后显示以下内容:请输入一字符串;对该字符串进行判断;输出原字符串与逆字符串;判断是否为回文;输出结果。六、源程序(带注释)#includestdio.h#includestdlib.h#includestring.h#defineSTACKSIZE100typedefstruct{charitem[STACKSIZE];inttop;}SqStack;typedefstructQNode{chardata;structQNode*next;}LQNode,*PQNode;typedefstruct{PQNodefront,rear;}LinkQueue;intInitStack(SqStack*S){S-top=-1;return1;}intStackEmpty(SqStackS){if(S.top==-1)return1;elsereturn0;}intPush(SqStack*s,chardata){if(s-top==STACKSIZE-1){printf(\n栈已满,不能完成入栈操作);return0;}s-top++;s-item[s-top]=data;return1;}intPop(SqStack*s,char*data){if(s-top==-1){printf(\n堆栈已空,不能完成出栈操作);return0;}*data=s-item[s-top];s-top--;return1;}intInitQueue(LinkQueue*q){q-front=q-rear=(PQNode)malloc(sizeof(LQNode));if(!q-front){printf(\n初始化队列失败);return0;}q-front-next=NULL;return1;}intQueueEmpty(LinkQueueq){if(q.front==q.rear){printf(\n队列为空);return1;}elsereturn0;}intEnQueue(LinkQueue*q,charitem){PQNodep;p=(PQNode)malloc(sizeof(LQNode));if(!p){printf(\n内存分配失败);return0;}p-data=item;p-next=NULL;q-rear-next=p;q-rear=p;return1;}intDeQueue(LinkQueue*q,char*item){PQNodep;if(q-front==q-rear){printf(\n队列已空,不能出队);return0;}p=q-front-next;*item=p-data;q-front-next=p-next;free(p);if(q-rear==p)/*若删除的为最后一个结点,移动队尾指针*/q-front=q-rear;return1;}intPutOutQueue(LinkQueueq){PQNodepos;if(q.front==q.rear){printf(\n队列为空);return0;}pos=q.front-next;printf(\nHereisthestring:);while(pos!=NULL){printf(%c,pos-data);pos=pos-next;}printf(\n);return1;}intmain(void){inti,len,count1=0;charstr1[100],ch,ch1;LinkQueuelq1,lq2;SqStacksq;printf(Pleaseinputstring:);scanf(%s,&str1);len=strlen(str1);InitQueue(&lq1);InitQueue(&lq2);InitStack(&sq);for(i=0;ilen;i++){EnQueue(&lq1,str1[i]);}PutOutQueue(lq1);for(i=0;ilen;i++){DeQueue(&lq1,&ch);Push(&sq,ch);EnQueue(&lq1,ch);}for(i=0;ilen;i++){Pop(&sq,&ch);EnQueue(&lq2,ch);}PutOutQueue(lq2);for(i=0;ilen;i++){DeQueue(&lq1,&ch);DeQueue(&lq2,&ch1);if(ch1!=ch){count1++;}}if(count1==0){printf(\n该字符串为回文);}else{printf(\n该字符串不是回文);}return0;}
本文标题:数据结构课程实验报告(回文篇)
链接地址:https://www.777doc.com/doc-4296266 .html