码迷,mamicode.com
首页 > 其他好文 > 详细

顺序栈

时间:2016-05-31 16:03:37      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

顺序栈

标签:

原文地址:http://www.cnblogs.com/ranranblog/p/5545792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!