顺序栈

来源:互联网 发布:概率影响矩阵 编辑:程序博客网 时间:2024/06/03 14:10

#include<iostream>

#include<string>

using namespace std;

template<class T>

class Student{

public:

Student(){top=-1;}

~Student(){}

void push(T x);

T pop();

T gettop(){if(top!=-1) return data[top];}

int empty(){if(top==-1)return 1;return 0;}

private:

int top;

T data[50];

};

template<class T>

void Student<T>::push(T x){

if(top==49)throw "上溢";

data[++top]=x;

}

template<class T>

T Student<T>::pop(){

if(top==-1)throw "下溢";

T x=data[top--];

return x;

}

void main(){

Student<string> stu;

string a[3]={"小明","小李","小栋"};

for(int i=0;i<3;i++){

stu.push(a[i]);

}

cout<<stu.gettop()<<endl;

cout<<stu.pop()<<endl;

cout<<stu.empty()<<endl;

}

原创粉丝点击