首页 > 其他 > 详细

顺序栈

时间:2016-05-31 16:01:32      阅读:176      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!