欢迎来到天天文库
浏览记录
ID:39576054
大小:42.00 KB
页数:4页
时间:2019-07-06
《实验四 栈的设计及应用》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验四栈的设计及应用一.实验目的1.掌握栈的逻辑结构。2.掌握顺序栈、链栈的设计过程。3.使用设计的栈实现进制转换。二、实验内容1.设计顺序栈:设计头文件SqStack.h,其内容如下:#defineMaxLen20#include"stdlib.h"typedefstruct{Selemtypedata[MaxLen];inttop;}SqStack;//初始化栈voidInitStack(SqStack&S){//栈中数据元素个数为0S.top=0;}//判栈空intStackEmpty(SqStackS){//栈中元素个数为0,
2、则为空if(S.top==0)return1;elsereturn0;}//判栈满intStackFull(SqStackS){//栈中元素个数为MaxLenif(S.top>MaxLen)return1;else4return0;}voidpush(SqStack&S,Selemtypee){if(StackFull(S)){cout<<"Stackisfull,theprogrambreak!";exit(-1);}//将元素e插入到栈顶S.data[S.top]=e;//栈中元素个数增加1S.top++;}voidpop(SqS
3、tack&S,Selemtype&e){if(StackEmpty(S)){cout<<"Stackisempty!";exit(-1);}//将栈顶元素通过形参e返回e=S.data[S.top-1];//栈中元素个数减少1S.top--;}voidGettop(SqStackS,Selemtype&e){if(StackEmpty(S)){cout<<"Stackisempty!";exit(-1);}//将栈顶元素通过形参e返回e=S.data[S.top-1];}42.利用顺序栈实现将一个十进制数转换成2、8、16进制:设计测
4、试文件test.cpp,其内容如下:#include"iostream.h"typedefintSelemtype;#include"SqStack.h"voidmain(){SqStacks;InitStack(s);intm;cout<<"pleaseinputanumber:";cin>>m;while(m){push(s,m%2);m=m/2;}while(!StackEmpty(s))Selemtypee;pop(s,e);cout<5、:#include"stdio.h"#include"stdlib.h"typedefstructsnode{Selemtypedata;snode*next;}SNode,*LinkStack;voidInitStack(LinkStack&S){S=NULL;}4intStackEmpty(LinkStackS){returnS==NULL;}voidpush(LinkStack&S,Selemtypee){SNode*p;p=newSNode;p->data=e;p->next=S;S=p;}voidpop(LinkStack&6、S,Selemtype&e){if(StackEmpty(S)){cout<<"Stackisempty!";exit(-1);}e=S->data;SNode*p=S;S=S->next;deletep;}voidGettop(LinkStackS,Selemtype&e){if(StackEmpty(S)){cout<<"Stackisempty!";exit(-1);}e=S->data;}三.分析4
5、:#include"stdio.h"#include"stdlib.h"typedefstructsnode{Selemtypedata;snode*next;}SNode,*LinkStack;voidInitStack(LinkStack&S){S=NULL;}4intStackEmpty(LinkStackS){returnS==NULL;}voidpush(LinkStack&S,Selemtypee){SNode*p;p=newSNode;p->data=e;p->next=S;S=p;}voidpop(LinkStack&
6、S,Selemtype&e){if(StackEmpty(S)){cout<<"Stackisempty!";exit(-1);}e=S->data;SNode*p=S;S=S->next;deletep;}voidGettop(LinkStackS,Selemtype&e){if(StackEmpty(S)){cout<<"Stackisempty!";exit(-1);}e=S->data;}三.分析4
此文档下载收益归作者所有