实验之顺序栈

实验目的:

学习顺序栈后,熟练应用顺序栈于实践应用中,明白顺序栈的原理,加以灵活改变应用。


实验要求:

运用顺序栈存放学生信息,并运行程序。


实验代码:

头文件:

#include <iostream>
const int Max=100;
class Seqstack
{
public:
 Seqstack(){top=-1;}
 ~Seqstack(){}
 void push();
 void pop();
 int Getpop(){if (top!=-1)return data[top];}
 int empyt(){if(top==-1) return 1;else return 0;}
private:
 int data[Max];
 int top;
 };


源文件:

#include"tou.h"
#include <iostream>
using namespace std;
void Seqstack::push(){
 int x;
 cin>>x;
 if(top==Max-1) throw"over";
  data[++top]=x;
}
 
void Seqstack::pop(){
 int x;
 if(top==-1) throw"over";
 x=data[top--];
 cout<<x;
}

void main(){
 Seqstack x; int n,i;
 cout<<"please input the number:"<<endl;
 cin>>n;
 for(i=1;i<=n;i++){
  x.push();}
 cout<<x.empyt()<<endl;
 cout<<x.Getpop()<<endl;
 for(i=1;i<=n;i++){
  x.pop();}
 system("pause");
}


运行结果:



实验总结:

栈是操作受限的线性表,允许插入和删除的一端称为栈顶,另一端为栈底,具有后进先出的特性,将数组作为存储空间即可。



猜你喜欢

转载自blog.csdn.net/Lxin12138/article/details/78324431