The Fourth Dimension Space

枯叶北风寒,忽然年以残,念往昔,语默心酸。二十光阴无一物,韶光贱,寐难安; 不畏形影单,道途阻且慢,哪曲折,如渡飞湍。斩浪劈波酬壮志,同把酒,共言欢! -如梦令

ACM模板之—堆栈(模板类)

//BEGIN_TEMPLATE_BY_ABILITYTAO_ACM
#include<iostream>
#include
<algorithm>
#include
<cassert>
using namespace std;

template
<class T>
class Stack
{

private:
    
int top;
    T 
*element;
    
int maxsize;
public:
    Stack(
int n=100000);
    
~Stack(){delete []element;}
    
void push(const T &item);
    T pop();
    T gettop();
    
int size();
    
void clear(){top=-1;}
    
bool isempty()const {return top==-1;}
    
bool isfull()const {return top==maxsize-1;}
}
;

template
<class T>
Stack
<T>::Stack(int n = 100000):top(-1),maxsize(n)
{

    element
=new T[maxsize];
    assert(element
!=0);
}


template
<class T>
void Stack<T>::push(const T &item)
{

    assert(
!isfull());
    element[
++top]=item;
}


template
<class T>
T Stack
<T>::pop()
{

    assert(
!isempty());
    
return element[top--];
}


template
<class T>
T Stack
<T>::gettop()
{

    assert(
!isempty());
    
return element[top];
}

template
<class T>
int Stack<T>::size()
{
    
return top+1;
}

//END_TEMPLATE_BY_ABILITYTAO_ACM


int main ()
{

    Stack
<int>test;
    
bool b;
    
int i;
    
int n;
    
for(i=1;i<=10;i++)
    
{
        b
=test.isfull();
        test.push(i);
    }

    n
=test.size();
    b
=test.isfull();
    
for(i=1;i<=5;i++)
        
int n=test.pop();
    test.clear();
    
for(i=1;i<=10;i++)
        test.push(i);
    
for(i=1;i<=10;i++)
    
{
        b
=test.isempty();
        cout
<<test.pop();
    }

    b
=test.isempty();
    
return 0;
    


}

posted on 2009-07-14 16:31 abilitytao 阅读(92) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理