欢迎来到天天文库
浏览记录
ID:44509145
大小:51.11 KB
页数:3页
时间:2019-10-22
《数据结构栈的应用》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、电子信息工程学系实验报告课程名称:数据结构与算法实验项目名称:栈的应用实验时间:成绩:指导教师(签名):班级:姓名:学号:实验目的:掌握栈的结构特性及其入栈,出栈操作实验环境:WindowsXPWin-Tc实验内容及过程:利用栈完成输入一个后缀表达式,输出它的值.编程代码如下:#include"stdio.h”#include"conio.h"typedeffloatElementType;typedefstructnode{ElementTypedata;structnode*link;}*Stack,Node;StackMakeEmptyO{StackS;S
2、=(Stack)malloc(sizeof(Node));S->link二NULL;returnS;}Push(StackS,ElementTypex){Stackp;p=(Stack)malloc(sizeof(Node));p->data=x;p->link=S->link;S->link=p;intPop(StackS)Stackp;if(isEmpty(S))return0;p=S->link;S->link=p->link;free(p);return1;}ElementTypeTop(StackS){ElementTypex;x=S->link->d
3、ata;returnx;}isEmpty(StackS){if(S->link==NULL)return1;elsereturn0;}DisposeStack(StackS){while(!isEmpty(S))Pop(S);free(S);}intpstype(charp){if(p==V
4、
5、p==I-l
6、p==>*l
7、p==7)return2;elsereturn1;}count(StackS,charp){ElementTypeopl,op2,op;op2=Top(S);Pop(S);opl=Top(S);Pop(S);switch(p){case屮:op
8、=opl+op2;break;caseL,:op=op1-op2;break;case*:op=opl*op2;break;case7':op=op1/op2;break;}Push(S,op);}Suffixcalculation(char*p,StackS){ElementTypex;while(*p!=fOf){switch(pstype(*p)){case1:x=*p-'O,;Push(S,x);break;case2:count(S,*p);break;}P++;}x=Top(S);Pop(S);printf("Theresultis:%0.1f
9、n,x);getch();}main(){StackS;char*p;S二MakeEmptyO;printf("Pleaseinputequation:");gets(p);Suffixcalculation(p,S);getch();}实验结果及分析:[JJE:Win-TCprojectsoname.exePleaseinputequation:85*3兴5-Theresultis:34.0分析:这个代码计算后缀表达式的方法是遇到操作数直接入栈,遇到操作符出栈,两个操作数算出结果再入栈,一直到计算出最后结果。实验心得:
此文档下载收益归作者所有