#include<iostream>
using namespace std;
#define Maxsize 10
template <class T>
class Stack
{
public:
Stack()
{
top=-1;
length=0;
}
~Stack() {}
void push(T x);
T pop();
T gettop();
int Empty();
int Size()
{
return length;
}
private:
T Date[Maxsize];
int top;
int length;
};
template <class T>
void Stack<T>::push(T x)
{
if(top==Maxsize-1)
{
throw"栈满无法入栈";
}
Date[++top]=x;
length++;
}
template <class T>
T Stack<T>::pop()
{
if(top==-1)
{
throw"栈空无法出栈";
}
T x=Date[top--];
return x;
}
template <class T>
T Stack<T>::gettop()
{
if(top==-1)
{
throw"栈空";
}
return Date[top];
}
template <class T>
int Stack<T>::Empty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
Stack<int>stack1;
stack1.push(10);
stack1.push(2);
stack1.push(4);
stack1.push(4);
while(stack1.Empty()!=1)
{
cout<<stack1.pop()<<endl;
}
cout<<stack1.Size()<<endl;
}
using namespace std;
#define Maxsize 10
template <class T>
class Stack
{
public:
Stack()
{
top=-1;
length=0;
}
~Stack() {}
void push(T x);
T pop();
T gettop();
int Empty();
int Size()
{
return length;
}
private:
T Date[Maxsize];
int top;
int length;
};
template <class T>
void Stack<T>::push(T x)
{
if(top==Maxsize-1)
{
throw"栈满无法入栈";
}
Date[++top]=x;
length++;
}
template <class T>
T Stack<T>::pop()
{
if(top==-1)
{
throw"栈空无法出栈";
}
T x=Date[top--];
return x;
}
template <class T>
T Stack<T>::gettop()
{
if(top==-1)
{
throw"栈空";
}
return Date[top];
}
template <class T>
int Stack<T>::Empty()
{
if(top==-1)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
Stack<int>stack1;
stack1.push(10);
stack1.push(2);
stack1.push(4);
stack1.push(4);
while(stack1.Empty()!=1)
{
cout<<stack1.pop()<<endl;
}
cout<<stack1.Size()<<endl;
}