#include <iostream>
using namespace std;
const int StackSize = 10;
template <class datatype>
class SeqStack
{
private:
datatype data[StackSize];
int top;
public:
SeqStack()
{
top = -1;
}
~SeqStack(){}
void Push(datatype x); //入栈
datatype Pop(); //出栈
datatype gettop()
{
if (top != -1)
return data[top];
}
int Empty()
{
if (top == -1)
return 1;
else
return 0;
}
};
template<class datatype>
void SeqStack<datatype>::Push(datatype x)
{
if (top == StackSize - 1)throw"上溢";
data[++top] = x;
}
template<class datatype>
datatype SeqStack<datatype>::Pop()
{
datatype x;
if (top == -1)throw"下溢";
x = data[top--];
return x;
}
void main()
{
SeqStack<int> s;
if (s.Empty())
cout << "栈为空" << endl;
else
cout << "栈非空" << endl;
cout << "对28,50,38,60,48,70依次进行入栈操作" << endl;
s.Push(28);
s.Push(50);
s.Push(38);
s.Push(60);
s.Push(48);
s.Push(70);
cout << "进行一次出栈操作"<<endl;
s.Pop();
cout << "此时栈顶元素为:" << endl;
cout << s.gettop() << endl;
using namespace std;
const int StackSize = 10;
template <class datatype>
class SeqStack
{
private:
datatype data[StackSize];
int top;
public:
SeqStack()
{
top = -1;
}
~SeqStack(){}
void Push(datatype x); //入栈
datatype Pop(); //出栈
datatype gettop()
{
if (top != -1)
return data[top];
}
int Empty()
{
if (top == -1)
return 1;
else
return 0;
}
};
template<class datatype>
void SeqStack<datatype>::Push(datatype x)
{
if (top == StackSize - 1)throw"上溢";
data[++top] = x;
}
template<class datatype>
datatype SeqStack<datatype>::Pop()
{
datatype x;
if (top == -1)throw"下溢";
x = data[top--];
return x;
}
void main()
{
SeqStack<int> s;
if (s.Empty())
cout << "栈为空" << endl;
else
cout << "栈非空" << endl;
cout << "对28,50,38,60,48,70依次进行入栈操作" << endl;
s.Push(28);
s.Push(50);
s.Push(38);
s.Push(60);
s.Push(48);
s.Push(70);
cout << "进行一次出栈操作"<<endl;
s.Pop();
cout << "此时栈顶元素为:" << endl;
cout << s.gettop() << endl;
}