栈和队列的基本操作及其应用_第1页
栈和队列的基本操作及其应用_第2页
栈和队列的基本操作及其应用_第3页
栈和队列的基本操作及其应用_第4页
栈和队列的基本操作及其应用_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第8页共9页第1页共3页实验报告课程名称:任课教师:实验日期:班级:姓名:学号:实验项目名称:栈和队列的基本操作及其应用一、实验目的及要求掌握栈和队列的顺序存储结构和链式存储结构,以便在实际中灵活应用。掌握栈和队列的特点,即后进先出和先进先出的原则。掌握栈和队列的基本运算,如:入栈与出栈,入队与出队等运算在顺序存储结构和链式存储结构上的实现。二、实验环境1.系统软件:Windows2000、SQLServer20002.工具:Visualstudio6.0或Visualstudio2003三、实验内容与步骤:1:字符串倒序2:回文判断对于一个从键盘输入的字符串,判断其是否为回文。回文即正反序相同。如“abba”是回文,而“abab”不是回文。3:设有两个栈S1,S2都采用顺序栈方式,并且共享一个存储区[O..maxsize-1],为了尽量利用空间,减少溢出的可能,可采用栈顶相向,迎面增长的存储方式。试设计S1,S2有关入栈和出栈的操作算法。实验过程:1:#include<iostream>usingnamespacestd;typedefdoubleStack_entry;constintmaxstack=20;enumError_code{underflow,overflow,success};classStack{public:Stack();boolempty()const;Error_codepop();Error_codetop(Stack_entry&item)const;Error_codepush(constStack_entry&item);private: intcount; Stack_entryentry[maxstack];};Stack::Stack(){count=0;}boolStack::empty()const{booloutcome=true;if(count>0) outcome=false; returnoutcome;}Error_codeStack::push(constStack_entry&item){ Error_codeoutcome=success; if(count==0) outcome=overflow; else entry[count++]=item; returnoutcome;}Error_codeStack::pop(){Error_codeoutcome=success;if(count==0) outcome=underflow; else--count; returnoutcome;}Error_codeStack::top(Stack_entry&item)const{Error_codeoutcome=success; if(count==0) outcome=underflow; else item=entry[count-1]; returnoutcome;}voidmain(){intn; doubleitem; Stacknumbers; cin>>n; for(inti=0;i<n;i++){ cin>>item; numbers.push(item); } cout<<endl<<endl; while(!numbers.empty()){ cout<<numbers.top(item)<<""; numbers.pop(); } cout<<endl;}实验结果:1:2:#include<iostream>usingnamespacestd;typedefcharStack_entry;constintmaxstack=5;enumError_code{success,underflow,overflow};classStack{public: Stack(); boolempty(); Error_codepop(); Error_codetop(Stack_entry&item); Error_codepush(Stack_entry&item);private: intcount; Stack_entryentry[maxstack];};boolStack::empty(){ booloutcome=true; if(count>0) outcome=false; returnoutcome;}Error_codeStack::pop(){ Error_codeoutcome=success; if(count==0) outcome=underflow; else--count; returnoutcome;}Error_codeStack::top(Stack_entry&item){ Error_codeoutcome=success; if(count==0) outcome=underflow; else item=entry[count-1]; returnoutcome;}Error_codeStack::push(Stack_entry&item){ Error_codeoutcome=success; if(count>=maxstack) outcome=overflow; else entry[count++]=item; returnoutcome;}Stack::Stack(){ count=0;}voidmain(){ inti; intc[maxstack]; charitem;Stackstra;for(i=0;i<maxstack;i++){ cin>>item; c[i]=item; stra.push(item); } for(i=0;i<maxstack;i++){ stra.top(item); if(c[i]==item)stra.pop(); elsebreak; } if(i==maxstack)cout<<"是回文数"; elsecout<<"不是回文数"<<endl;}实验结果:2:3:#include<iostream>usingnamespacestd;typedefcharStack_entry;constintmaxstack=50;enumError_code{success,underflow,overflow};classStack{public: Stack(); boolempty(); Error_codepop(); Error_codetop(Stack_entry&item); Error_codepush(Stack_entry&item);private: intcount; Stack_entryentry[maxstack];};boolStack::empty(){ booloutcome=true; if(count>0) outcome=false; returnoutcome;}Error_codeStack::pop(){ Error_codeoutcome=success; if(count==0) outcome=underflow; else--count; returnoutcome;}Error_codeStack::top(Stack_entry&item){ Error_codeoutcome=success; if(count==0) outcome=underflow; else item=entry[count-1]; returnoutcome;}Error_codeStack::push(Stack_entry&item){ Error_codeoutcome=success; if(count>=maxstack) outcome=overflow; elseentry[count++]=item; returnoutcome;}Stack::Stack(){ count=0;}classStack2{public: Stack2(); boolempty(); Error_codepop(); Error_codetop(Stack_entry&item); Error_codepush(Stack_entry&item);private: intcount; Stack_entryentry[maxstack];};boolStack2::empty(){ booloutcome=true; if(count<maxstack) outcome=false; returnoutcome;}Error_codeStack2::pop(){ Error_codeoutcome=success; if(count==maxstack) outcome=underflow; else++count; returnoutcome;}Error_codeStack2::top(Stack_entry&item){ Error_codeoutcome=success; if(count==maxstack) outcome=underflow; else item=entry[count+1]; returnoutcome;}Error_codeStack2::push(Stack_entry&item){ Error_codeoutcome=success; if(count<=0) outcome=overflow; else entry[count--]=item; returnoutcome;}Stack2::Stack2(){ count=maxstack;}voidmain(){ chara[maxstack]; charb[maxstack]; charc[maxstack]; inti;charitem; Stacks1; Stack2s2; for(i=0;i<10;i++){

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论