顺序栈

#include<iostream>

template<class T>
class SqStack
{
protected:
int count;
int maxSize;
T *elem;
public:
SqStack(int size);
~SqStack();
int Length(){ return count;};
bool Empty(){return count==0;};
bool Full() {return count==maxSize;};
void Clear(){ count=0;};
bool Push(const T &e);
bool Top(T &e);
bool Pop(T &e);

};


template<class T>
SqStack<T>::SqStack(int size)
{
maxSize=size;
elem=new T[maxSize];
count=0;
}

template<class T>
SqStack<T>::~SqStack()
{
delete []elem;


}

template<class T>
bool SqStack<T>::Push(const T &e)
{
if(Full())
return false;
else
{
elem[count]=e;
count++;
return true;
}
}

template<class T>
bool SqStack<T>::Top(T &e)
{
if(Empty())
return false;
else
{
e=elem[count-1];
return true;

}


}

template<class T>
bool SqStack<T>::Pop(T &e)
{
if(Empty())
return false;
else
{
e=elem[count-1];
count--;
return true;

}
}

 

 

 

#include"SqStack.h"
using namespace std;
int main()
{
SqStack<int> st(10);
cout<<st.Empty();
cout<<st.Full();
st.Push(1);
st.Push(1);
st.Push(1);
cout<<st.Length();
cin.get();
return 0;
}

posted @ 2016-05-31 14:39  ranran1203  阅读(126)  评论(0)    收藏  举报