实 验 报 告
课程名称: 任课教师: 班 级: 姓 名:
实验日期: 学 号:
实验项目名称: 栈和队列的基本操作及其应用 一、实验目的及要求 掌握栈和队列的顺序存储结构和链式存储结构,以便在实际中灵活应用。 掌握栈和队列的特点,即后进先出和先进先出的原则。 掌握栈和队列的基本运算,如:入栈与出栈,入队与出队等运算在顺序存储结构和链式存储结构上的实现。 二、实验环境 1.系统软件:Windows 2000、SQL Server 2000 2.工具:Visual studio 6.0或Visual studio 2003 三、实验内容与步骤: 1:字符串倒序 2:回文判断对于一个从键盘输入的字符串,判断其是否为回文。回文即正反序相同。如“abba”是回文,而“abab”不是回文。 3:设有两个栈S1,S2都采用顺序栈方式,并且共享一个存储区[O..maxsize-1],为了尽量利用空间,减少溢出的可能,可采用栈顶相向,迎面增长的存储方式。试设计S1,S2有关入栈和出栈的操作算法。 实验过程: 1:#include using namespace std; typedef double Stack_entry; const int maxstack=20; enum Error_code {underflow,overflow,success}; class Stack{ public: Stack(); bool empty()const; Error_code pop(); Error_code top(Stack_entry&item)const; Error_code push(const Stack_entry &item); private: 第 1 页 共 9 页int count; Stack_entry entry[maxstack]; }; Stack::Stack(){ count=0; } bool Stack::empty()const{ bool outcome=true; if(count>0) outcome=false; return outcome; } Error_code Stack::push(const Stack_entry &item){ Error_code outcome=success; if(count==0) outcome=overflow; else entry[count++]=item; return outcome; } Error_code Stack::pop(){ Error_code outcome=success; if(count==0) outcome=underflow; else --count; return outcome; } Error_code Stack::top(Stack_entry&item)const{ Error_code outcome=success; if(count==0) outcome=underflow; else item=entry[count-1]; return outcome; } void main(){ int n; double item; Stack numbers; cin>>n; for(int i=0;i>item; numbers.push(item); 第 2 页 共 9 页} cout< using namespace std; typedef char Stack_entry; const int maxstack=5; enum Error_code {success,underflow,overflow}; class Stack{ public: Stack(); bool empty(); Error_code pop(); Error_code top(Stack_entry &item); Error_code push(Stack_entry&item); private: int count; Stack_entry entry[maxstack]; }; bool Stack::empty(){ bool outcome=true; if(count>0) outcome=false; return outcome; } Error_code Stack::pop(){ Error_code outcome=success; if(count==0) 第 3 页 共 9 页outcome=underflow; else --count; return outcome; } Error_code Stack::top(Stack_entry &item) { Error_code outcome=success; if(count==0) outcome=underflow; else item=entry[count-1]; return outcome; } Error_code Stack::push(Stack_entry &item) { Error_code outcome=success; if(count>=maxstack) outcome=overflow; else entry[count++]=item; return outcome; } Stack::Stack(){ count=0; } void main(){ int i; int c[maxstack]; char item; Stack stra; for(i=0;i>item; c[i]=item; stra.push(item); } for(i=0;i实验结果:2: 3:#include using namespace std; typedef char Stack_entry; const int maxstack=50; enum Error_code {success,underflow,overflow}; class Stack{ public: Stack(); bool empty(); Error_code pop(); Error_code top(Stack_entry &item); Error_code push(Stack_entry&item); private: int count; Stack_entry entry[maxstack]; }; bool Stack::empty(){ bool outcome=true; if(count>0) outcome=false; return outcome; } Error_code Stack::pop(){ Error_code outcome=success; if(count==0) 第 5 页 共 9 页outcome=underflow; else --count; return outcome; } Error_code Stack::top(Stack_entry &item) { Error_code outcome=success; if(count==0) outcome=underflow; else item=entry[count-1]; return outcome; } Error_code Stack::push(Stack_entry &item) { Error_code outcome=success; if(count>=maxstack) outcome=overflow; else entry[count++]=item; return outcome; } Stack::Stack(){ count=0; } class Stack2{ public: Stack2(); bool empty(); Error_code pop(); Error_code top(Stack_entry &item); Error_code push(Stack_entry&item); private: int count; Stack_entry entry[maxstack]; }; bool Stack2::empty(){ bool outcome=true; if(countelse ++count; return outcome; } Error_code Stack2::top(Stack_entry &item) { Error_code outcome=success; if(count==maxstack) outcome=underflow; else item=entry[count+1]; return outcome; } Error_code Stack2::push(Stack_entry &item) { Error_code outcome=success; if(count<=0) outcome=overflow; else entry[count--]=item; return outcome; } Stack2::Stack2(){ count=maxstack; } void main(){ char a[maxstack]; char b[maxstack]; char c[maxstack]; int i;char item; Stack s1; Stack2 s2; for(i=0;i<10;i++){ cin>>item; a[i]=item; s1.push(a[i]); s2.push(a[i]); } for(i=0;i<10;i++){ s1.top(item); item=b[i]; s1.pop(); } for(i=0;i<10;i++){ s2.top(item); item=c[i]; s2.pop(); } for(i=0;i<10;i++){ cout<实验结果:3:程序可执行。 四、总结 。 第 8 页 共 9 页第 9 页 共 9 页