• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C++模板化堆栈类

OC/C/C++ 水墨上仙 2806次浏览

一个C++堆栈类,已经模板化,支持泛型

//An example of using templated class to create stack depends on underlying array.
#include<iostream>
#include<cstdlib>
#define default_value 10
using namespace std;
 
template< class T > class Stack
{
    public:
    Stack(int = default_value);//default constructor
    ~Stack()//destructor
    {delete [] values;}
    bool push( T );
    T pop();
    bool isEmpty();
    bool isFull();
    private:
    int size;
    T *values;
    int index;
 
};
 
template< class T > Stack<T>::Stack(int x):
    size(x),//ctor
    values(new T[size]),
    index(-1)
{ /*empty*/  }
 
template< class T > bool Stack<T>::isFull()
{
    if((index + 1) == size )
    return 1;
    else
    return 0;
}
 
template< class T > bool Stack<T>::push(T x)
{
    bool b = 0;
    if(!Stack<T>::isFull())
    {
    index += 1;
    values[index] = x;
    b = 1;
    }
    return b;
}
 
template< class T > bool Stack<T>::isEmpty()
{
    if( index  == -1 )//is empty
    return 1;
    else
    return 0; //is not empty
}
 
template< class T > T Stack<T>::pop()
{
    T val = -1;
    if(!Stack<T>::isEmpty())
    {
    val = values[index];
    index -=  1;
    }
    else
    {
    cerr << "Stack is Empty : ";
    }
    return val;
 
}
 
int main()
{
    Stack <double> stack1;
    Stack <int> stack2(5);
    int y = 1;
    double x = 1.1;
    int i, j; 
    cout << "\n pushed values into stack1: ";
 
    for( i = 1  ; i <= 11 ; i++) //start enter 11 elements into stack
    {
    if(stack1.push(i*x))
        cout << endl << i*x;
    else
        cout << "\n Stack1 is full: ";
    }
 
    cout << "\n\n popd values from stack1 : \n";
    for( j = 1 ; j <= 11 ; j++)
    cout << stack1.pop() << endl;
 
 
 
    cout << "\n pushd values into stack2: ";
 
    for( i = 1  ; i <= 6 ; i++) //start enter 6 elements into stack
    {
    if(stack2.push(i*y))
        cout << endl << i*y;
    else
        cout << "\n Stack2 is full: ";
    }
 
    cout << "\n\n popd values from stack2: \n";
    for( j = 1 ; j <= 6 ; j++)
    cout << stack2.pop() << endl;
    cout << endl << endl;
    return 0;
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C++模板化堆栈类
喜欢 (0)
加载中……